蓝牙BLE设备一旦连接就会断开连接?

我一直在使用连接到蓝牙BLE设备的Swift编写应用程序。 出于某种原因,应用程序并不总是连接到设备。 在这种情况下,它将连接,但直接断开连接。 这只会发生10次它连接,但绝对干扰应用程序的可靠性。 我正在使用CoreBluetooth连接到BLE设备。 再次尝试连接时,通常会重新连接,而其他与此设备通信的应用程序每次都能正常工作,所以我相信这不是外围设备的问题。

我只想知道有没有人有类似的问题,或者有什么特别的原因可能会发生?

编辑:这是我的表的willSelectRow的代码。 这是我得到外设连接的地方。

  func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { centralManager.stopScan() connectingPeripheral.append(discoveredDeviceArrayInfo[indexPath.row]) connectPeripheralNow(connectingPeripheral[0]) return indexPath } 

这是我得到它连接,在这一点上,我select设置设备的CBPeripheral细节连接到的行。

connectPeripheralNow看起来像这样:

  func connectPeripheralNow(peripheral: CBPeripheral!){ self.centralManager.connectPeripheral(peripheral, options: nil) } 

didConnectPeripheral和didDiscoverServices看起来像这样

  func centralManager(central: CBCentralManager,didConnectPeripheral peripheral: CBPeripheral) { peripheral.delegate = self peripheral.discoverServices([CBUUID(string: "FFE5")]) print("SUCCESS: Connected to " + peripheral.name!) } func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) { if let servicePeripherals = peripheral.services as [CBService]! { for servicePeripheral in servicePeripherals { print("INFORMATION: Service discovered " + String(stringInterpolationSegment: servicePeripheral.UUID)) peripheral.discoverCharacteristics(nil, forService: servicePeripheral) } } } 

只是为了获得信息,我确实得到了一个“成功:连接到xxx”的消息,显示它正在连接。 如果你需要更多的代码,让我知道!

你提到的更多的是一个预期的行为。 BTLE被devise为消耗非常less量的能量,因此尽快地断开连接。 如果您不需要永久连接,请按照发现 – >连接 – >读/写 – >设置计时器 – >计时器触发连接。

如果您需要永久连接,您应该订阅传输像心率应用程序这样的实时数据的特性。 你需要实现setNotifyValue:forCharacteristic:方法。 阅读Apple文档了解更多详情。