Convert Figma logo to code with AI

innoveit logoreact-native-ble-manager

React Native BLE communication module

2,143
772
2,143
13

Top Related Projects

An Android Bluetooth Low Energy (BLE) Library with RxJava3 interface

React Native BLE library

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.

Quick Overview

React Native BLE Manager is a Bluetooth Low Energy (BLE) library for React Native applications. It provides a comprehensive set of functions to interact with BLE devices, including scanning, connecting, and reading/writing characteristics.

Pros

  • Cross-platform support for both iOS and Android
  • Extensive API covering most BLE operations
  • Active community and regular updates
  • Well-documented with examples and TypeScript support

Cons

  • Requires native module linking, which can be complex for some developers
  • Performance may vary depending on device and OS version
  • Limited support for background operations on iOS
  • Some users report occasional stability issues with certain devices

Code Examples

Scanning for BLE devices:

import BleManager from 'react-native-ble-manager';

BleManager.start({ showAlert: false });

BleManager.scan([], 5, true).then(() => {
  console.log('Scan started');
});

BleManager.addListener('BleManagerDiscoverPeripheral', (device) => {
  console.log('Discovered device:', device);
});

Connecting to a device and reading a characteristic:

BleManager.connect(deviceId)
  .then(() => {
    console.log('Connected');
    return BleManager.retrieveServices(deviceId);
  })
  .then((peripheralInfo) => {
    console.log('Peripheral info:', peripheralInfo);
    return BleManager.read(deviceId, serviceUUID, characteristicUUID);
  })
  .then((readData) => {
    console.log('Read:', readData);
  })
  .catch((error) => {
    console.log('Error:', error);
  });

Writing to a characteristic:

const data = [0x01, 0x02, 0x03];
BleManager.write(deviceId, serviceUUID, characteristicUUID, data)
  .then(() => {
    console.log('Write successful');
  })
  .catch((error) => {
    console.log('Write error:', error);
  });

Getting Started

  1. Install the library:

    npm install react-native-ble-manager
    
  2. Link the native modules:

    npx react-native link react-native-ble-manager
    
  3. Import and initialize in your React Native app:

    import BleManager from 'react-native-ble-manager';
    
    // In your app's entry point or main component
    componentDidMount() {
      BleManager.start({ showAlert: false })
        .then(() => {
          console.log('BLE Manager initialized');
        })
        .catch((error) => {
          console.log('BLE Manager initialization error:', error);
        });
    }
    
  4. Add necessary permissions to your app's Info.plist (iOS) and AndroidManifest.xml (Android) files.

Competitor Comparisons

An Android Bluetooth Low Energy (BLE) Library with RxJava3 interface

Pros of RxAndroidBle

  • Utilizes RxJava for reactive programming, offering better handling of asynchronous operations
  • Provides more robust error handling and connection state management
  • Offers a more comprehensive API for advanced BLE operations

Cons of RxAndroidBle

  • Limited to Android platform, while react-native-ble-manager supports both iOS and Android
  • Steeper learning curve for developers not familiar with RxJava
  • May have higher overhead due to RxJava dependency

Code Comparison

RxAndroidBle:

rxBleClient.scanBleDevices()
    .subscribe(
        scanResult -> {
            // Handle scan result
        },
        throwable -> {
            // Handle error
        }
    );

react-native-ble-manager:

BleManager.scan([], 5, true)
  .then(() => {
    // Scan started
  })
  .catch((error) => {
    // Handle error
  });

The RxAndroidBle example demonstrates the use of RxJava observables for scanning, while react-native-ble-manager uses promises. RxAndroidBle provides a more streamlined approach to handling continuous scan results and errors within a single subscription.

React Native BLE library

Pros of react-native-ble-plx

  • More comprehensive API with better TypeScript support
  • Actively maintained with frequent updates and bug fixes
  • Better documentation and examples for complex use cases

Cons of react-native-ble-plx

  • Larger package size and potentially higher memory footprint
  • Steeper learning curve for beginners due to more advanced features

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);
});

react-native-ble-manager:

import BleManager from 'react-native-ble-manager';

BleManager.start({ showAlert: false });
BleManager.scan([], 5, true).then(() => {
  console.log('Scan started');
});

Both libraries provide similar functionality for BLE operations in React Native, but react-native-ble-plx offers a more robust and feature-rich API. It's better suited for complex projects with advanced BLE requirements, while react-native-ble-manager is simpler and may be easier for beginners to grasp. The choice between the two depends on the specific needs of your project and your team's expertise.

A library that makes working with Bluetooth LE on Android a pleasure. Seriously.

Pros of Android-BLE-Library

  • Native Android development, offering better performance and deeper system integration
  • Comprehensive BLE functionality, including advanced features like packet splitting and MTU negotiation
  • Extensive documentation and examples provided by Nordic Semiconductor

Cons of Android-BLE-Library

  • Limited to Android platform, lacking cross-platform support
  • Steeper learning curve for developers not familiar with native Android development
  • May require more boilerplate code compared to React Native solutions

Code Comparison

Android-BLE-Library (Kotlin):

val bleManager = BleManager.getInstance()
bleManager.connect(device)
    .retry(3, 100)
    .useAutoConnect(false)
    .timeout(30000)
    .done { /* Handle connection */ }
    .fail { /* Handle error */ }

react-native-ble-manager (JavaScript):

BleManager.connect(deviceId)
  .then(() => {
    console.log('Connected');
  })
  .catch((error) => {
    console.log('Connection error', error);
  });

The Android-BLE-Library offers more granular control over the connection process, while react-native-ble-manager provides a simpler, cross-platform API. The native library allows for more advanced configuration options, such as retry attempts and connection timeouts, which may be beneficial for complex BLE applications.

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-manager

npm version npm downloads GitHub issues

A React Native Bluetooth Low Energy library.

Originally inspired by https://github.com/don/cordova-plugin-ble-central.

Introduction

The library is a simple connection with the OS APIs, the BLE stack should be standard but often has different behaviors based on the device used, the operating system and the BLE chip it connects to. Before opening an issue verify that the problem is really the library.

Requirements

RN 0.60+

RN 0.40-0.59 supported until 6.7.X RN 0.30-0.39 supported until 2.4.3

Supported Platforms

  • iOS 10+
  • Android (API 19+)

Install

npm i --save react-native-ble-manager

The library support the react native autolink feature.

Documentation

Read here the full documentation

Example

The easiest way to test is simple make your AppRegistry point to our example component, like this:

// in your index.ios.js or index.android.js
import React, { Component } from "react";
import { AppRegistry } from "react-native";
import App from "react-native-ble-manager/example/App"; //<-- simply point to the example js!
/* 
Note: The react-native-ble-manager/example directory is only included when cloning the repo, the above import will not work 
if trying to import react-native-ble-manager/example from node_modules
*/
AppRegistry.registerComponent("MyAwesomeApp", () => App);

Or, use the example directly

Library development

  • the library is written in typescript and needs to be built before being used for publication or local development, using the provided npm scripts in package.json.
  • the local example project is configured to work with the locally built version of the library. To be able to run it, you need to build at least once the library so that its outputs listed as entrypoint in package.json (in the dist folder) are properly generated for consumption by the example project:

from the root folder:

npm install
npm run build

if you are modifying the typescript files of the library (in src/) on the fly, you can run npm run watch instead. If you are modifying files from the native counterparts, you'll need to rebuild the whole app for your target environnement (npm run android/ios).

NPM DownloadsLast 30 Days