Giter Site home page Giter Site logo

kumbhar-ketan / react-native-bluetooth-manager Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sogilis/react-native-bluetooth-manager

0.0 0.0 0.0 376 KB

Bluetooth API for React Native

License: Apache License 2.0

Python 1.00% Java 31.54% JavaScript 42.23% Objective-C 8.53% Swift 16.72%

react-native-bluetooth-manager's Introduction

react-native-bluetooth-manager

npm version

Disclaimer: This is alpha software.

Bluetooth API for React Native.

Features

  • Handle bluetooth availability
  • Scan nearby low-energy devices
  • Discover services and characteristics
  • Connect to devices
  • Read, write and receive characteristic notifications
  • Support both iOS/Android

Install

npm install --save react-native-bluetooth-manager
react-native link react-native-bluetooth-manager

Usage

See the Example app.

import Bluetooth from 'react-native-bluetooth-manager';

Bluetooth state change

const unsubscribe = Bluetooth.didChangeState(bluetoothState => {
    // bluetoothState == 'enabled' | 'disabled'
  });

Device scan

const discoverOptions = {
  uuids: [] // list of BLE service uuids to filter devices during scan
};

const onDeviceFound = device => {
  const {id, name} = device;
  ...
};

Bluetooth.startScanWithDiscovery(discoverOptions, onDeviceFound)
  .then(scan => scan.stopAfter(9000)) // automatically stop scan after 9000ms
  .then(stoppedOnTime => {
    // true if scan ran for full duration, false if stopped before
  });

Bluetooth.stopScan(); // manually stop scan

Device connection, services & characteristics discovery

Bluetooth.connect(device)
  .then(() => {
    // device is connected
    // proceed with services & characteristics discovery (see below)
  }).catch(error => {
    // error when connecting to device
  });

Bluetooth.disconnect(device)
  .then(() => { // disconnection ok })
  .catch(() => { // disconnection error });

Once device is connected, it is recommended to discover service and characteristics before calling any operation.

Bluetooth.discoverServices(device, serviceIds)
  .then(services => {
    // discover service characteristics
    services.forEach(service => {
      // optionally, perform characteristic discovery one after the other for better stability
      Bluetooth.discoverCharacteristics(service, characteristicIds)
        .then(characteristics => {
          // memoize characteristics for later use
        });
    });
  });

Disconnection events can happen any time due to loss of communication:

const unsubscribe = Bluetooth.deviceDidDisconnect(event => {
  unsubscribe();
  if (event.error) {
    // disconnection due to error
  } else {
    ...
  }
});

Characteristic operations

Use characteristics retrieved during the discovery stage.

Read a characteristic value:

Bluetooth.readCharacteristicValue(characteristic)
  .then(payload => {
    const {
      value, // value after base-64 decoding
      base64Value, // original base-64 encoded value
    } = payload;
    ...
  });

Write a value to a characteristic (with or without response):

const payload = new Buffer(...);
// see https://www.npmjs.com/package/buffer
// lib will encode buffer in Base 64 before transmission

Bluetooth.writeCharacteristicValue(characteristic, payload, withResponse)
  .then(() => {
    // if withResponse == true, wait for Bluetooth write response (acknowledgement)
    // if withResponse == false, returns immediately
  });

Subscribe to notifications from characteristic:

const onNotification = payload => {
  const { value } = payload; // value after base-64 decoding
};

const unsubscribe = Bluetooth.characteristicDidNotify(characteristic, onNotification);

unsubscribe(); // stop notifications handling

License

Copyright 2016 Sogilis SARL

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this software except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

react-native-bluetooth-manager's People

Contributors

sebn avatar graymanto avatar sdenier avatar

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.