Table of contents
- onStopScan
- onDidUpdateState
- onDiscoverPeripheral
- onDidUpdateValueForCharacteristic
- onConnectPeripheral
- onDisconnectPeripheral
- onPeripheralDidBond
- onCentralManagerWillRestoreState [iOS only]
- onDidUpdateNotificationStateFor [iOS only]
- onCompanionPeripheral [Android only]
- onCompanionFailure [Android only]
Events
Since react-native version 0.76, events are handled with specific methods that return the listener.
Examples
useEffect(() => {
const onStopListener = BleManager.onStopScan((args) => {
// Scanning is stopped args.status
});
return () => {
onStopListener.remove();
};
}, []);
onStopScan
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
onDidUpdateState
The BLE state changed.
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).
onDiscoverPeripheral
The scanning found 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-BooleanserviceUUIDs-String[]manufacturerData-JSON- contains a json with the company id as field (4-digit lowercase hex string, e.g."004c"for Apple) and the custom value as rawbytesanddata(Base64 encoded string). On Android, if the same company id is in both the advertising packet and the scan response, the data is merged in that order. On iOS, the data is the manufacturer data as reported by CoreBluetoothmanufacturerRawData-JSON- contains the rawbytesanddata(Base64 encoded string) of the manufacturer data: the 2-byte company id in little-endian order followed by the data (e.g. company0xAABBwith data01 02 03 04->bb aa 01 02 03 04), same format on Android and iOS. Before this version, Android used a 4-byte big-endian company id (00 00 aa bb)serviceData-JSON- contains the rawbytesanddata(Base64 encoded string)txPowerLevel-IntrawData- [Android only]JSON- contains the rawbytesanddata(Base64 encoded string) of all the advertising data
onDidUpdateValueForCharacteristic
A characteristic notified a new value.
Event will only be emitted after successful
startNotification.
Arguments
value—Number[]— the read valueperipheral—String— the id of the peripheralcharacteristic—String— the UUID of the characteristicservice—String— the UUID of the characteristic
onConnectPeripheral
A peripheral was connected.
Arguments
peripheral-String- the id of the peripheralstatus-Number- [Android only] connectreasons
onDisconnectPeripheral
A peripheral was disconnected.
Arguments
peripheral-String- the id of the peripheralstatus-Number- [Android only] disconnectreasonsdomain-String- [iOS only] disconnect error domaincode-Number- [iOS only] disconnect error code (https://developer.apple.com/documentation/corebluetooth/cberror/code)
onPeripheralDidBond
A bond with a peripheral was established.
Arguments
Object with information about the device.
onCentralManagerWillRestoreState [iOS only]
This is fired when centralManager:WillRestoreState: is called (app relaunched in the background to handle a bluetooth event).
For more on performing long-term bluetooth actions in the background:
iOS Bluetooth State Preservation and Restoration
Arguments
peripherals-Array- an array of previously connected peripherals.
onDidUpdateNotificationStateFor [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
onCompanionPeripheral [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
onCompanionFailure [Android only]
Associate callback received a failure or failed to start the intent to pick the device to associate.