Bluetooth in react-native

BluetoothReact Native

Bluetooth Problem Overview


My project requires to receive&send bluetooth signal, and I am pretty new to react-native so which component I can use in react-native for bluetooth related functionality?

Bluetooth Solutions


Solution 1 - Bluetooth

Update (with disclaimer):

Since I gave the original answer, things have changed. In my company (Polidea) since we realised there is no very good library for React Native for BT we developed and fully open-sourced (permissive licence) our own solution. It's based on two RX-based Android and iOS native libraries and we wrote a common React Native wrapper around it so that you can use the same Javascript/RN interface but under the hood native iOS/Android specifics are handled in native code.

The library is here: https://github.com/Polidea/react-native-ble-plx

Disclaimer: I am a Principal Software Engineer and former CTO @Polidea - the company that created the library.

Original answer:

I don't think there is a built-in component for bluetooth access in RN.

If you want to use Bluetooth, you either need to write your own Obj-C API component (as described at https://facebook.github.io/react-native/docs/native-modules-ios.html#content ) or to use some 3rd party components that suit your needs where someone did it already. There are couple of those available (quick google search):

https://github.com/frostney/react-native-bluetooth-state

https://github.com/frostney/react-native-ibeacon

Not sure if they work well, but you can use them as starting point if you need other functionality.

Solution 2 - Bluetooth

Check this package out, a real life saver for me and works on both iOS and Android. React Native BLE Manager you can perform all sorts of bluetooth activities with it: scan, connect, send, enable, disable, disconnect, etc. You should check it out!

Solution 3 - Bluetooth

I created a cross platform Bluetooth/ WIFI framework that allows you to browse and advertise in a manner similar to Apple's Multipeer Connectivity and Google's Nearby Connections. It works between Android and iOS so you can send messages from an iOS device to an Android device and vice versa. Here's a sample of the library which can be found at https://github.com/alexkendall/RCTUnderdark

import {
  NativeModules,
  NativeAppEventEmitter,
} from 'react-native';
import React from 'react';

var NativeManager = NativeModules.NetworkManager

module.exports = {
  // kind can be one of "WIFI", "BT", and "WIFI-BT"
  browse(kind) {
    NativeManager.browse(kind)
  },
  // kind can be one of "WIFI", "BT", and "WIFI-BT"
  advertise(kind) {
    NativeManager.advertise(kind)
  },
  stopAdvertising() {
    NativeManager.stopAdvertising()
  },
  stopBrowsing() {
    NativeManager.stopBrowsing()
  },
  disconnectFromPeer(peerId) {
    NativeManager.disconnectFromPeer(peerId)
  },
  inviteUser(peerId) {
    NativeManager.inviteUser(peerId)
  },
  sendMessage(message, peerId) {
    NativeManager.sendMessage(message, peerId)
  },
  acceptInvitation(peerId) {
    NativeManager.acceptInvitation(peerId)
  },
  getNearbyPeers(callback) {
    NativeManager.getNearbyPeers((peers) => {
      callback(peers)
    })
  },
  getConnectedPeers(callback) {
    NativeManager.getConnectedPeers((peers) => {
      callback(peers)
    })
  },
  /*listener callbacks
  peer contains .id (string), type(string), connected(bool), message(string), display name(string)
  */
  addPeerDetectedListener(callback) {
    NativeAppEventEmitter.addListener(
    'detectedUser',
    (peer) =>  callback(peer)
    );
  },
  addPeerLostListener(callback) {
    NativeAppEventEmitter.addListener(
    'lostUser',
    (peer) => callback(peer)
    );
  },
  addReceivedMessageListener(callback) {
    NativeAppEventEmitter.addListener(
      'messageReceived',
      (peer) => callback(peer)
    );
  },
  addInviteListener(callback) {
    NativeAppEventEmitter.addListener(
      'receivedInvitation',
      (peer) => callback(peer)
    );
  },
  addConnectedListener(callback) {
    NativeAppEventEmitter.addListener(
      'connectedToUser',
      (peer) => callback(peer)
    );
  },
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1522451View Question on Stackoverflow
Solution 1 - BluetoothJarek PotiukView Answer on Stackoverflow
Solution 2 - BluetoothDaniel BardeView Answer on Stackoverflow
Solution 3 - BluetoothAlex HarrisonView Answer on Stackoverflow