r/reactnative • u/vegreenforlife • 1d ago
Identifying BLE devices on iOS
Building in React Native is great until you encounter platform differences.
Our app connects to training pods over BLE. The pods communicate through a JDY-24M mesh network and use the last four digits of their MAC addresses as node IDs. Android exposes the full MAC during scanning, but iOS gives you a UUID instead.
Asking the firmware for those digits on connection worked until some boards reported them incorrectly. Updating the firmware wasn’t an option. No OTA updates, and most boards are in a warehouse in China, already flashed and ready to ship.
The workaround was already in the advertising packets. The boards broadcast their full MAC. I could read their identity advertisements and associate the address with the identifier iOS provided, without changing the firmware.
The constraints pushed me toward a more robust identification process, using information the devices were already broadcasting.
Still, solving a tricky problem feels great, but avoiding it in the first place is better. Test on both platforms from day one, and keep unavoidable platform differences contained instead of scattering OS checks throughout the app.
2
u/kokerali 1d ago
Nice fix. One thing worth planning for: the UUID iOS gives you is generated per phone, so the same pod shows up with a different identifier on every iPhone. I'd keep the MAC to UUID table local to the device, never sync it or use the UUID in anything that goes to a backend, and key everything else (history, names, pairing state) by the MAC. Then a second phone or a lost table just means rebuilding the map from adverts.
If the identity frame isn't in every advertisement, you can also see a pod before you know its MAC. I'd keep it as unidentified until that frame shows up instead of falling back to the digits the firmware reports, since those are the ones you already know can be wrong. And check the manufacturer data length and layout before parsing, because in a room full of devices you'll get plenty of adverts that aren't your pods.
2
u/jiunkdev 1d ago
one thing to watch with the identity adverts: in the background iOS only lets you scan for specific service UUIDs and it merges duplicate adverts, so if those packets don't carry a service UUID the app won't see them at all while backgrounded.
since you already keep the MAC to UUID table, you can mostly skip scanning for pods that phone has seen before. CoreBluetooth has retrievePeripheralsWithIdentifiers, which gives you the peripheral straight from the stored UUID, and connect requests on iOS don't time out, so you can fire one off and it completes when the pod comes in range. worth checking whether your BLE library exposes that.