How to find a specific BLE 4.0 peripheral from two devices with the same UUID service

For BLE 4.0, it provides an API for detecting peripherals with an array of service UUIDs.

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

If you need to assign an identifier to a specific device, how to do it?

(I think my question needs some kind of basic iOS iOS bluetooth context.)

+1
source share
2 answers

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

Essentially, if you know the UUID of the device you want to connect to (this is the identifier property of the CBPeripheral object), you can use the retrievePeripheralsWithIdentifiers: method for CBCentralManager to get CBPeripheral , and then try to establish 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]; } 

This may not work, and you may need to scan your peripheral device as an identifier for some peripheral devices (in particular, iOS devices), periodically changing their UUIDs.

Programming Notes -

Note. The peripheral device may not be available for connection for several reasons. For example, a device cannot be near Central. In addition, some low-power Bluetooth devices use a random device address that changes periodically. Therefore, even if a device is nearby, the device address may have changed since the last time it was detected by the system, in which case the CBPeripheral object you are trying to connect to does not match the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must reopen it using scanForPeripheralsWithServices: options: method.

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

+6
source

Tell me if I'm wrong. You have 2 devices that play a central role that want to connect to the same BLE peripheral.

What is important is the UUID of the services in the peripheral part of the BLE. From your devices in a central role, you must look for peripherals with the required UUID service. That's all.

To develop with iOS, follow the Bluetooth Core Programming Guide . You have a good tutorial.

As an example of Apple documentation:

 [myCentralManager scanForPeripheralsWithServices:nil options:nil]; 

In this line of code, you can add an array of UUID objects ( CBUUID ) instead of nil for scanForPeripheralsWithServices:

0
source

All Articles