问题描述
我已经写使用SWIFT连接到蓝牙设备BLE的应用程序。出于某种原因,应用程序并不总是连接到设备。在这种情况下,它会连接,但断开连接,立竿见影。这只发生也许1 10倍它连接,但绝对与应用程序的可靠性干扰。
我使用 CoreBluetooth
连接到BLE装置。再次尝试连接通常总是得到它重新连接,并与该设备进行通信的其他应用程序每次正常工作,所以我相信,这是不是与外设的问题。
I've been writing an app using Swift that connects to a bluetooth BLE device. 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. 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?
编辑:这里是code代表我的表的willSelectRow。这是我得到的外设连接。
Here's the code for the willSelectRow of my table. 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
}
这是我得到它连接起来,在这一点上我选择这台设备的CBPeripheral详细信息以连接到该行。
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.
connectPeripheralNow看起来是这样的:
connectPeripheralNow looks like this:
func connectPeripheralNow(peripheral: CBPeripheral!){
self.centralManager.connectPeripheral(peripheral, options: nil)
}
didConnectPeripheral和didDiscoverServices看起来像这样
didConnectPeripheral and didDiscoverServices looks like this
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的消息出现,显示它连接。如果您需要更多code,让我知道!
Just for the info, I do get a 'SUCCESS: Connected to xxx' message appear which shows that it is connecting. If you need more code, let me know!
推荐答案
你所提到更多的是一种预期行为。 BTLE被设计成它立即丢弃连接尽可能地消耗能量很少量的。如果你不需要一个永久连接然后按照查看 - >连接 - >读/写 - >设置定时器 - >当定时器触发连接
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. If you do not need a permanent connection then follow Discover --> Connect --> Read/write --> Setup timer --> When timer fires Connect.
如果你需要一个永久连接,您应该认购其发送实时数据,如心脏速率应用的特点。你会需要实现 setNotifyValue:forCharacteristic:
方法。读<一个href=\"https://developer.apple.com/library/$p$prelease/mac/documentation/CoreBluetooth/Reference/CBPeripheral_Class/index.html#//apple_ref/occ/instm/CBPeripheral/setNotifyValue:forCharacteristic:\"相对=nofollow>苹果文档了解更多详情。
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. Read Apple Documentation for more details.
这篇关于蓝牙BLE设备断开连接,只要连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!