Incorrect BLE Peripheral Name with iOS

MSchmidtbauer picture MSchmidtbauer · Sep 19, 2014 · Viewed 12.4k times · Source

I am writing an iOS app to communicate with a BLE device. The device can change names between connections (not during the BLE connection), but iOS refuses to change the device name.

For example: I can connect to the device when its name is SadName. I disconnect it, shut down the app, etc. and change the device's name to HappyName. But, when I scan for devices iOS still shows the peripheral name as SadName.

If I debug the app and look at:

 (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

the value of peripheral.name is SadName so I don't think that it is something that I am interpreting incorrectly in code. I should mention that when I scan for devices, my code is:

[self.CM scanForPeripheralsWithServices:nil options:0]; // Start scanning 

I am guessing that it is simply because the devices UUID is the same so iOS is pulling it from its cached devices list, but I want to override that.

Thoughts? Sorry, I am new to iOS. Cheers - MSchmidtbauer

Answer

Abrow picture Abrow · Sep 21, 2014

The CoreBluetooth API of iOS SDK does not provide a way to force refresh the peripheral name.

Currently it is not feasible to use peripheral.name in iOS when the device name in the BLEdevice changes.

Apple suggests to scan for a specific device by specifying a list of CBUUID objects (containing one or more service UUIDs) that you pass to scanForPeripheralsWithServices:

NSArray *services = @[[CBUUID UUIDWithString: @"2456e1b9-26e2-8f83-e744-f34f01e9d701"] ]; // change to your service UUID!
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

[self.manager scanForPeripheralsWithServices:services options:dictionary];

This reduces the number of calls of didDiscoverPeripheral. Do not just pass nil to scanForPeripheralsWithServices. It also allows your app to scan for a peripheral when in background state.

If you are looking for a way to broadcast dynamic information that's available before a connection is established, you can use the Advertise or Scan Response Data. The peripheral can be configured to broadcast the entries called Local Name and Manufacturer Specific Data. This data is availabe in the didDiscoverPeripheral:

- (void)centralManager:         (CBCentralManager *)central
 didDiscoverPeripheral:  (CBPeripheral *)peripheral
     advertisementData:      (NSDictionary *)advertisementData
                  RSSI:         (NSNumber *)RSSI {
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
NSData *manufacturerData = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
NSLog(@"Local: name: %@", localName); 
NSLog(@"Manufact. Data: %@", [manufacturerData description]);
}

Local Name is an NSString, so write only printable characters on the BLE device in this filed. Manufacturer Data is an NSData, this can contain any byte value, so you can even have binary data here.

Depending on the BLE device you use, the length of Local Name and Manufacturer Specific Data is limited.

On my BLE device,I can send the 128 Bit service UUID and a 8 char Local Name with the Advertise Data. The Manufacturer Specific Data goes into the Scan Response Data and can be 29 bytes long.

Good thing about using the Adv./Scan Response Data is, it can change on this BLE device without a power cycle.

Suggestion:

  1. Use the service UUID to filter when scanning (UUID must be part of advertising data! I omitted it in the above description)
  2. Use the Advertise/Scan Response Data for further filtering
  3. Forget about peripheral.name as long as there is no deterministic refresh available