Convert Figma logo to code with AI

dotintent logoreact-native-ble-plx

React Native BLE library

3,095
517
3,095
40

Top Related Projects

React Native BLE library

React Native BLE communication module

Quick Overview

React-Native-BLE-PLX is a Bluetooth Low Energy (BLE) library for React Native applications. It provides a comprehensive API for managing BLE connections, scanning for devices, and interacting with BLE peripherals on both iOS and Android platforms.

Pros

  • Cross-platform compatibility (iOS and Android)
  • Comprehensive API covering most BLE operations
  • Active development and community support
  • TypeScript support for improved type safety

Cons

  • Learning curve for developers new to BLE concepts
  • Occasional platform-specific issues due to differences in iOS and Android BLE implementations
  • Limited support for older Android versions (minimum SDK 18)
  • Potential performance overhead in high-throughput scenarios

Code Examples

  1. Scanning for BLE devices:
import { BleManager } from 'react-native-ble-plx';

const manager = new BleManager();

manager.startDeviceScan(null, null, (error, device) => {
  if (error) {
    console.log('Scan error:', error);
    return;
  }
  console.log('Discovered device:', device.name);
});
  1. Connecting to a device and reading a characteristic:
manager.connectToDevice(deviceId)
  .then((device) => device.discoverAllServicesAndCharacteristics())
  .then((device) => {
    return device.readCharacteristicForService(serviceUUID, characteristicUUID);
  })
  .then((characteristic) => {
    console.log('Characteristic value:', characteristic.value);
  })
  .catch((error) => {
    console.log('Error:', error);
  });
  1. Writing to a characteristic:
device.writeCharacteristicWithResponseForService(
  serviceUUID,
  characteristicUUID,
  Base64.encode('Hello BLE')
)
  .then((characteristic) => {
    console.log('Write successful');
  })
  .catch((error) => {
    console.log('Write error:', error);
  });

Getting Started

  1. Install the library:

    npm install --save react-native-ble-plx
    
  2. For iOS, add the following to your Info.plist:

    <key>NSBluetoothAlwaysUsageDescription</key>
    <string>Our app uses bluetooth to find, connect and transfer data between different devices</string>
    
  3. For Android, add the following permissions to your AndroidManifest.xml:

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  4. Import and use the library in your React Native app:

    import { BleManager } from 'react-native-ble-plx';
    
    const bleManager = new BleManager();
    // Start using BLE functionality
    

Competitor Comparisons

React Native BLE library

Pros of react-native-ble-plx

  • Comprehensive BLE functionality for React Native
  • Active development and maintenance
  • Extensive documentation and examples

Cons of react-native-ble-plx

  • Larger package size due to extensive features
  • Steeper learning curve for beginners
  • May require additional setup for certain platforms

Code Comparison

react-native-ble-plx:

import { BleManager } from 'react-native-ble-plx';

const manager = new BleManager();
manager.startDeviceScan(null, null, (error, device) => {
  if (error) {
    console.log(error);
    return;
  }
  console.log(device.name);
});

Both repositories are actually the same project. The repository dotintent/react-native-ble-plx is the main and only repository for the react-native-ble-plx library. There is no separate repository to compare it with. The library provides a comprehensive set of features for working with Bluetooth Low Energy (BLE) in React Native applications.

The react-native-ble-plx library offers a robust API for scanning, connecting, and interacting with BLE devices. It supports both iOS and Android platforms and provides a consistent interface for developers to work with BLE functionality across different devices.

Given that there's only one repository, the pros and cons listed above are general observations about the library itself, rather than a comparison between two different repositories.

React Native BLE communication module

Pros of react-native-ble-manager

  • Simpler API with fewer methods, making it easier to learn and use for basic BLE operations
  • Lighter weight and potentially better performance due to its focused feature set
  • More active community with frequent updates and issue resolutions

Cons of react-native-ble-manager

  • Less comprehensive feature set compared to react-native-ble-plx
  • Limited support for advanced BLE operations and complex scenarios
  • Fewer platform-specific optimizations and customizations

Code Comparison

react-native-ble-manager:

BleManager.start({ showAlert: false })
  .then(() => {
    console.log('BLE Manager initialized');
  });

react-native-ble-plx:

const manager = new BleManager();
manager.onStateChange((state) => {
  if (state === 'PoweredOn') {
    console.log('BLE Manager initialized');
  }
}, true);

Both libraries offer similar basic functionality, but react-native-ble-plx provides more granular control over the BLE state and initialization process. react-native-ble-manager's API is more straightforward, while react-native-ble-plx offers more flexibility and advanced features at the cost of increased complexity.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-native-ble-plx library logo

About this library

It supports:

It does NOT support:

Table of Contents

  1. Compatibility
  2. Recent Changes
  3. Documentation & Support
  4. Configuration & Installation
  5. Troubleshooting
  6. Contributions

Compatibility

For old RN versions (<0.60) please check old README (1.x) for the old instructions or migration guide.

React Native3.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 the AndroidManifest.xml. Default false.
  • 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+. Default false. WARNING: This parameter is experimental and BLE might not work. Make sure to test before releasing to production.
  • modes (string[]): Adds iOS UIBackgroundModes to the Info.plist. Options are: peripheral, and central. Defaults to undefined.
  • bluetoothAlwaysPermission (string | false): Sets the iOS NSBluetoothAlwaysUsageDescription permission message to the Info.plist. Setting false will skip adding the permission. Defaults to Allow $(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/react-native-ble-plx@5.0.0 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)

  1. Make sure your Expo project is ejected (formerly: detached). You can read how to do it here. (only for Expo SDK < 43)
  2. Follow steps for iOS/Android.

iOS (example setup)

  1. npm install --save react-native-ble-plx
  2. Enter ios folder and run pod update
  3. Add NSBluetoothAlwaysUsageDescription in info.plist file. (it is a requirement since iOS 13)
  4. If you want to support background mode:
    • In your application target go to Capabilities tab and enable Uses Bluetooth LE Accessories in Background Modes section.
    • Pass restoreStateIdentifier and restoreStateFunction to BleManager constructor.

Android (example setup)

  1. npm install --save react-native-ble-plx

  2. In top level build.gradle make sure that min SDK version is at least 23:

    buildscript {
        ext {
            ...
            minSdkVersion = 23
            ...
    
  3. In build.gradle make sure to add jitpack repository to known repositories:

    allprojects {
        repositories {
          ...
          maven { url 'https://www.jitpack.io' }
        }
    }
    
  4. 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"/>
    
        ...
    
  5. (Optional) In SDK 31+ You can remove ACCESS_FINE_LOCATION (or mark it as android:maxSdkVersion="30" ) from AndroidManifest.xml and add neverForLocation flag into BLUETOOTH_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 for ACCESS_FINE_LOCATION in your app

Troubleshooting

Contributions

  • Special thanks to @EvanBacon for supporting the expo config plugin.

NPM DownloadsLast 30 Days