0
我想连接到我使用BlueFruit BLE spi模块的Arduino项目。尝试使用我的iOS应用进行连接时遇到问题。在找到设备后,我试着连接它,但状态卡在'连接'状态= 1。这阻止我搜索服务,这样,因为“已连接”状态没有达到 下面是一个代码剪断......蓝牙外设卡在iOS的“连接”状态
//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(外设,选择:无),该外设尝试连接。我添加以下while循环进行测试,并始终将状态显示为“连接”。我已经尝试了LightBlue iOS应用程序,并且我可以正确连接并接收特性值更改的通知,因此Arduino固件应该都很好。请帮助!
谢谢保罗。我已经尝试过没有while循环,既没有'didconnect',也没有'didfailconnect'每一个被调用。 – StoNeD510
感谢保罗,做到了!我以为我曾尝试过所有的事情,花上几个小时盯着它看。我从你发布的代码切换的唯一东西是我对外设的强烈引用,并且设置委托必须在didDiscover中进行。它不断下降连接,否则 – StoNeD510
是的,我不知道这一点。我只是写一些代码来测试。 – Paulw11