mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
fix: fixed access to the IOBluetoothDevice when reading device from the CBCentralManager (#628)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user