简体   繁体   English

iOS BLE 应用程序中的重新连接过程

[英]Reconnection procedure in iOS BLE application

Consider a scenario where i have a bonded connection established with a peer device from my iPhone 4S running iOS 5.1.考虑一个场景,我与运行 iOS 5.1 的 iPhone 4S 的对等设备建立了绑定连接。

And then there is a disconnection due to some reason (either user initiated or otherwise).然后由于某种原因(用户启动或其他原因)断开连接。

In this case if the app has to auto reconnect to the previously paired device, as far as i know Apple exposes the API retrieve_peripheral which is supposed to give the call back did_retrieve_peripheral with the previously connected or paired peripheral structure filled.在这种情况下,如果应用程序必须自动重新连接到先前配对的设备,据我所知,Apple 公开了 APIretrieve_peripheral,它应该在填充了先前连接或配对的外围结构的情况下给 did_retrieve_peripheral 回调。

And using this peripheral instance i can compare my previously paired uuid and if matches can attempt connection (reconnection).使用这个外围实例,我可以比较我之前配对的 uuid,如果匹配可以尝试连接(重新连接)。 But i am not able to fetch the peripheral since the count is coming as 0 in did retrieve peripheral callback.但是我无法获取外围设备,因为在确实检索外围设备回调时计数为 0。

Am I missing anything?我错过了什么吗?

Also have anybody tried replicating or storing the entire peripheral structure in the app which includes the (services discovered in the peripheral, characteristics etc).也有没有人尝试在应用程序中复制或存储整个外围结构,其中包括(在外围发现的服务、特征等)。 and used this in reconnection.并在重新连接中使用它。

To use the retrievePeripheral API, you need to save the CBPeripheral UUID somewhere (I use CoreData).要使用retrievePeripheral API,您需要将CBPeripheral UUID 保存在某处(我使用CoreData)。 The retrievePeripheral call takes an NSArray of UUIDs as a parameter. retrievePeripheral 调用将 UUID 的 NSArray 作为参数。 Looks like you're not providing the saved UUID to your retrievePeripheral call.看起来您没有向retrievePeripheral 调用提供保存的UUID。 When your didRetrievePeripherals callback is called, you will get an array of CBPeripheral objects.当您的 didRetrievePeripherals 回调被调用时,您将获得一个 CBPeripheral 对象数组。 You can then reconnect to these peripherals.然后您可以重新连接到这些外围设备。 You don't need to scan for these and you also won't need to pair with the peripherals.您不需要扫描这些,也不需要与外围设备配对。

I have the same problem and I think one solution: when connection is lost, masterhave to scan for peripherials in the same way that we scan and connect, and slave have to pass to advertisin state and both have to recognice caractheristics services uuid's and caracteristics for the services again.我有同样的问题,我想一个解决方案:当连接丢失时,master 必须以与我们扫描和连接相同的方式扫描外围设备,slave 必须传递到 advertisin 状态,并且两者都必须识别 caracteristics 服务 uuid 和 caracteristics再次服务。

This is the way I going to try to reconnect.这就是我要尝试重新连接的方式。

Of course I thing we have to refresh on your own way with some more functions当然,我认为我们必须以自己的方式刷新更多功能

You should save the peripheral UUID (peripheral.identifier.UUIDString) when you successfully connect to the device.当您成功连接到设备时,您应该保存外围 UUID (peripheral.identifier.UUIDString)。 I am using NSUserDefaults to save the UUID.我正在使用 NSUserDefaults 来保存 UUID。

When you want to connect to the device use the following method from CBCentralManager retrievePeripheralsWithIdentifiers: and pass as parameter the UUID.当您想连接到设备时,请使用 CBCentralManager 中的以下方法retrievePeripheralsWithIdentifiers:并将 UUID 作为参数传递。 This will return an array of CBPeripherals, find yours and connect to it.这将返回一组 CBPeripherals,找到你的并连接到它。

Don't try to save the CBPeripheral object anywhere.不要试图将 CBPeripheral 对象保存在任何地方。 It is a very bad idea.这是一个非常糟糕的主意。 Just use its UUID.只需使用其 UUID。

If your device is already connected to the phone you could retrieve the CBPeripheral object using the method retrieveConnectedPeripheralsWithServices from CBCentralManager.如果您的设备已经连接到手机,您可以使用 CBCentralManager 中的retrieveConnectedPeripheralsWithServices方法检索CBPeripheral 对象。 You need to pass the services that the peripheral is using as parameters.您需要传递外围设备正在使用的服务作为参数。

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

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