Reputation: 13
I use Core Bluetooth Framework to discover bluetooth devices for macos APP, code as below, the variable discoveredPeripherals
and print
statement in centralManager
callback, has no connected devices that show on my Macbook bluetooth settings, like 'Devialet Gemini II' on the image. The centralManager
callback is normal, the print
has print many other devices but 'Devialet Gemini II'. And I'm sure its' a BLE device.
!!! When I unpaired and disconnected the "Devialet Gemini II", can't discover the devices neither, but macos system bluetooth settings can.
system panel --(connected status img):
!!! When I unpaired and disconnected the "Devialet Gemini II", can't discover the devices neither, but macos system bluetooth settings can.
system panel--(unpaired and disconnected status img:)
print log: connected or disconnected or unpaired, always can't discover the "Devialet Gemini II" device.
class BluetoothManager: NSObject, CBCentralManagerDelegate, ObservableObject {
@Published var isBluetoothEnabled = false
@Published var discoveredPeripherals = [CBPeripheral]()
@Published var connectedPeripherals = [CBPeripheral]()
@Published var pairedPeripherals = [CBPeripheral]()
private var centralManager: CBCentralManager!
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func connect(_ central: CBCentralManager, _ peripheral: CBPeripheral) {
central.connect(peripheral)
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOn {
isBluetoothEnabled = true
centralManager.scanForPeripherals(withServices: nil, options: nil)
} else {
isBluetoothEnabled = false
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// there is no connected devices(named "Devialet Gemini II") as image show !!!!!!!!!
print("discover: \(peripheral.name ?? "Unknown")-\(peripheral.state)")
if !discoveredPeripherals.contains(peripheral) {
discoveredPeripherals.append(peripheral)
}
if !connectedPeripherals.contains(peripheral) && peripheral.state == .connected {
connectedPeripherals.append(peripheral)
}
}
}
Upvotes: 0
Views: 50
Reputation: 13
Problem solved, the name of device maybe(is) assigned after connected, so I shouldn't lookup for name.
Upvotes: 0