蓝牙外围设备卡在"连接"状态在iOS上

时间:2016-11-28 22:09:42

标签: ios bluetooth arduino adafruit

我正在尝试连接到使用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固件应该都很好。请帮助!!!

1 个答案:

答案 0 :(得分:1)

你不希望while循环;这只会阻止Core Bluetooth委托线程。发出connect后,您将收到didConnect CBCentralManagerDelegate方法的来电。连接外设后,需要在外设上调用discoverServices,这将回调peripheral:didDiscoverServices:外设委托方法。然后,您可以以类似的方式发现特征。

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 {
            self.peripheralHalo = peripheral
            central.stopScan()
            central.connect(peripheral, options: nil)
        }
    }
}

func centralManager(_ central: CBCentralManager, 
              didConnect peripheral: CBPeripheral) {
    print("Connected!!")
    peripheralHalo!.delegate = self
    peripheral.discoverServices([serviceID)
}

此外,如果您要存储标识要连接到哪个外围设备的内容,我建议您使用标识符而不是名称,因为名称可以更改。