一、無法掃描到藍牙,但其他APP如LightBlue能搜到
1、方法
func central.scanForPeripherals(withServices: nil, options: nil)
要寫在
func centralManagerDidUpdateState(_ central: CBCentralManager)
中的
case .poweredOn:
下,不能寫在其他地方。
2、掃描到設備的代理方法改變了,swift2.0的代理方法是
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber)
現在swift3.0的代理方法是
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
如果不把方法改過來,就始終不能獲取到掃描到設備的代理。
二、能掃描到設備,無法連接設備
可以掃描到藍牙設備,能夠走上面的didDiscover代理,但是當我連接到指定的藍牙設備的時候central.connect(peripheral, options: nil),卻始終無法連接,藍牙連接成功和失敗的接口都沒有回調。
解決方法:
peripherals.append(peripheral)
定義一個數組,把CBPeripheral存儲到數組中才可以掃描到此設備。