$ yarn add react-native-nearby-api
or $ npm install react-native-nearby-api --save
$ react-native link react-native-nearby-api
NearbyMessages
need to be linked to the project. Look to the example for Cocoapods usage.
cd ios/
pod init
Podfile
in the project's target.
pod 'NearbyMessages'
pod install
to install dependencies..xcworkspace
.node_modules/react-native-nearby-api/ios
, drag and drop RNNearbyApi.xcodeproj
into your Libraries
group.libRNNearbyApi.a
.android/app/src/main/java/[...]/MainActivity.java
import com.badfeatures.nearby.RNNearbyApiPackage;
to the imports at the top of the filenew RNNearbyApiPackage()
to the list returned by the getPackages()
methodandroid/settings.gradle
:
include ':react-native-nearby-api'
project(':react-native-nearby-api').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-nearby-api/android')
android/app/build.gradle
:
compile project(':react-native-nearby-api')
See the example app for more detail and code examples.
Retrieve your API Keys from the Google Console iOS | Android
Add the correct permissions to the AndroidManifest.
<!-- Include if wish to use BLE -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!-- Include if wish to use audio -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Add the Android API Key in the AndroidManifest .
<meta-data
android:name="com.google.android.nearby.messages.API_KEY"
android:value="MY_API_KEY" />
The iOS API key will be supplied through the connect()
method.
Add NSBluetoothPeripheralUsageDescription
to the Info.plist
Add NSMicrophoneUsageDescription
to the iOS project's Info.plist if using audio
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();
Install the dependencies in the root folder
yarn
projectRoots
reordered
for the example/ directory. In another terminal window: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
cd example ios/
bundle exec pod install
example.xcworkspace
AppDelegate.m
jsCodeLocation = [NSURL URLWithString:@"http://<IP-ADDRESS>:8081/index.bundle?platform=ios&dev=true"];
yarn start
Run on device
NOTE: If you receive a duplicate symbols for architecture
error through Xcode, this is a known issue. A work around is to run ./renameLogger.sh
after node_modules
have been installed and you've attempted to run in Xcode.