0

I have simulated a beacon from computer and i'm trying to read beacon info (uuid,identifier etc) from my react native app.

I decided to use this library https://github.com/dotintent/react-native-ble-plx as the library.

The issue i'm facing is although it shows some devices when i scan, the name,uuid and other info shows as null.

For ex:

'device', { serviceUUIDs: null,
      isConnectable: null,
      overflowServiceUUIDs: null,
      txPowerLevel: null,
      serviceData: null,
      manufacturerData: 'TEACFCd6h5jcoxKqh9ACQqwTAAOBqZYcxQ==',
      name: null,
      mtu: 23,
      rssi: -47,
      solicitedServiceUUIDs: null,
      localName: null,
      id: '32:BD:32:6C:E9:C2',

And this is my code

const bluetoothInstance = new BleManager();

  const scanAndConnect = () => {
    bluetoothInstance.startDeviceScan(null, { allowDuplicates: true }, (error, device) => {
      console.log('device', device);
      console.log('error', error);
      if (error) {
        // Handle error (scanning will be stopped automatically)
        return;
      }

      if (device?.name === 'MyProjectName') {
        bluetoothInstance.stopDeviceScan();
      } else {
        // bluetoothInstance.stopDeviceScan();
      }
    });
  };

  useEffect(() => {
    bluetoothInstance.onStateChange((state) => {
      console.log('state', state);
      if (state === 'PoweredOn') {
        scanAndConnect();
      }
    }, true);
  }, []);

How i can read the beacon uuid and name? Is there any other library you can recommend? Or is there something missing in the code? Any help would be appreciated.

CraZyDroiD
  • 6,622
  • 30
  • 95
  • 182
  • Could you check wether they actually have a name and service uuid using a generic BLE scanner such as [nRF Connect](https://www.nordicsemi.com/Products/Development-tools/nrf-connect-for-mobile)? – Michael Kotzjan Oct 06 '21 at 07:26
  • 1
    i checked from another beacon scanner,it showed my simulated beacon – CraZyDroiD Oct 06 '21 at 07:39

1 Answers1

2

The scanning code is probably working fine. The information you are looking for is either not always present or present in a different field.

The iBeacon Proximity UUID is actually embedded inside the manufacturerData field. However, on iOS devices this field is deleted by the operating system for all iBeacon packets as a security mechanism by Apple. Apple forbids using the CoreBluetooth framework (used by react-react-native-ble-plx under the hood) to detect iBeacon. On iOS you must use CoreLocation. For that you can try react-native-beacons-manager. For Android, react-native-ble-plx will work fine to detect iBeacon but you must parse out the beacon fields yourself from the manyfacturerData field.

The Bluetooth name is only populated (in the name field) if a scan response packet containing the name has been received recently before the main advertisement packet. It is unclear how iOS will handle this process for forbidden iBeacon advertisements, but it will probably work for non-iBeacon advertisements just fine. On Android it will work fine, too. Just do not expect it to be populated 100% of the time on either platform.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thanks for the explanation. Any idea how i can parse out the beacon fields using the manufacturerData? – CraZyDroiD Oct 06 '21 at 16:33
  • I have another StackOverflow answer about how to parse out the beacon fields [here](https://stackoverflow.com/questions/18906988/what-is-the-ibeacon-bluetooth-profile/19040616#19040616). Note that the manufacturerData field from `react-native-ble-plx` is Base64 encoded, so you need to Base64 decode that string to get the raw bytes of the manufacturer advertisement, then follow my answer above. – davidgyoung Oct 06 '21 at 18:05