react-native-ble-plx
v3.2.1
Published
React Native Bluetooth Low Energy library
Downloads
118,573
Readme
About this library
It supports:
- observing device's Bluetooth adapter state
- scanning BLE devices
- making connections to peripherals
- discovering services/characteristics
- reading/writing characteristics
- observing characteristic notifications/indications
- reading RSSI
- negotiating MTU
- background mode on iOS
- turning the device's Bluetooth adapter on
It does NOT support:
- bluetooth classic devices.
- communicating between phones using BLE (Peripheral support)
- bonding peripherals
Table of Contents
- Compatibility
- Recent Changes
- Documentation & Support
- Configuration & Installation
- Troubleshooting
- Contributions
Compatibility
For old RN versions (<0.60) please check old README (1.x) for the old instructions or migration guide.
| React Native | 3.1.2 | |--------------| ------------------ | | 0.74.1 | :white_check_mark: | | 0.69.6 | :white_check_mark: | | Expo 51 | :white_check_mark: |
Recent Changes
3.2.0
- Added Android Instance checking before calling its method, an error will be visible on the RN side
- Added information related to Android 14 to the documentation.
- Changed destroyClient, cancelTransaction, setLogLevel, startDeviceScan, stopDeviceScan calls to promises to allow error reporting if it occurs.
- Fixed one of the functions calls that clean up the BLE instance after it is destroyed.
Current version changes All previous changes
Documentation & Support
Interested in React Native project involving Bluetooth Low Energy? We can help you!
Documentation can be found here.
Quick introduction can be found here
Contact us at intent.
Configuration & Installation
Expo SDK 43+
Tested against Expo SDK 49 This package cannot be used in the "Expo Go" app because it requires custom native code. First install the package with yarn, npm, or
npx expo install
.
After installing this npm package, add the config plugin to the plugins
array of your app.json
or app.config.js
:
{
"expo": {
"plugins": ["react-native-ble-plx"]
}
}
Then you should build the version using native modules (e.g. with npx expo prebuild
command).
And install it directly into your device with npx expo run:android
.
You can find more details in the "Adding custom native code" guide.
API
The plugin provides props for extra customization. Every time you change the props or plugins, you'll need to rebuild (and prebuild
) the native app. If no extra properties are added, defaults will be used.
isBackgroundEnabled
(boolean): Enable background BLE support on Android. Adds<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
to theAndroidManifest.xml
. Defaultfalse
.neverForLocation
(boolean): Set to true only if you can strongly assert that your app never derives physical location from Bluetooth scan results. The location permission will be still required on older Android devices. Note, that some BLE beacons are filtered from the scan results. Android SDK 31+. Defaultfalse
. WARNING: This parameter is experimental and BLE might not work. Make sure to test before releasing to production.modes
(string[]): Adds iOSUIBackgroundModes
to theInfo.plist
. Options are:peripheral
, andcentral
. Defaults to undefined.bluetoothAlwaysPermission
(string | false): Sets the iOSNSBluetoothAlwaysUsageDescription
permission message to theInfo.plist
. Settingfalse
will skip adding the permission. Defaults toAllow $(PRODUCT_NAME) to connect to bluetooth devices
.
Expo SDK 48 supports iOS 13+ which means
NSBluetoothPeripheralUsageDescription
is fully deprecated. It is no longer setup in@config-plugins/[email protected]
and greater.
Example
{
"expo": {
"plugins": [
[
"react-native-ble-plx",
{
"isBackgroundEnabled": true,
"modes": ["peripheral", "central"],
"bluetoothAlwaysPermission": "Allow $(PRODUCT_NAME) to connect to bluetooth devices"
}
]
]
}
}
Legacy Expo (SDK < 43)
- Make sure your Expo project is ejected (formerly: detached). You can read how to do it here. (only for Expo SDK < 43)
- Follow steps for iOS/Android.
iOS (example setup)
npm install --save react-native-ble-plx
- Enter
ios
folder and runpod update
- Add
NSBluetoothAlwaysUsageDescription
ininfo.plist
file. (it is a requirement since iOS 13) - If you want to support background mode:
- In your application target go to
Capabilities
tab and enableUses Bluetooth LE Accessories
inBackground Modes
section. - Pass
restoreStateIdentifier
andrestoreStateFunction
toBleManager
constructor.
- In your application target go to
Android (example setup)
npm install --save react-native-ble-plx
In top level
build.gradle
make sure that min SDK version is at least 23:buildscript { ext { ... minSdkVersion = 23 ...
In
build.gradle
make sure to add jitpack repository to known repositories:allprojects { repositories { ... maven { url 'https://www.jitpack.io' } } }
In
AndroidManifest.xml
, add Bluetooth permissions and update<uses-sdk/>
:<manifest xmlns:android="http://schemas.android.com/apk/res/android"> ... <!-- Android >= 12 --> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- Android < 12 --> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> <!-- common --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Add this line if your application always requires BLE. More info can be found on: https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions --> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> ...
(Optional) In SDK 31+ You can remove
ACCESS_FINE_LOCATION
(or mark it asandroid:maxSdkVersion="30"
) fromAndroidManifest.xml
and addneverForLocation
flag intoBLUETOOTH_SCAN
permissions which says that you will not use location based on scanning eg:<uses-permission android:name="android.permission.INTERNET" /> <!-- Android >= 12 --> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <!-- Android < 12 --> <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" /> ...
With
neverForLocation
flag active, you no longer need to ask forACCESS_FINE_LOCATION
in your app
Troubleshooting
Contributions
- Special thanks to @EvanBacon for supporting the expo config plugin.