Skip to content

Commit

Permalink
feature: support mac monterey
Browse files Browse the repository at this point in the history
em, bad case...

Signed-off-by: o98k-ok <[email protected]>
  • Loading branch information
o98k-ok committed Feb 14, 2022
1 parent 28c4f0f commit b26acce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
25 changes: 2 additions & 23 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/haoguanguan/bluetooth_flow/models"
"github.com/haoguanguan/bluetooth_flow/plist"
"github.com/haoguanguan/bluetooth_flow/system"
"github.com/urfave/cli/v2"
"os"
Expand All @@ -27,33 +26,13 @@ func show() {
return
}

pp, err := plist.NewPlist("/Library/Preferences/com.apple.Bluetooth.plist")
if err != nil {
items.Append(models.NewItem("ERROR", "init bluetooth plist failed", "", icons["Default"]))
fmt.Println(items.Encode())
return
}

for _, device := range system.GetAllBlueTooth() {
var iconPath string
if device.Product != "" {
iconPath = icons[device.Product]
}

battery := device.BatteryLevel
// for airpods, need refill battery level by plist
if device.Product == "Headphones" {
attrs := [][]string{
{"DeviceCache", device.Addr, "BatteryPercentCase"},
{"DeviceCache", device.Addr, "BatteryPercentLeft"},
{"DeviceCache", device.Addr, "BatteryPercentRight"},
}
batteries := pp.GetAttrByNames(attrs)
if len(batteries) == len(attrs[0]) {
battery = fmt.Sprintf("C:%v%%/L:%v%%/R:%v%%", batteries[0], batteries[1], batteries[2])
}
}

var subInfo, nextOP string
if device.Status {
subInfo = fmt.Sprintf("Connected %s", battery)
Expand All @@ -69,15 +48,15 @@ func show() {
}

func connect(addr string) {
command := fmt.Sprintf("./blueutil --connect %s", addr)
command := fmt.Sprintf("./blueutil --connect %s --info %s", addr, addr)
_, err := exec.Command("bash", "-c", command).CombinedOutput()
if err != nil {
fmt.Printf("connect bluetooth error %v\n", err)
}
}

func disconnect(addr string) {
command := fmt.Sprintf("./blueutil --disconnect %s", addr)
command := fmt.Sprintf("./blueutil --disconnect %s --info %s", addr, addr)
_, err := exec.Command("bash", "-c", command).CombinedOutput()
if err != nil {
fmt.Printf("disconnect bluetooth error %v\n", err)
Expand Down
36 changes: 29 additions & 7 deletions system/bluetooth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package system

import (
"fmt"
"github.com/haoguanguan/bluetooth_flow/models"
"strings"
)

var BlueTooth *Info
Expand Down Expand Up @@ -29,21 +31,41 @@ func GetAllBlueTooth() []models.BlueToothDevice {
res := make([]models.BlueToothDevice, 0, 3)

for _, dataType := range dataTypes {
deviceAll := dataType.(map[string]interface{})["device_title"]
deviceAll := dataType.(map[string]interface{})["devices_list"]
for _, device := range deviceAll.([]interface{}) {
for name, d := range device.(map[string]interface{}) {
info := d.(map[string]interface{})

res = append(res, models.BlueToothDevice{
device := models.BlueToothDevice{
Name: name,
Addr: AsString(info, "device_addr"),
BatteryLevel: AsString(info, "device_batteryPercent"),
Product: AsString(info, "device_minorClassOfDevice_string"),
Status: AsString(info, "device_isconnected") == "attrib_Yes",
})
Addr: strings.Replace(AsString(info, "device_address"), ":", "-", -1),
BatteryLevel: GetBattery(info),
Product: AsString(info, "device_minorType"),
Status: AsString(info, "device_connected") == "Yes",
}
if device.Product != "" {
res = append(res, device)
}
}

}
}
return res
}

func GetBattery(info map[string]interface{}) string {
keys := map[string]string{
"device_batteryLevelCase": "C:",
"device_batteryLevelLeft": "L:",
"device_batteryLevelRight": "R:",
"device_batteryLevel": "",
}

var res string
for key, value := range keys {
if val, ok := info[key]; ok {
res += fmt.Sprintf("%s%s;", value, val)
}
}
return res
}

0 comments on commit b26acce

Please sign in to comment.