Skip to content

Commit

Permalink
fix: fixed BLE battery level update (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
exelban committed Jul 20, 2021
1 parent 4197e2f commit 284d394
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
7 changes: 0 additions & 7 deletions Modules/Bluetooth/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ import Foundation
import Kit
import CoreBluetooth

public enum BLEType: String {
case iPhone
case airPods
case unknown
}

public struct BLEDevice {
let uuid: UUID
let name: String
let type: BLEType

var RSSI: Int?
var batteryLevel: [KeyValue_t]
Expand Down
22 changes: 13 additions & 9 deletions Modules/Bluetooth/readers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal class DevicesReader: Reader<[BLEDevice]> {

class BluetoothDelegate: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
private var manager: CBCentralManager!
private let cache = UserDefaults(suiteName: "/Library/Preferences/com.apple.Bluetooth")

private var peripherals: [CBPeripheral] = []
public var devices: [BLEDevice] = []
Expand All @@ -51,23 +50,29 @@ class BluetoothDelegate: NSObject, CBCentralManagerDelegate, CBPeripheralDelegat
}

public func read() {
guard let dict = UserDefaults(suiteName: "/Library/Preferences/com.apple.Bluetooth") else {
return
}

IOBluetoothDevice.pairedDevices().forEach { (d) in
guard let device = d as? IOBluetoothDevice,
let cache = self.findInCache(address: device.addressString) else {
guard let device = d as? IOBluetoothDevice, device.isPaired() || device.isConnected(),
let cache = self.findInCache(dict, address: device.addressString) else {
return
}

let rssi = device.rawRSSI() == 127 ? nil : Int(device.rawRSSI())

if let idx = self.devices.firstIndex(where: { $0.uuid == cache.uuid }) {
self.devices[idx].RSSI = rssi
if cache.batteryLevel.isEmpty {
self.devices[idx].batteryLevel = cache.batteryLevel
}
self.devices[idx].isConnected = device.isConnected()
self.devices[idx].isPaired = device.isPaired()
self.devices[idx].RSSI = rssi
} else {
self.devices.append(BLEDevice(
uuid: cache.uuid,
name: device.nameOrAddress,
type: .unknown,
RSSI: rssi,
batteryLevel: cache.batteryLevel,
isConnected: device.isConnected(),
Expand All @@ -78,10 +83,9 @@ class BluetoothDelegate: NSObject, CBCentralManagerDelegate, CBPeripheralDelegat
}
}

private func findInCache(address: String) -> (uuid: UUID, batteryLevel: [KeyValue_t])? {
guard let plist = self.cache,
let deviceCache = plist.object(forKey: "DeviceCache") as? [String: [String: Any]],
let coreCache = plist.object(forKey: "CoreBluetoothCache") as? [String: [String: Any]] else {
private func findInCache(_ cache: UserDefaults, address: String) -> (uuid: UUID, batteryLevel: [KeyValue_t])? {
guard let deviceCache = cache.object(forKey: "DeviceCache") as? [String: [String: Any]],
let coreCache = cache.object(forKey: "CoreBluetoothCache") as? [String: [String: Any]] else {
return nil
}

Expand Down

0 comments on commit 284d394

Please sign in to comment.