GithubHelp home page GithubHelp logo

octavioturra / react-native-alt-beacon Goto Github PK

View Code? Open in Web Editor NEW
34.0 9.0 10.0 1018 KB

A work-in-progress lib to work with AltBeacon and React Native.

License: MIT License

JavaScript 45.50% Java 44.09% Objective-C 10.41%

react-native-alt-beacon's Introduction

RNAAltBeacon

A lib to work with AltBeacon and React Native. You need a valid Bluetooth 4.0 device to run it.

IOS

No donuts for you. I can't make a functional iOs version.

For test purpose, you can install AltBeacon for iOs into your project and add iOs folder content. It doesn't emit signal nor errors..

Android

Can transmit and receive beacon data. You need >4.4.4 Android version to see transmission working.

STEP 0 - Install Package

First of all, install it with npm install -S react-native-alt-beacon.

Step 1 - Update Gradle Settings

// file: android/settings.gradle
...

include ':react-native-alt-beacon'
project(':react-native-alt-beacon').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-alt-beacon/android')

Step 2 - Update Gradle Build

// file: android/app/build.gradle
...

dependencies {
    ...
    compile project(':react-native-alt-beacon')
}

Step 3 - Target version minimum to 21

// file: android/app/build.gradle

defaultConfig {
    ...
    minSdkVersion 21
    ...
}

Step 4 - Register React Package and Handle onActivityResult

...
import br.com.fraguto.rnabeacon.RNABeaconPackage; // <--- import

public class MainActivity extends ReactActivity {

    ...

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new RNABeaconPackage() // <------ add the package
        );
    }

    ...
}

Use:

For using it you must instance RNABeacon() or use the Legacy static one (deprecated);

  const beacon = new RNABeacon();

Methods:

Transmit:

I can't make this work in my example. So, use in your own risk.

checkTransmissionSupported() : Promise

checkTransmissionSupported(callback:Function) : void

Checks if transmission is supported for now in the device.

  beacon
    .checkTransmissionSupported()
    .then(() => {/*...success*/} )
    .catch((errorCode) => console.log(beacon.errors[errorCode]));

Errors

It is an integer array with error codes and its description.

  // can found in the "errors" property or translated into:
  errors = {
    1: 'NOT_SUPPORTED_MIN_SDK',
    2: 'NOT_SUPPORTED_BLE',
    3: 'DEPRECATED_NOT_SUPPORTED_MULTIPLE_ADVERTISEMENTS',
    4: 'NOT_SUPPORTED_CANNOT_GET_ADVERTISER',
    5: 'NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS'
  };

(deprecated) static startTransmitting(uuid:String, params:Object, onSuccess:Function, onError:Function) : void

transmit(uuid:String, params:Object, onSuccess:Function, onError:Function) : void

transmit(uuid:String, params:Object) : Promise

Starts transmitting UUID region beacon with parameters. UUID must be a 32 bit string. Eg. "F234454-CF6D-4A0F-ADF2-F4911BA9FFA6"

  beacon.transmit("F234454-CF6D-4A0F-ADF2-F4911BA9FFA6", {
    minor: "1", //of numerical one bit content
    major: "2", //of numerical one bit content
    manufacturer: 0x0118 //as in AltBeacon example
    data: [] //array of integers
  })
    .then(()=> /* success */)
    .catch((err) => console.log(err));

stopTransmitting(onSuccess:Function, onError:Function) : void

stopTransmitting() : Promise

Stops beacon transmission.

  beacon.stopTransmitting()
    .then(()=> /* success */)
    .catch((err) => console.log(err));

Monitoring:

static startMonitoring(uuid:String) : void (deprecated)

startMonitoring(uuid:String) : Promise

startMonitoring(uuid:String, onSuccess: Function, onError: Function) : void

