Giter Club home page Giter Club logo

react-native-ble-manager's Introduction

react-native-ble-manager

npm version npm downloads GitHub issues

This is a porting of https://github.com/don/cordova-plugin-ble-central project to React Native.

##Supported Platforms

  • iOS
  • Android (API 18)

##Install

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

####iOS

  • Open the node_modules/react-native-ble-manager/ios folder and drag BleManager.xcodeproj into your Libraries group.
  • Check the "Build Phases"of your project and add "libBleManager.a" in the "Link Binary With Libraries" section.

####Android #####Update Gradle Settings

// file: android/settings.gradle
...

include ':react-native-ble-manager'
project(':react-native-ble-manager').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-ble-manager/android')

#####Update Gradle Build

// file: android/app/build.gradle
...

dependencies {
    ...
    compile project(':react-native-ble-manager')
}

#####Register React Package

...
import it.innove.BleManagerPackage; // <--- import

public class MainApplication extends Application implements ReactApplication {

    ...

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new BleManagerPackage() // <------ add the package
        );
    }

    ...
}

##Methods

scan(serviceUUIDs, seconds)

Scan for availables peripherals. Returns a Promise object.

Arguments

  • serviceUUIDs - Array of String - the UUIDs of the services to looking for. On Android the filter works only for 5.0 or newer.
  • seconds - Integer - the amount of seconds to scan.
  • allowDuplicates - Boolean - [iOS only] allow duplicates in device scanning

Examples

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

connect(peripheralId)

Attempts to connect to a peripheral. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral to connect.

Examples

BleManager.connect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
  .then(() => {
    // Success code
    console.log('Connected');
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

disconnect(peripheralId)

Disconnect from a peripheral. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral to disconnect.

Examples

BleManager.disconnect('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
  .then(() => {
    // Success code
    console.log('Disconnected');
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

checkState()

Force the module to check the state of BLE and trigger a BleManagerDidUpdateState event.

Examples

BleManager.checkState();

startNotification(peripheralId, serviceUUID, characteristicUUID)

Start the notification on the specified characteristic. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral.
  • serviceUUID - String - the UUID of the service.
  • characteristicUUID - String - the UUID of the characteristic.

Examples

BleManager.startNotification('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
  .then(() => {
    // Success code
    console.log('Notification started');
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

stopNotification(peripheralId, serviceUUID, characteristicUUID)

Stop the notification on the specified characteristic. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral.
  • serviceUUID - String - the UUID of the service.
  • characteristicUUID - String - the UUID of the characteristic.

read(peripheralId, serviceUUID, characteristicUUID)

Read the current value of the specified characteristic. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral.
  • serviceUUID - String - the UUID of the service.
  • characteristicUUID - String - the UUID of the characteristic.

Examples

BleManager.read('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX')
  .then((readData) => {
    // Success code
    console.log('Read: ' + readData);
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

write(peripheralId, serviceUUID, characteristicUUID, data)

Write with response to the specified characteristic. Returns a Promise object.

Arguments

  • peripheralId - String - the id/mac address of the peripheral.
  • serviceUUID - String - the UUID of the service.
  • characteristicUUID - String - the UUID of the characteristic.
  • data - String - the data to write in Base64 format.

To get the data into base64 format, you will need a library like base64-js. Install base64-js:

npm install base64-js --save

To format the data before calling the write function:

var base64 = require('base64-js');
var data = base64.fromByteArray(yourData);

Examples

BleManager.write('XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX', data)
  .then(() => {
    // Success code
    console.log('Write: ' + data);
  })
  .catch((error) => {
    // Failure code
    console.log(error);
  });

##Events

BleManagerStopScan

The scanning for peripherals is ended.

Arguments

  • none

Examples

NativeAppEventEmitter.addListener(
    'BleManagerStopScan',
    () => {
        // Scanning is stopped
    }
);

BleManagerDidUpdateState

The BLE change state.

Arguments

  • state - String - the new BLE state ('on'/'off').

Examples

NativeAppEventEmitter.addListener(
    'BleManagerDidUpdateState',
    (args) => {
        // The new state: args.state
    }
);

BleManagerDiscoverPeripheral

The scanning find a new peripheral.

Arguments

  • id - String - the id of the peripheral
  • name - String - the name of the peripheral

Examples

NativeAppEventEmitter.addListener(
    'BleManagerDiscoverPeripheral',
    (args) => {
        // The id: args.id
        // The name: args.name
    }
);

BleManagerDidUpdateValueForCharacteristic

A characteristic notify a new value.

Arguments

  • peripheral - String - the id of the peripheral
  • characteristic - String - the UUID of the characteristic
  • value - String - the read value in Hex format

BleManagerDisconnectPeripheral

A peripheral is disconnected.

Arguments

  • peripheral - String - the id of the peripheral

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.