This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

react-native-beacon-scanner
TypeScript icon, indicating that this package has built-in type declarations

1.8.0 • Public • Published

react-native-beacon-scanner

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.

Dependents (0)

Package Sidebar

Install

npm i react-native-beacon-scanner

Weekly Downloads

70

Version

1.8.0

License

MIT

Unpacked Size

174 kB

Total Files

28

Last publish

Collaborators

  • fermuch