Start monitoring for beacon regions.

  beacon.startMonitoring("F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
    .then(()=> /* success */)
    .catch((err) => console.log(err));

As you start monitoring, you must listen for the given events:

  beacon.on("didEnterRegion", (data)=> {
    /*
    data = {
      'uuid': String,
      'minor': String,
      'major': String
    }
    */
  });
  beacon.on("didExitRegion", ()=> {
    /*
    success
    */
  });
  beacon.on("startMonitoring", ()=> { //deprecated
    /*
    success
    */
  })

stopMonitoring() : Promise

It stops all monitors, then emits a didExitRegion event.

  beacon.stopMonitoring()
    .then(()=> /* success */)
    .catch((err) => console.log(err));
  beacon.on("didExitRegion", ()=> {
    /*
    success
    */
  });

Ranging:

static startRanging(uuid:String) : void (deprecated)

startRanging(uuid:String) : Promise

startRanging(uuid:String, onSuccess: Function, onError: Function) : void

Start ranging for beacon near the the device within its UUID.

  beacon.startRanging("F234454-CF6D-4A0F-ADF2-F4911BA9FFA6")
    .then(()=> /* success */)
    .catch((err) => console.log(err));

As you start ranging, you must listen for the given events:

  beacon.on("didFoundBeacons", (data)=> {
    /*
    data = [{
      'uuid': String,
      'minor': String,
      'major': String,
      'distance': Double //near in meters
    }, ...]
    */
  });
  beacon.on("didNotFoundBeacons", ()=> {
    /*
    success
    */
  });
  beacon.on("startRanging", ()=> { //deprecated
    /*
    success
    */
  })

stopRanging() : Promise

It stops all ranging monitors, then emits a didNotFoundBeacons event.

  beacon.stopRanging()
    .then(()=> /* success */)
    .catch((err) => console.log(err));
  beacon.on("didNotFoundBeacons", ()=> {
    /*
    success
    */
  });

Example

To view the example running, simple run npm install in example folder, then react-native run-android.

Remember that you need to run in a android device, because there is no BLE functionality in the emulator.

LICENSE

Copyright (c) 2015 Octavio Turra Barbosa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

react-native-alt-beacon's People

Contributors

andrekovac avatar octavioturra 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-alt-beacon's Issues

Error when calling stopTransmitting

Hey.
When I call stopTransmitting function to stop beacon transmitting, an error indicating undefined reference for success variable keeps showing up.
Any help would do. Thanks.

Why does minSdkVersion have to be 21?

How did you find out about this restriction? Via trial and error? Or is there some documentation that react-native only supports BLE-signals from SDK version 21, i.e. for Android version 5.0 and up?

Ranging: Array of beacons is empty

When ranging for beacons, the array of beacons (last line in the code excerpt from RNABeacon.java below) is always empty. I tested with the exact same BeaconLayout and Configuration in a small Android dummy app. There the array has data and I can successfully range the beacons.

To test the package I added two more events (see the code excerpt below) . The new events didServiceConnect and didRangeBeaconsInRegionEnter do get called, but because the array is zero didFoundBeacons is never called, but didNotFoundBeacons is called instead.

Any thoughts?

private BeaconConsumer rangingConsumer = new BeaconConsumer() {
        @Override
        public void onBeaconServiceConnect() {
            sendEvent(context, "didServiceConnect", null);
            beaconManager.setRangeNotifier(new RangeNotifier() {
                @Override
                public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                    sendEvent(context, "didRangeBeaconsInRegionEnter", null);
                    WritableMap map = new WritableNativeMap();
                    map.putString("uuid", region.getUniqueId());

                    if (beacons.size() > 0) {

Doesn't work !

Hi Octavioturra, thank for your plugin but i have a problem !

When i run this cmd 'react-native run-android' i have this error :

[...]/android/app/src/main/java/com/project/MainApplication.java:31: error: constructor RNABeaconPackage in class RNABeaconPackage cannot be applied to given types;
new RNABeaconPackage()
^
required: Context
found: no arguments
reason: actual and formal argument lists differ in length
1 error
:app:compileDebugJavaWithJavac FAILED

Do you have an idea?

Thank you

i success run your example,but no auto open bluetooth.

Example won't run

When running react-native run-android after npm install in the example\ folder after cloning the rep, I get the following error:

Android project not found. Maybe run react-native android first?

When pulling the project from npm, the src/ folder in the example/ folder is not consistent with the repo here on Github. For example the files Button.js and List.js are missing.

That might be related to the npm difficulties you mentioned in issue #4 .

Update NPM repos

Hey wonderful project but I was wondering if you could update the npm repos so that we could easily get version 1.1.1 as currently the only viable one is 1.0.2 which will not compile with the directions provided nor follow the docs

App crashes while stopping monitoring

Hi. First thanks for writing this library.
I am using this library for detecting and making AltBeacon formatted beacon. I have kept the flow as -

  1. Start monitoring/ranging on pressing button.
  2. Look for any devices nearby(look for proper events). Do something.
  3. And stop monitoring on pressing button.

While stopping monitoring, the app just crashes. I don't get anything on the remote-debugger.
Any help would do.

Also, this error - "folly::toJson: JSON object value was a Nan or INF", also keeps on crashing the app pretty much randomly.

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.