Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth in react-native

People also ask

Can you use Bluetooth with react native?

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!

Can I use Bluetooth with Expo?

Expo Go does not support 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.


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!


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)
    );
  },
}