简体   繁体   English

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

[英]Bluetooth BLE Device disconnects as soon as connected?

I've been writing an app using Swift that connects to a bluetooth BLE device. 我一直在用Swift编写一个连接到蓝牙BLE设备的应用程序。 For some reason, the app doesn't always connect to the device. 由于某些原因,该应用程序并不总是连接到设备。 In this case it will connect but gets disconnected straight away. 在这种情况下,它将连接但立即断开连接。 This only happens maybe 1 in 10 times it connects, but definitely interferes with the reliability of the app. 这种情况仅发生在其连接的十分之一中,但肯定会干扰应用程序的可靠性。 I'm using CoreBluetooth to connect to the BLE device. 我正在使用CoreBluetooth连接到BLE设备。 Attempting connection again usually always gets it to reconnect, and other apps that communicate with this device works correctly every time, so I'm confident that it is not a problem with the peripheral. 通常,再次尝试连接总是可以使其重新连接,并且与此设备进行通信的其他应用每次都可以正常运行,因此我相信外设不是问题。

I'd just like to know if there is anyone out there who has had a similar issue or if there is a particular reason why this may be happening? 我只想知道外面是否有人遇到过类似的问题,或者是否有特定原因导致这种情况发生?

EDIT: Here's the code for the willSelectRow of my table. 编辑:这是我表的willSelectRow的代码。 This is where I get the peripheral to connect. 这是我连接外围设备的地方。

    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

        centralManager.stopScan()
        connectingPeripheral.append(discoveredDeviceArrayInfo[indexPath.row])
        connectPeripheralNow(connectingPeripheral[0])

        return indexPath
}

This is where I get it to connect, at this point I select the row which sets the CBPeripheral details of the device to connect to. 这是我进行连接的地方,这时,我选择一行来设置要连接的设备的CBPeripheral详细信息。

connectPeripheralNow looks like this: connectPeripheralNow看起来像这样:

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

didConnectPeripheral and didDiscoverServices looks like this 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)

        }

    }
}

Just for the info, I do get a 'SUCCESS: Connected to xxx' message appear which shows that it is connecting. 仅出于信息方面,我确实收到一条“成功:已连接至xxx”消息,表明它正在连接。 If you need more code, let me know! 如果您需要更多代码,请告诉我!

What you are mentioning is more of an expected behaviour. 您所说的更多是预期的行为。 BTLE is designed to consume very little amount of energy so it drops the connection as soon as possible. BTLE旨在消耗很少的能量,因此它会尽快断开连接。 If you do not need a permanent connection then follow Discover --> Connect --> Read/write --> Setup timer --> When timer fires Connect. 如果不需要永久连接,请按照“ 发现”->“连接”->“读/写”->“设置计时器”->“计时器触发时间”进行操作。

If you need a permanent connection, you should subscribe for characteristic which transmits real time data like heart rate application. 如果需要永久连接,则应订阅可传输实时数据的特性,例如心率应用程序。 You would need to implement setNotifyValue:forCharacteristic: method. 您将需要实现setNotifyValue:forCharacteristic:方法。 Read Apple Documentation for more details. 请阅读Apple文档以获取更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM