蓝牙外围设备卡在 "Connecting" 状态 iOS
Bluetooth Peripheral Stuck in "Connecting" State On iOS
我正在尝试连接到使用 BlueFruit BLE spi 模块的 Arduino 项目。我在尝试使用我的 iOS 应用进行连接时遇到问题。找到设备后,我尝试连接到它,但状态卡在 'connecting' state=1。这会阻止我搜索服务等,因为未达到 'connected' 状态
这是代码片段...
//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 循环进行测试,并始终将状态显示为 "connecting"。我已经尝试了 LightBlue iOS 应用程序,我可以正确连接并接收特征值更改的通知,因此 Arduino 固件应该都是 good.PLEASE HELP!!!
你不想要那个 while
循环;这只会阻塞 Core Bluetooth 委托线程。发出 connect
后,您将在外围设备上收到对 didConnect
CBCentralManagerDelegate
method. Once the peripheral is connected you need to call 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)
}
此外,如果您要存储一些东西来标识您要连接的外围设备,我建议您使用标识符而不是名称,因为名称可以更改。
我正在尝试连接到使用 BlueFruit BLE spi 模块的 Arduino 项目。我在尝试使用我的 iOS 应用进行连接时遇到问题。找到设备后,我尝试连接到它,但状态卡在 'connecting' state=1。这会阻止我搜索服务等,因为未达到 'connected' 状态 这是代码片段...
//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 循环进行测试,并始终将状态显示为 "connecting"。我已经尝试了 LightBlue iOS 应用程序,我可以正确连接并接收特征值更改的通知,因此 Arduino 固件应该都是 good.PLEASE HELP!!!
你不想要那个 while
循环;这只会阻塞 Core Bluetooth 委托线程。发出 connect
后,您将在外围设备上收到对 didConnect
CBCentralManagerDelegate
method. Once the peripheral is connected you need to call 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)
}
此外,如果您要存储一些东西来标识您要连接的外围设备,我建议您使用标识符而不是名称,因为名称可以更改。