fix: fixed access to the IOBluetoothDevice when reading device from the CBCentralManager (#628)

This commit is contained in:
Serhiy Mytrovtsiy
2021-09-18 17:09:32 +02:00
parent a8f48fef3d
commit f7341e6702
2 changed files with 15 additions and 14 deletions

View File

@@ -23,7 +23,7 @@ public struct BLEDevice {
let conn: BLEConnType
let address: String
let name: String
var name: String
var uuid: UUID?
var RSSI: Int? = nil
@@ -79,7 +79,7 @@ public class Bluetooth: Module {
return
}
let active = value.filter{ $0.isPaired && ($0.isConnected || !$0.batteryLevel.isEmpty) }
let active = value.filter{ $0.isPaired || ($0.isConnected && !$0.batteryLevel.isEmpty) }
DispatchQueue.main.async(execute: {
self.popupView.batteryCallback(active)
self.settingsView.setList(active)

View File

@@ -55,6 +55,18 @@ internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBP
public override func read() {
self.IODevices()
self.cacheDevices()
IOBluetoothDevice.pairedDevices()?.forEach({ d in
guard let device = d as? IOBluetoothDevice, device.isPaired() || device.isConnected(),
let idx = self.devices.firstIndex(where: { $0.address == device.addressString }) else {
return
}
self.devices[idx].name = device.nameOrAddress
self.devices[idx].isPaired = device.isPaired()
self.devices[idx].isConnected = device.isConnected()
})
self.callback(self.devices)
}
@@ -185,20 +197,11 @@ internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBP
return
}
guard let device: IOBluetoothDevice = IOBluetoothDevice.pairedDevices().first(where: { d in
guard let device = d as? IOBluetoothDevice, device.isPaired() || device.isConnected() else {
return false
}
return device.addressString == address
}) as? IOBluetoothDevice else {
return
}
guard let idx = self.devices.firstIndex(where: { $0.address == address && $0.conn == .ble }) else {
self.devices.append(BLEDevice(
conn: .ble,
address: address,
name: peripheral.name ?? device.nameOrAddress ?? "Unknown",
name: peripheral.name ?? "Unknown",
uuid: peripheral.identifier,
RSSI: Int(truncating: RSSI),
peripheral: peripheral
@@ -207,8 +210,6 @@ internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBP
}
self.devices[idx].RSSI = Int(truncating: RSSI)
self.devices[idx].isConnected = device.isConnected()
self.devices[idx].isPaired = device.isPaired()
if peripheral.state == .disconnected {
central.connect(peripheral, options: nil)