Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect BLE Peripheral Name with iOS

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

like image 833
MSchmidtbauer Avatar asked Sep 19 '14 16:09

MSchmidtbauer


People also ask

What is BLE peripherals?

Device modes When using BLE, an Android device can act as a peripheral device, a central device, or both. Peripheral mode lets devices send advertisement packets. Central mode lets devices scan for advertisements.

What is BLE iOS?

Introduction: One of the main features of the Bluetooth 4 specification is Bluetooth Low Energy (BLE). Also called Bluetooth smart, this technology allows peripherals to communicate by consuming much less energy than regular Bluetooth.

What is central and peripheral in BLE?

In the BLE world, the central/peripheral difference is very easy to define and recognize: Central - the BLE device which initiates an outgoing connection request to an advertising peripheral device. Peripheral - the BLE device which accepts an incoming connection request after advertising.


3 Answers

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
like image 196
Abrow Avatar answered Oct 01 '22 08:10

Abrow


Your guessing is correct.
It is because of the core-blutetooth cache.

Generally changing name / services / characteristics on BLE devices are "not supported". All these parameters are getting cached.

There are two ways of solving this:

  • restart bluetooth adapter, so bluetooth cache gets cleared (I'm afraid there is no way to do this programatically, but i might be wrong)
  • your device BLE implements the GATT Service Changed characteristic: read about this here: core_v4.1.zip
    Vol 3, Part G, 2.5.2, and Vol 3, Part G, 7.1.

Alternatively check the advertisement data of your BLE device. It might have a name property which should get refreshed every time the BLE device is advertising data (advertising data doesn't get cachced).

like image 25
benka Avatar answered Oct 05 '22 08:10

benka


The CBPeripheralDelegate protocol contains a method...

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);

... which is made for this purpose.

like image 22
Nikolai Ruhe Avatar answered Oct 02 '22 08:10

Nikolai Ruhe