Bluetooth Advertiser & Scanner for React Native. This is in a very early development focused in contact tracing applications. Please use with caution.
RN 0.60+
- Android (API 21+)
- Android Advertiser (v0.0.2)
- Android Scanner (v0.0.6)
- Android BLE Status Events (v0.0.8)
- iOS Advertiser (v0.0.10)
- iOS Scanner (v0.0.11)
- iOS BLE Status Events
- Android Background Service (Just use react-native-background-fetch for now)
- iOS Background Service (Just use react-native-background-fetch for now)
npm install react-native-ble-advertiser --save
In AndroidManifest.xml, add Bluetooth permissions and update :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
On your plist file, include:
<key>NSLocationWhenInUseUsageDescription</key>
<string>We log your location to allow comparisons with other user's locations in a privacy-preserving way.</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>We broadcast and scan for bluetooth signals in a way to track all phones nearby you in a privacy-preserving way.</string>
<key>UIBackgroundModes</key>
<array>
<string>bluetooth-central</string>
<string>bluetooth-peripheral</string>
...
</array>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>bluetooth-le</string>
....
</array>
Import the module
import BLEAdvertiser from 'react-native-ble-advertiser'
Define your company ID and broadcast your UUID with additional data. Start:
BLEAdvertiser.setCompanyId(0x00); // Your Company's Code
BLEAdvertiser.broadcast(UUID, [ManufacturerData], {}) // The UUID you would like to advertise and additional manufacturer data.
.then(success => console.log('Broadcasting Sucessful', success))
.catch(error => console.log('Broadcasting Error', error));
Stop broadcasting
BLEAdvertiser.stopBroadcast()
.then(success => console.log("Stop Broadcast Successful", success))
.catch(error => console.log("Stop Broadcast Error", error));
Import the modules
import BLEAdvertiser from 'react-native-ble-advertiser'
import { NativeEventEmitter, NativeModules } from 'react-native';
Define your company ID and additional data (Scanner fitlers inbound based on these).
BLEAdvertiser.setCompanyId(0x00); // Your Company's Code
BLEAdvertiser.scan([ManufacturerData], {}) // manufacturer data and options
.then(success => console.log("Scan Successful", success))
.catch(error => console.log("Scan Error", error));
Collect devices through ReactNative events.
const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);
eventEmitter.addListener('onDeviceFound', (event) => {
console.log(event) // "device data"
});
Stop scannig.
BLEAdvertiser.stopScan()
.then(success => console.log("Stop Scan Successful", success))
.catch(error => console.log("Stop Scan Error", error));
const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);
onBTStatusChange = eventEmitter.addListener('onBTStatusChange', (enabled) => {
console.log("Bluetooth status: ", enabled);
});