Events
BleManagerStopScan
The scanning for peripherals is ended.
Arguments
status
-Number
- [iOS] the reason for stopping the scan. Error code 10 is used for timeouts, 0 covers everything else. [Android] the reason for stopping the scan (https://developer.android.com/reference/android/bluetooth/le/ScanCallback#constants_1). Error code 10 is used for timeouts
Examples
bleManagerEmitter.addListener("BleManagerStopScan", (args) => {
// Scanning is stopped
});
BleManagerDidUpdateState
The BLE change state.
Arguments
state
-String
- the new BLE state. can be one ofunknown
(iOS only),resetting
(iOS only),unsupported
,unauthorized
(iOS only),on
,off
,turning_on
(android only),turning_off
(android only).
Examples
bleManagerEmitter.addListener("BleManagerDidUpdateState", (args) => {
// The new state: args.state
});
BleManagerDiscoverPeripheral
The scanning find a new peripheral.
Arguments
id
-String
- the id of the peripheralname
-String
- the name of the peripheralrssi
-Number
- the RSSI valueadvertising
-JSON
- the advertising payload, here are some examples:isConnectable
-Boolean
serviceUUIDs
-Array of String
manufacturerData
-JSON
- contains a json with the company id as field and the custom value as rawbytes
anddata
(Base64 encoded string)serviceData
-JSON
- contains the rawbytes
anddata
(Base64 encoded string)txPowerLevel
-Int
rawData
- [Android only]JSON
- contains the rawbytes
anddata
(Base64 encoded string) of the all advertising data
Examples
bleManagerEmitter.addListener("BleManagerDiscoverPeripheral", (args) => {
// The id: args.id
// The name: args.name
});
BleManagerDidUpdateValueForCharacteristic
A characteristic notify a new value.
Arguments
value
—Array
— the read valueperipheral
—String
— the id of the peripheralcharacteristic
—String
— the UUID of the characteristicservice
—String
— the UUID of the characteristic
Event will only be emitted after successful
startNotification
.
Example
import { bytesToString } from "convert-string";
import { NativeModules, NativeEventEmitter } from "react-native";
const BleManagerModule = NativeModules.BleManager;
const bleManagerEmitter = new NativeEventEmitter(BleManagerModule);
async function connectAndPrepare(peripheral, service, characteristic) {
// Connect to device
await BleManager.connect(peripheral);
// Before startNotification you need to call retrieveServices
await BleManager.retrieveServices(peripheral);
// To enable BleManagerDidUpdateValueForCharacteristic listener
await BleManager.startNotification(peripheral, service, characteristic);
// Add event listener
bleManagerEmitter.addListener(
"BleManagerDidUpdateValueForCharacteristic",
({ value, peripheral, characteristic, service }) => {
// Convert bytes array to string
const data = bytesToString(value);
console.log(`Received ${data} for characteristic ${characteristic}`);
}
);
// Actions triggereng BleManagerDidUpdateValueForCharacteristic event
}
BleManagerConnectPeripheral
A peripheral was connected.
Arguments
peripheral
-String
- the id of the peripheralstatus
-Number
- [Android only] connectreasons
BleManagerDisconnectPeripheral
A peripheral was disconnected.
Arguments
peripheral
-String
- the id of the peripheralstatus
-Number
- [Android only] disconnectreasons
domain
-String
- [iOS only] disconnect error domaincode
-Number
- [iOS only] disconnect error code (https://developer.apple.com/documentation/corebluetooth/cberror/code)
BleManagerPeripheralDidBond
A bond with a peripheral was established
Arguments
Object with information about the device
BleManagerCentralManagerWillRestoreState [iOS only]
This is fired when centralManager:WillRestoreState:
is called (app relaunched in the background to handle a bluetooth event).
Arguments
peripherals
-Array
- an array of previously connected peripherals.
For more on performing long-term bluetooth actions in the background:
iOS Bluetooth State Preservation and Restoration
BleManagerDidUpdateNotificationStateFor [iOS only]
The peripheral received a request to start or stop providing notifications for a specified characteristic’s value.
Arguments
peripheral
-String
- the id of the peripheralcharacteristic
-String
- the UUID of the characteristicisNotifying
-Boolean
- Is the characteristic notifying or notdomain
-String
- [iOS only] error domaincode
-Number
- [iOS only] error code
BleManagerCompanionPeripheral [Android only]
User picked a device to associate with.
Null if the request was cancelled by the user.
Arguments
id
-String
- the id of the peripheralname
-String
- the name of the peripheralrssi
-Number
- the RSSI value
BleManagerCompanionFailure [Android only]
Associate callback received a failure or failed to start the intent to pick the device to associate.