Skip to content

PrivateKit/react-native-ble-advertiser

 
 

Repository files navigation

react-native-ble-advertiser npm version npm downloads GitHub issues

Bluetooth Advertiser & Scanner for React Native. This is in a very early development focused in contact tracing applications. Please use with caution.

Requirements

RN 0.60+

Supported Platforms

  • Android (API 21+)

Features / TO-DO List

  • 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)

Install

npm install react-native-ble-advertiser --save

Setting up the Android Project

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"/>

Setting up the iOS Project

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>

Usage

Advertiser

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));

Scanner

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));

Bluetooth Status

const eventEmitter = new NativeEventEmitter(NativeModules.BLEAdvertiser);
onBTStatusChange = eventEmitter.addListener('onBTStatusChange', (enabled) => {
    console.log("Bluetooth status: ", enabled);
});

About

Bluetooth Advertiser/Scanner for React Native.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 47.1%
  • Objective-C 22.9%
  • JavaScript 21.5%
  • Ruby 6.1%
  • Shell 1.3%
  • Starlark 1.1%