如何从具有相同服务UUID的两个设备中查找特定的BLE

如何从具有相同服务UUID的两个设备中查找特定的BLE

本文介绍了如何从具有相同服务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.

编程指南注意事项-

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

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

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