Skip to content

iOS CoreBluetooth caches peripheral.name #17

@vladimircherevko

Description

@vladimircherevko

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch react-native-ble-nitro@1.12.0 for the project I'm working on.

iOS CoreBluetooth caches peripheral.name from the first discovery. After the device is configured and starts advertising a new name, peripheral.name still returns the stale cached value. The fresh name is available in advertisementData[CBAdvertisementDataLocalNameKey], but it is only used as a fallback — so it never gets picked up.

Android does not have this issue because its scan API reads the name directly from the current advertisement packet.

Fix:
Swap the priority to prefer the live advertisement data over the cached value.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-ble-nitro/ios/BleNitroBleManager.swift b/node_modules/react-native-ble-nitro/ios/BleNitroBleManager.swift
index 5ade36d..1ae1e21 100644
--- a/node_modules/react-native-ble-nitro/ios/BleNitroBleManager.swift
+++ b/node_modules/react-native-ble-nitro/ios/BleNitroBleManager.swift
@@ -714,7 +714,7 @@ public class BleNitroBleManager: HybridNativeBleNitroSpec {
         rssi: Double
     ) -> BLEDevice {
         let deviceId = peripheral.identifier.uuidString
-        let deviceName = peripheral.name ?? advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? "Unknown"
+        let deviceName = advertisementData[CBAdvertisementDataLocalNameKey] as? String ?? peripheral.name ?? "Unknown"
         
         // Extract service UUIDs
         let serviceUUIDs = (advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID])?.map { $0.uuidString } ?? []

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions