Giter Site home page Giter Site logo

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

graymanto avatar sdenier avatar sebn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-bluetooth-manager's Issues

Bluetooth peripheral support!

Hi. This issue is a sort of query. And the query is - if this library can/ "will" ( do you have any plans :) ) make android device a beacon ( act as bluetooth peripheral device ).

RCTBridge.h import syntax fix and embedded binaries instructions fix needed

For anyone having issues with RCT import errors on a freshly expo-ejected create-react-native-app running react-native 0.55, all #import "RCTBridge.h import statements should be changed to #import <React/RCTBridge.h> (use find and replace in xcode and change them all). This is the SO answer that pointed me in the right direction.

Additionally, I needed to add CoreBluetoothSwift.framework to my embedded binaries in xcode under my project's general tab. Hope this helps others run this library on react-native v0.55. SO answer that helped me out.)
Using xcode 9.3.1.

Id comparison is case sensitive

We are currently using string representations of device/service/characteristic's UUID as their id (whether this is or this is not a good idea may be discussed in another issue).

  • The UUID RFC says that string representation should be lowercase.
  • The Bluetooth spec uses uppercase everywhere ๐Ÿ˜ญ
  • The Android bluetooth stack uses the java UUID class, which converts to lowercase string ๐Ÿ˜„
  • The iOS UUID class converts to uppercase string ๐Ÿ’€

There were discussions about choosing one way and enforcing it in the API, but since there were no consensus, we decided to expose the underlying representation, and make sure all id comparisons would be case insensitive (which is a pain since it means user code will have to convert all its UUID string constants before comparing to anything... to be discussed in another issue too).

Most comparisons use the idsAreSame function.

Making it case-insensitive should fix the remaining issues, at least in the API (although maybe not for user code).

onServicesDiscovered An error

undefined is not a function (evaluating 'console.assert("services"in serviceMap||"service"in serviceMap,
"Missing services in service found event")')

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.