我正在尝试连接到使用 BlueFruit BLE spi 模块的 Arduino 项目。尝试使用我的 iOS 应用程序连接时遇到问题。找到设备后,我尝试连接它,但状态卡在“正在连接”状态=1。这会阻止我搜索服务等,因为未达到“连接”状态这是一个代码片段......
//check state of the bluetooth on phone
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == .poweredOff{
//TODO: ADD SAVE DATA TO REALM BEFORE DISSMISSING
errorView.isHidden = false
}
if central.state == .poweredOn{
errorView.isHidden = true
//scan for peripherals with the service i created
central.scanForPeripherals(withServices: nil, options: nil)
}
}
//devices found(should only be ours because we will create Unique serviceID)
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
// get advertisement data and check to make sure the name is matching. set it as the peripheral then make connection
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print("NEXT PERIPHERAL NAME: \(peripheralName)")
print("NEXT PERIPHERAL UUID: \(peripheral.identifier.uuidString)")
if peripheralName == nameID{
manager.stopScan()
self.peripheralHalo = peripheral
peripheralHalo!.delegate = self
manager.connect(peripheral, options: nil)
while(peripheralHalo?.state.rawValue == 1)
{
if(manager.retrieveConnectedPeripherals(withServices: [serviceID]).count > 0 ){
print("\(manager.retrieveConnectedPeripherals(withServices: [serviceID]))")
}
}
}
print("Connected!!")
}
当我调用 manager.connect(peripheral, options: nil) 时,外围设备会尝试连接。我添加了以下 while 循环进行测试,并始终将状态显示为“正在连接”。我已经尝试过 LightBlue iOS 应用程序,我可以正确连接并接收特征值更改的通知,因此 Arduino 固件应该是好的。请帮助!!!