/react-native-ble

Central role BLE for react native noble api surface

Primary LanguageJavaScriptMIT LicenseMIT

react-native-ble

Author

Originaly forked from Jacob Rosenthal

Installation

npm install git+https://git@github.com/Shakarang/react-native-ble

Linking

Next see the react native linking guide for visual instructions, but you need to open your project and from node_modules/react-native-estimote drag RNBLE.xcodeproj into your project file browser under Libraries. Then, navigate to your top level project and for your main target, click Build Phases and drag the RNBLE.a Product into the Link Binary With Libraries section. Finally, click the plus button and add CoreBluetooth.framework as well.

Usage

It follows the noble api usage.

Only support of following usages on iOS !

Require the module

var noble = ('react-native-ble');

Handle Bluetooth State

noble.on('stateChange', function (state) {
  if (state === 'poweredOn') {
    console.log('Start Scanning');
    noble.startScanning([], false);
  } else {
    noble.stopScanning();
    console.log('Stop Scanning');
  }
});

Start Scanning

noble.on('discover', function (peripheral) {
  // Peripheral is an object that was found
});

Peripheral

Connect

peripheral.connect(function (error) {

});

Disconnect

When you're connected to a peripheral, you can handle the automatic disconnection by doing :

peripheral.once('disconnect', function () {

});

If you want to disconnect manually :
```javascript
peripheral.disconnect(function (error) {

});

Discover Services

var serviceUUIDs = ["<service UUID 1>", ...];
peripheral.discoverServices(serviceUUIDs, function (error, services) {

});

Service

Discover characteristics

var service = services[0];
var characteristicsUUIDs = ["<service UUID 1>", ...];
service.discoverCharacteristics(characteristicsUUIDs, function (characteristics) {

});

Characteristic

Read

var characteristic = characteristics[0];
characteristic.read(function (data) {
  // Data is a buffer
});

Notify

var characteristic = characteristics[0];
characteristic.notify(function (data) {
  // Data is a buffer
});