I am working on a macOS app for monitoring battery levels of BlueTooth devices. All of the Apple devices I can get via ioReg and all of the BTLe devices I can use 180F and 2A19, but the Powerbeats Pro has no ioReg I could find and although CoreBluetooth sees the devices, when I query for the peripheral’s services I get back an empty response.
As Apple displays the battery level information in both the Bluetooth app in System Prefs, as well as the accompanying menu bar app, and on iOS devices, there must be a way to obtain this information. All I want to do is retrieve and monitor it so I can display the current battery level.
Here’s an overview of the CoreBluetooth portion of my App Delegate. I realize this is not my complete App Delegate but this is the complete CoreBluetooth portion I can post.
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
} else {
print("Bluetooth is not available.")
}
if let centralManager {
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
centralManager.stopScan()
}
}
}
func centralManager(
_ central: CBCentralManager,
didDiscover peripheral: CBPeripheral,
advertisementData: [String : Any],
rssi RSSI: NSNumber
) {
if !self.btDevicesBTLe.contains(where: {
$0.deviceUUID == peripheral.identifier.uuidString
}) {
if let deviceName = peripheral.name {
let device = BTDevice(
deviceUUID: peripheral.identifier.uuidString,
deviceConnected: false,
deviceMACAddress: nil,
deviceName: deviceName,
hasBattery: false,
batteryLow: false,
batteryPercent: ""
)
self.btDevicesBTLe.append(device)
}
discoveredPeripherals[peripheral.identifier] = peripheral
}
if let centralManager {
centralManager.connect(peripheral, options: nil)
}
}
func centralManager(
_ central: CBCentralManager,
didConnect peripheral: CBPeripheral
) {
if let index = btDevicesBTLe.firstIndex(where: {
$0.deviceName == peripheral.name
}) {
btDevicesBTLe[index].deviceConnected = true
}
peripheral.delegate = self
peripheral.discoverServices(nil)
}
func peripheral(
_ peripheral: CBPeripheral,
didDiscoverServices error: Error?
) {
if let services = peripheral.services {
for service in services {
if service.uuid == CBUUID(string: "180F") {
peripheral.discoverCharacteristics(nil, for: service)
if let deviceIndex = self.btDevicesBTLe.firstIndex(where: {
$0.deviceName == peripheral.name
}) {
self.btDevicesBTLe[deviceIndex].hasBattery = true
}
} else {
self.btDevicesBTLe.removeAll {
$0.deviceName == peripheral.name
}
}
}
}
}
func peripheral(
_ peripheral: CBPeripheral,
didDiscoverCharacteristicsFor service: CBService,
error: Error?
) {
if let characteristics = service.characteristics {
for characteristic in characteristics {
if characteristic.uuid == CBUUID(string: "2A19") {
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
}
}
}
}
func peripheral(
_ peripheral: CBPeripheral,
didUpdateValueFor characteristic: CBCharacteristic,
error: Error?
) {
if characteristic.uuid == CBUUID(string: "2A19") {
if let value = characteristic.value {
let valueUint8 = [UInt8](value)
let batteryLevel: Int32 = Int32(bitPattern: UInt32(valueUint8[0]))
if let index = self.btDevicesBTLe.firstIndex(where: {
$0.deviceName == peripheral.name
}) {
btDevicesBTLe[index].batteryPercent = "(batteryLevel)"
}
}
}
}
func centralManager(
_ central: CBCentralManager,
didDisconnectPeripheral peripheral: CBPeripheral,
error: Error?
) {
if central.state == .poweredOn {
central.scanForPeripherals(withServices: nil, options: nil)
} else {
print("Bluetooth is not available.")
}
if let centralManager {
DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
centralManager.stopScan()
}
}
}