Discover nearby devices using BLE Bluetooth Low Energy.
This is a fork from https://github.com/N3TC4T/react-native-discovery.
Discovery is a useful library for discovering nearby devices with BLE(Bluetooth Low Energy) and for exchanging a value (UUID) that can be an unique ID for device or user.
- iOS 9+
- Android (API 19+)
npm install --save https://github.com/jonathanpalma/react-native-discovery
$ react-native link react-native-discovery
import Discovery from "react-native-discovery";
const myUUID = "3E1180E5-222E-43E9-98B4-E6C0DD18E728";
const myService = "MYAPP"
Discovery.initialize(myUUID, myService).then(uuid => {
Discovery.setShouldAdvertise(true);
Discovery.setShouldDiscover(true);
});
// Listen for discovery changes
Discovery.on("discoveredUsers", (data) => { console.log(data.users) } );
// Listen for bluetooth state changes
Discovery.on('bleStateChanged', (event) => { console.log('BLE is On: ' + event.isOn) } );
Or, you can still look into the whole example folder for a standalone project.
Method | Params | Info |
---|---|---|
initialize(uuid, service) | string, string | Initialize the Discovery object with a UUID specific to your device, and a service specific to your app. Returns a promise which resolves to the specified UUID |
setPaused(isPaused) | bool | pauses advertising and detection |
setShouldDiscover(shouldDiscover) | bool | starts and stops discovery |
setShouldAdvertise(shouldAdvertise) | bool | starts and stops advertising |
setUserTimeoutInterval(userTimeoutInterval) | integer in seconds | After not seeing a user for x seconds, we remove him from the users list in our callback (for the specified uuid) |
isBluetoothEnabled() | null | Returns a promise, which will return a boolean value, true if bluetooth is enabled, false if disabled. |
The following methods are specific to the Android version.
Method | Params | Info |
---|---|---|
setScanForSeconds(scanForSeconds) | integer in seconds | This parameter specifies the duration of the ON part of the scan cycle for the specified uuid. Returns a promise which resolves to true. |
setWaitForSeconds(waitForSeconds) | integer in seconds | This parameter specifies the duration of the OFF part of the scan cycle for the specified uuid. Returns a promise which resolves to true. |
setBluetoothOn() | null | Changes bluetooth state to On, Returns a promise, which returns whether the change was successful or not. |
setBluetoothOff() | null | Changes bluetooth state to Off, Returns a promise, which returns whether the change was successful or not. |
isLocationEnabled() | null | Returns a promise, which will return a boolean value, true if location is enabled, false if disabled. |