Git Product home page Git Product logo

react-native-google-nearby-connection's Introduction

react-native-google-nearby-connection

React Native wrapper for Googles Nearby Connection API

Install

Install package

npm i react-native-google-nearby-connection --save

Android

  • Make sure you are using Gradle 2.2.x (project build.gradle)
  • Add google-services
buildscript {
    ...
    dependencies {
        ...
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.google.gms:google-services:3.1.2'
        ...
    }
    ...
}
  • Add the following to your build.gradle's repositories section. (project build.gradle)
allprojects {
    repositories {
        ...
        maven { url "https://maven.google.com" }
        ...
    }
}

app:build.gradle

Add project under dependencies

android {
    ...
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    ...
    defaultConfig {
        ...
        minSdkVersion 16
        targetSdkVersion 26
        ...
        multiDexEnabled true
        ...
    }
...
dependencies {
    ...
	compile 'com.google.android.gms:play-services-nearby:11.8.0'
    compile project(':react-native-google-nearby-connection')
	compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:multidex:1.0.1'
    ...
}

settings.gradle

Include project, so gradle knows where to find the project

include ':react-native-google-nearby-connection'
project(':react-native-google-nearby-connection').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-google-nearby-connection/android')

MainApplication.java

We need to register our package

Add import com.butchmarshall.reactnative.google.nearby.connection.NearbyConnectionPackage; as an import statement and new NearbyConnectionPackage() in getPackages()

Usage

Import library

import NearbyConnection from 'react-native-google-nearby-connection';

Starting the discovery service

NearbyConnection.startDiscovering();

Stopping the discovery service

NearbyConnection.stopDiscovering();

Starting the advertising service

NearbyConnection.startAdvertising();

Stopping the advertising service

NearbyConnection.stopAdvertising();

Open the microphone and broadcast audio to an endpoint

NearbyConnection.openMicrophone(endpointId);

Stop broadcasting audio to an endpoint

NearbyConnection.closeMicrophone(endpointId);

Start playing an audio stream from a received payload

NearbyConnection.startPlayingAudioStream(endpointId);

Stop playing an audio stream from a received payload

NearbyConnection.stopPlayingAudioStream(endpointId);

Callbacks

Endpoint Discovery

NearbyConnection.onDiscoveryStarting(() => {
	// Discovery services is starting
});

NearbyConnection.onDiscoveryStarted(() => {
	// Discovery services has started
});

NearbyConnection.onDiscoveryStartFailed(() => {
	// Failed to start discovery service
});

!!! BROKEN - I cannot figure out why this event never fires
NearbyConnection.onEndpointLost(() => {
	// Endpoint moved out of range or disconnected
});

Endpoint Advertisement

NearbyConnection.onAdvertisingStarting(() => {
	// Advertising service is starting
});

NearbyConnection.onAdvertisingStarted(() => {
	// Advertising service has started
});

NearbyConnection.onAdvertisingStartFailed(() => {
	// Failed to start advertising service
});

Connection negotiation

NearbyConnection.onConnectionInitiatedToEndpoint(({
	endpointId,             // ID of the endpoint wishing to connect
	authenticationToken,    // A small symmetrical token that has been given to both devices.
	endpointName,           // The name of the remote device we're connecting to.
	incomingConnection      // True if the connection request was initated from a remote device.
}) => {
	// Connection has been initated
});

NearbyConnection.onConnectedToEndpoint((endpointId) => {
	// Succesful connection to an endpoint established
});

NearbyConnection.onEndpointConnectionFailed(({
	endpointId,             // ID of the endpoint we failed to connect to
	statusCode              // The status of the response [See CommonStatusCodes](https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes)
}) => {
	// Failed to connect to an endpoint
});

NearbyConnection.onDisconnectedFromEndpoint((endpointId) => {
	// Disconnected from an endpoint
});

Payload Status

Nearby.onReceivePayload(({
	endpointId,             // ID of the endpoint we got the payload from
	payloadType,            // The type of this payload (File or a Stream) [See Payload](https://developers.google.com/android/reference/com/google/android/gms/nearby/connection/Payload)
	payloadId               // Unique identifier of the payload
}) => {
	// Payload has been received
});

Nearby.onPayloadUpdate(({
	bytesTransferred,       // Bytes transfered so far
	totalBytes,             // Total bytes to transfer
	payloadId,              // Unique identifier of the payload
	payloadStatus,          // [See PayloadTransferUpdate.Status](https://developers.google.com/android/reference/com/google/android/gms/nearby/connection/PayloadTransferUpdate.Status)
	payloadHashCode,        // ???
}) => {
	// Update on a previously received payload
});

react-native-google-nearby-connection's People

Contributors

butchmarshall avatar

Watchers

 avatar  avatar

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.