如何从具有相同服务UUID的两个设备中查找特定的BLE 4.0外设 [英] How to find the specific BLE 4.0 peripheral from two devices with same service UUID

查看:121
本文介绍了如何从具有相同服务UUID的两个设备中查找特定的BLE 4.0外设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于BLE 4.0,它提供了API以发现具有服务UUID阵列的外围设备。

For BLE 4.0, it provides API to discover peripherals with array of service UUID.

我只想找到特定的设备。如何实现这一目标?

I just want to find the specific one. How to achieve this ?

如果需要将标识符分配给特定设备,该怎么做?

If need assign the identifier to the specific device, how to do it ?

(我认为我的问题需要iOS核心蓝牙的某些上下文。)

(I think my question need some context of core bluetooth of iOS. )

推荐答案

重新连接到已知外围设备的过程在

The process for reconnecting to known peripherals is described in the Core Bluetooth Programming Guide.

基本上,如果您知道要重新连接的设备的UUID(即标识符 CBPeripheral 对象的code>属性),则可以使用<$ c的 retrievePeripheralsWithIdentifiers:方法$ c> CBCentralManager 获取 CBPeripheral 对象,然后尝试连接-

Essentially if you know the UUID of the device you want to reconnect to (which is the identifier property of the CBPeripheral object) then you can use the retrievePeripheralsWithIdentifiers: method of the CBCentralManager to obtain the CBPeripheral object and then attempt a connection -

NSUUID *pid=[[NSUUID alloc]initWithUUIDString:@"45D956BB-75E4-6CEB-C06C-D531B00174E4" ];
NSArray *peripherals=[self.centralManager retrievePeripheralsWithIdentifiers:@[pid]];
if ([peripherals count]>0)
{
      CBPeripheral *peripheral=[peripherals objectAtIndex:0];
      peripheral.delegate=self;
      [self.connectingPeripherals addObject:peripheral];

      NSLog(@"Found peripheral %@ with name %@",peripheral.identifier,peripheral.name);
      [self.centralManager connectPeripheral:peripheral options:nil];
 }

这可能不起作用,您可能需要扫描外围设备某些外围设备(尤其是iOS设备)的标识符会定期更改其UUID。

This may not work, and you may have to scan for your peripheral as the identifier of some peripherals (iOS devices in particular) periodically change their UUID.

编程指南注意事项-


注意:出于某些原因,可能由于
a无法连接外围设备。例如,该设备可能不在中央
附近。此外,某些低功耗蓝牙设备使用的
随机设备地址会定期更改。因此,即使设备附近有
,该设备的地址也可能自上次被系统发现
以来就已更改,在这种情况下,您要尝试使用
CBPeripheral对象连接到不对应实际的外围设备
。如果由于地址更改而无法重新连接到
外设,则必须使用scanForPeripheralsWithServices:options:方法重新发现

Note: A peripheral device may not be available to be connected to for a few reasons. For instance, the device may not be in the vicinity of the central. In addition, some Bluetooth low energy devices use a random device address that changes periodically. Therefore, even if the device is nearby, the address of the device may have changed since the last time it was discovered by the system, in which case the CBPeripheral object you are trying to connect to doesn’t correspond to the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must rediscover it using the scanForPeripheralsWithServices:options: method.

第一次遇到外围设备时,您还必须扫描。

You will also have to scan the first time you encounter a peripheral.

这篇关于如何从具有相同服务UUID的两个设备中查找特定的BLE 4.0外设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