Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

monoflow-ayvu/react-native-beacon-scanner

Repository files navigation

react-native-beacon-scanner

DEPRECATED

The minew SDK proved to be too unreliable to be used in serious app, and not just as a scanning app.

I'm sorry to inform you this SDK is deprecated.

I recommend you to check out react-native-ble-plx and pair it with advlib (specially advlib-ble-services, since it contains several of minew's data frames)

NOTE

This scanner only scans for Minew beacons. Please keep that in mind.

All frames provided by Minew's documentation are implemented.

How to Install (Android)

  • Add the following to AndroidManifest.xml:
  <service android:name="com.minew.beaconplus.sdk.ConnectService"/>
  <service android:name="com.minew.beaconplus.sdk.services.DfuService"/>
  <receiver android:name="com.minew.beaconplus.sdk.receivers.BluetoothChangedReceiver"
      android:exported="true">
      <intent-filter>
          <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
      </intent-filter>
  </receiver>

How to use

  1. First you'll need to ensure the permission for bluetooth and fine location are given.
import { PermissionsAndroid } from 'react-native'

export async function requestLocationPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'Example App',
        message: 'Example App access to your location ',
        buttonPositive: 'OK',
        buttonNegative: 'Cancel',
        buttonNeutral: 'Ask Me Later',
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      return true
    } else {
      return false
    }
  } catch (err) {
    console.warn(err)
    return false
  }
}
  1. Then subscribe to beacon events from the lib
import {setBluetoothState} from 'react-native-beacon-scanner'

const scanner = onBeaconScan((beacons) => {
  console.info('Beacons found: ', beacons.length)
  console.info(JSON.stringify(beacons, undefined, 2))
})

// Do not forget to clear subscription when not needed anymore
scanner.remove()
  1. Finally, make sure bluetooth is ON and request the library to start scanning:
import {
  setBluetoothState,
  start,
  stop,
} from 'react-native-beacon-scanner'

requestLocationPermission()
  .then(async (granted) => {
    if (granted) {
      console.log('ensuring bluetooth is on...')
      await setBluetoothState(true)
    } else {
      throw new Error('bluetooth permission not granted')
    }
  })
  // scan starts here
  .then(() => start())
  // here on we already have started scanning
  .then(() => console.info('scan started!'))
  // oops! Something happened
  .catch((err) => console.error('error', err))

See the example folder for a working demo.