1. Background

  1. When the device has been successfully connected in the App
  2. Modifying the Device Name
  3. The device name scanned by App is still the same as before
  4. The way to get the name in your App code is (perpheral. Name)

IOS development exchange technology group: 563513413, no matter you are big bull or small white are welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together!

2. Problem analysis

When the APP is connected to another Bluetooth device as the center.

After the first successful connection, the peripheral cache is recorded in the iOS system.

In the next search, when the Bluetooth device is searched, the peripheral. Name is directly printed and the bluetooth name in the cache is obtained.

If the name of the Bluetooth device is updated during this period, the parameter (Peripheral. Name) will not change, so it is necessary to obtain the name of the device in another way. There is a field kCBAdvDataLocalName in the broadcast packet, which can obtain the name of the current device in real time.

Problem solving

OC and Swift solutions are given below:

OC

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
        NSString *localName = [advertisementData objectForKey:@"kCBAdvDataLocalName"];
} 
Copy the code

Swift

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        let localName = advertisementData["kCBAdvDataLocalName"]
}
Copy the code