【问题标题】:Bluetooth Peripheral Stuck in "Connecting" State On iOS蓝牙外设卡在 iOS 上的“正在连接”状态
【发布时间】:2016-11-28 22:09:42
【问题描述】:

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

【问题讨论】:

    标签: ios bluetooth arduino adafruit


    【解决方案1】:

    你不想要那个while 循环;这只会阻塞核心蓝牙委托线程。发出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)
    }
    

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

    【讨论】:

    • 谢谢保罗。我在没有 while 循环的情况下尝试过,并且每次都没有调用 'didconnect' 和 'didfailconnect'。
    • 感谢 Paul 做到了!我以为我已经尝试了一切,花了几个小时盯着它。我从您发布的代码中切换的唯一内容是我对外围设备的硬引用,并且必须在 didDiscover 中设置委托。否则它会一直断开连接
    • 是的,我不确定。我只是在写一些代码来测试。
    猜你喜欢
    • 2016-09-13
    • 2013-08-07
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2015-11-02
    相关资源
    最近更新 更多