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