badfeatures / react-native-nearby-api

React Native wrapper around the Google Nearby Messages API SDK
Other
33 stars 23 forks source link
google-nearby nearby react-native

react-native-nearby-api npm version

Getting started

$ yarn add react-native-nearby-api or $ npm install react-native-nearby-api --save

Mostly automatic installation

$ react-native link react-native-nearby-api

Manual installation

iOS

Android

  1. Open up android/app/src/main/java/[...]/MainActivity.java
  1. Append the following lines to android/settings.gradle:
    include ':react-native-nearby-api'
    project(':react-native-nearby-api').projectDir = new File(rootProject.projectDir,    '../node_modules/react-native-nearby-api/android')
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
     compile project(':react-native-nearby-api')

Usage

See the example app for more detail and code examples.

import NearbyApi from "react-native-nearby-api";

const nearbyAPI = new NearbyAPI(true); // Use BLE only, no audio.
nearbyAPI.onConnected(message => {
  console.log(message);
});
nearbyAPI.onDisconnected(message => {
  console.log(message);
});
nearbyAPI.onFound(message => {
  console.log("Message Found!");
  console.log(message);
});
nearbyAPI.onLost(message => {
  console.log("Message Lost!");
  console.log(message);
});
// Android Only
nearbyAPI.onDistanceChanged((message, value) => {
  console.log("Distance Changed!");
  console.log(message, value);
});
// Android Only
nearbyAPI.onBLESignalChanged((message, value) => {
  console.log("BLE Signal Changed!");
  console.log(message, value);
})
nearbyAPI.onPublishSuccess(message => {
  console.log(message);
});
nearbyAPI.onPublishFailed(message => {
  console.log(message);
});
nearbyAPI.onSubscribeSuccess(() => {});
nearbyAPI.onSubscribeFailed(() => {});

// To connect from Google API Client
nearbyAPI.connect(API_KEY);

// To check if the nearby API is connected.
nearbyAPI.isConnected((connected, error) => {
  console.log(connected);
});

// To disconnect later
nearbyAPI.disconnect();

// To publish to nearby devices
nearbyAPI.publish("Hello World!");

// To check if the nearby API is publishing.
nearbyAPI.isPublishing((publishing, error) => {
  console.log(publishing);
});

// To subscribe to nearby devices broadcasting
nearbyAPI.subscribe();

// To check if the nearby API is subscribing.
nearbyAPI.isSubscribing((subscribing, error) => {
  console.log(subscribing);
});

// To unpublish
nearbyAPI.unpublish();

// To unsubscribe
nearbyAPI.unsubscribe();

Running the Example

Generate an API Key from the Google Developer Console

  1. Go to the Google Developers Console.
  2. Create or select a project to register your application with.
  3. Click Continue to Enable the API.
  4. On the Credentials page, create a new API Key. (No key restrictions are needed for this example)
  5. Copy/Paste your key in example/index.js and in the example AndroidManifest.xml

Android

yarn start --projectRoots <FULL-PATH-TO-REPO>/react-native-nearby-api/example,<FULL-PATH-TO-REPO>/react-native-nearby-api

yarn run:android

adb reverse tcp:8081 tcp:8081

iOS

  jsCodeLocation = [NSURL URLWithString:@"http://<IP-ADDRESS>:8081/index.bundle?platform=ios&dev=true"];