ios – 连接两个BLE外设的流程是什么样的?

前端之家收集整理的这篇文章主要介绍了ios – 连接两个BLE外设的流程是什么样的?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_403_0@
我在论坛上一直在阅读很多帖子,我看到了很多与我的案例相关的帖子.但是我仍然没有找到我想要的清晰度.

我想连接两个CBPeripherals并将数据写入它们.根据我的阅读,我认为在连接第二个设备之前,我必须断开当前的外围设备.好吧,假设我要将一个命令写入其中一个外设,然后我想将另一个命令写入另一个外设,我是否必须断开与当前外设的连接?如果我断开连接到另一个,前一个命令是否仍然有效? iOS上最好的做法是什么?

解决方法

我的蓝牙朋友,首先,如果你想发送这两个消息,没有必要断开当前的外设连接另一个.但是许多应用程序将连接设备(CBPeripheral)的数量限制为5-10,因为超过5-10个连接设备可能会自发丢失,我对它有点了解(我只使用4个设备).例如: @H_404_8@[[RKCentralManager sharedManager] scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@NO} onUpdated:^(RKPeripheral *peripheral) { //first of all u should start a scan [[RKCentralManager sharedManager] connectPeripheral: peripheral options:nil onFinished:^(RKPeripheral * connectedperipheral,NSError *error) { //after u can connect to Peripheral immediately [connectedperipheral discoverServices:nil onFinish:^(NSError *error) { // services - a collection of data and associated behaviors for accomplishing a function or feature of a device [connectedperipheral discoverCharacteristics:nil forService: [connectedperipheral.services lastObject] onFinish:^(CBService *service,NSError *error) { //after u should take a characteristic - Represents a service's characteristic CBCharacteristic * characteristic = service.characteristics[0]; //and at last u can write value in characteristic in which you are going to write down something NSData * data = [NSData dataWithHexString: newstring]; CBCharacteristicWriteType type = CBCharacteristicWriteWithoutResponse; [connectedperipheral writeValue:data forCharacteristic:characteristic type:type onFinish:nil]; }]; }]; }]; }];

为蓝牙设备发送消息的近似方案,不是必须对方法进行投资,它们可以在动作上分发.

您不应该担心连接和发送数据
对于多个设备,因为它适用于CBCentralManager,如果你正确使用它.

CBCentralManager对象用于管理已发现或连接的远程外围设备(由CBPeripheral对象表示),包括扫描,发现和连接到广告外围设备.

您可以立即连接某些设备并向其发送消息,一切正常.
如果您有疑问,请尽量回答.

这是一个很好的例子,你可以看到它的工作原理:https://github.com/ruiking/ble

关于设备的最大数量https://stackoverflow.com/a/17282862/4912496

原文链接:https://www.f2er.com/iOS/331452.html

猜你在找的iOS相关文章