Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
S-AT committed Nov 28, 2019
1 parent 06cadab commit 0cc73fd
Show file tree
Hide file tree
Showing 68 changed files with 3,285 additions and 0 deletions.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "cordova-plugin-google-nearby",
"version": "1.1.6",
"description": "This plugin adds support for the [Google Nearby Messages API](https://developers.google.com/nearby/messages/overview). Support for the other Nearby APIs will follow.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/S-AT/cordova-plugin-gns"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/S-AT/cordova-plugin-gns/issues"
},
"homepage": "https://github.com/S-AT/cordova-plugin-gns#readme",
"keywords": [
"ecosystem:cordova",
"cordova-android"
]

}
95 changes: 95 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
id="cordova-plugin-google-nearby"
version="1.1.6">

<name>cordova-plugin-google-nearby</name>
<preference name="API_KEY" />

<engines>
<engine name="cordova" version=">=3.4.0"/>
</engines>

<asset src="www/nearby.js" target="js/nearby.js"/>

<js-module src="www/nearby.js" name="nearby">
<clobbers target="nearby" />
</js-module>

<platform name="android">
<framework src="com.google.android.gms:play-services-nearby:17.0.0" />

<config-file target="res/xml/config.xml" parent="/*">
<feature name="NearbyPlugin">
<param name="android-package" value="org.apache.cordova.NearbyPlugin"/>
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest/application">
<meta-data
android:name="com.google.android.nearby.messages.API_KEY"
android:value="$API_KEY" />
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</config-file>

<source-file src="src/android/NearbyPlugin.java" target-dir="src/org/apache/nearby/"/>
</platform>

<platform name="ios">
<config-file target="config.xml" parent="/*">
<feature name="NearbyPlugin">
<param name="ios-package" value="CDVNearbyPlugin" />
</feature>
</config-file>
<header-file src="src/ios/CDVNearbyPlugin.h" />
<source-file src="src/ios/CDVNearbyPlugin.m" />
<framework src="src/ios/GoogleInterchangeUtilities.framework" custom="true" />
<framework src="src/ios/GoogleNetworkingUtilities.framework" custom="true" />
<framework src="src/ios/GoogleSymbolUtilities.framework" custom="true" />
<framework src="src/ios/GoogleUtilities.framework" custom="true" />

<resource-file src="src/ios/NearbyMessages/Resources/resources/GNSSharedResources.bundle" />
<resource-file src="src/ios/NearbyMessages/Resources/resources/ic_expand_more.xcassets" />
<resource-file src="src/ios/NearbyMessages/Resources/resources/ic_nearby_48pt.xcassets" />

<source-file src="src/ios/NearbyMessages/Libraries/libGNSMessages.a" framework="true" />

<header-file src="src/ios/NearbyMessages/Sources/GNSBeaconStrategy.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSError.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSMessage.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSMessageManager.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSMessages.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSOperationStatus.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSPermission.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSPublicationParams.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSStrategy.h" />
<header-file src="src/ios/NearbyMessages/Sources/GNSSubscriptionParams.h" />

<framework src="Accelerate.framework" />
<framework src="AddressBook.framework" />
<framework src="AudioToolbox.framework" />
<framework src="AVFoundation.framework" />
<framework src="CoreBluetooth.framework" />
<framework src="CoreGraphics.framework" />
<framework src="CoreLocation.framework" />
<framework src="CoreMedia.framework" />
<framework src="CoreText.framework" />
<framework src="MediaPlayer.framework" />
<framework src="libc++.tbd" />
<framework src="libz.tbd" />

<config-file target="*-Info.plist" parent="NearbyApiKey">
<string>$API_KEY</string>
</config-file>
</platform>

</plugin>
160 changes: 160 additions & 0 deletions src/android/NearbyPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.apache.cordova;
import android.util.Log;
import org.apache.cordova.*;
import org.apache.cordova.PluginResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.Uri;
import com.google.android.gms.nearby.Nearby;

import com.google.android.gms.nearby.messages.Message;
import com.google.android.gms.nearby.messages.Strategy;
import com.google.android.gms.nearby.messages.MessageListener;
import com.google.android.gms.nearby.messages.Messages;
import com.google.android.gms.nearby.messages.PublishOptions;
import com.google.android.gms.nearby.messages.SubscribeOptions;
import com.google.android.gms.nearby.messages.PublishCallback;
import com.google.android.gms.nearby.messages.SubscribeCallback;
import com.google.android.gms.tasks.OnFailureListener;

public class NearbyPlugin extends CordovaPlugin {
private static final String TAG = "NearbyPlugin";
private static CallbackContext publish_callback = null;
private static CallbackContext unpublish_callback = null;
private static CallbackContext subscribe_callback = null;
private static CallbackContext unsubscribe_callback = null;
private static Message published_message = null;
private static final int REQUEST_RESOLVE_ERROR = 1001;
MessageListener mMessageListener = new MessageListener() {
@Override
public void onFound(Message message) {
String found_message = new String(message.getContent());
Log.d(TAG, "found messsage: " + found_message);
PluginResult result = new PluginResult(PluginResult.Status.OK, found_message);
result.setKeepCallback(true);

if(NearbyPlugin.this.subscribe_callback != null)
NearbyPlugin.this.subscribe_callback.sendPluginResult(result);
}

@Override
public void onLost(Message message) {
Log.d(TAG, "message lost: " + message);
}
};
OnFailureListener failListener = new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, e.getMessage());
}
};

@Override
public boolean execute(String action, final String message, final CallbackContext callbackContext) throws JSONException {
if (action.equals("subscribe")) {
this.subscribe_callback = callbackContext;
this.subscribe();
PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
pluginResult.setKeepCallback(true);
this.subscribe_callback.sendPluginResult(pluginResult);
return true;
} else if (action.equals("unsubscribe")) {
this.unsubscribe_callback = callbackContext;
this.unsubscribe();
return true;
} else if (action.equals("publish")) {
this.publish_callback = callbackContext;
this.publish(message);
return true;
} else if (action.equals("unpublish")) {
this.unpublish_callback = callbackContext;
this.unpublish();
return true;
} else {
return false;
}
}

// Subscribe to receive messages.
private void subscribe() {

unsubscribe();

SubscribeCallback callback = new SubscribeCallback() {
@Override
public void onExpired() {
Log.d(TAG, "subscribtion expired");
}
};

Strategy strategy = new Strategy.Builder()
.setDiscoveryMode(Strategy.DISCOVERY_MODE_DEFAULT)
.setDistanceType(Strategy.DISTANCE_TYPE_DEFAULT)
.setTtlSeconds(Strategy.TTL_SECONDS_INFINITE)
.build();

SubscribeOptions options = new SubscribeOptions.Builder()
.setCallback(callback)
.setStrategy(strategy)
.build();

Nearby.getMessagesClient(cordova.getActivity()).subscribe(NearbyPlugin.this.mMessageListener, options).addOnFailureListener(this.failListener);
Log.d(TAG, "subscribed successfully");
}

private void publish(String message) {

unpublish();

PublishCallback callback = new PublishCallback() {
@Override
public void onExpired() {
Log.d(TAG, "publish expired");
}
};

Strategy strategy = new Strategy.Builder()
.setDiscoveryMode(Strategy.DISCOVERY_MODE_DEFAULT)
.setDistanceType(Strategy.DISTANCE_TYPE_DEFAULT)
.setTtlSeconds(300) // Strategy.TTL_SECONDS_MAX)
.build();

PublishOptions options = new PublishOptions.Builder()
.setCallback(callback)
.setStrategy(strategy)
.build();

Message mActiveMessage = new Message(message.getBytes());
Nearby.getMessagesClient(cordova.getActivity()).publish(mActiveMessage, options).addOnFailureListener(this.failListener);
Log.d(TAG, "published message: " + message);
this.published_message = mActiveMessage;

if(this.publish_callback != null)
this.publish_callback.success("published message");
}

private void unsubscribe() {
Nearby.getMessagesClient(cordova.getActivity()).unsubscribe(this.mMessageListener);
Log.d(TAG, "unsubscribed");

if(this.unsubscribe_callback != null)
this.unsubscribe_callback.success("unsubscribed");
}

private void unpublish() {
Message msg = this.published_message;

if(msg == null) {
Log.d(TAG, "no message to unpublish");
} else {
Nearby.getMessagesClient(cordova.getActivity()).unpublish(this.published_message);
Log.d(TAG, "unpublished");
}

if(this.unpublish_callback != null)
this.unpublish_callback.success("unpublished");
}
}
19 changes: 19 additions & 0 deletions src/ios/CDVNearbyPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// CDVNearbyPlugin.h
// nearby2
//
// Created by Fábio Akira Yoshida on 19/07/18.
// Copyright © 2018 Fábio Akira Yoshida. All rights reserved.
//

#import <Cordova/CDV.h>
#import <Foundation/Foundation.h>

@interface CDVNearbyPlugin : CDVPlugin

- (void)subscribe:(CDVInvokedUrlCommand*)command;
- (void)unsubscribe:(CDVInvokedUrlCommand*)command;
- (void)publish:(CDVInvokedUrlCommand*)command;
- (void)unpublish:(CDVInvokedUrlCommand*)command;

@end
74 changes: 74 additions & 0 deletions src/ios/CDVNearbyPlugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// NearbyPlugin.m
//
//
// Created by Bruno Rigo Werminghoff on 19/07/18.
//

#import "CDVNearbyPlugin.h"
#import "GNSMessages.h"

@interface CDVNearbyPlugin ()

@property(strong) NSString* subscribeCallbackId;
@property(strong) NSString* unsubscribeCallbackId;
@property(strong) GNSMessageManager* messageManager;
@property(strong) id<GNSSubscription> subscription;
@property(strong) id<GNSPublication> publication;
@end

@implementation CDVNearbyPlugin

- (void)pluginInitialize {
[GNSMessageManager setDebugLoggingEnabled:YES];
NSString* apiKey = [[NSBundle mainBundle] objectForInfoDictionaryKey: @"NearbyApiKey"];
self.messageManager = [[GNSMessageManager alloc] initWithAPIKey:apiKey];
}

- (void)publish:(CDVInvokedUrlCommand*)command {
NSString* parameter = [command.arguments objectAtIndex:0];
// NSString* parameter = command.arguments;

NSData* publishedData = [parameter dataUsingEncoding:NSUTF8StringEncoding];
GNSMessage* message = [GNSMessage messageWithContent: publishedData];
self.publication = [self.messageManager publicationWithMessage:message];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)unpublish:(CDVInvokedUrlCommand*)command {
self.publication = nil;

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:true];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void)subscribe:(CDVInvokedUrlCommand*)command {
self.subscribeCallbackId = command.callbackId;

self.subscription = [self.messageManager subscriptionWithMessageFoundHandler:^(GNSMessage *message) {
NSString* messageString = [[NSString alloc] initWithData:message.content encoding:NSUTF8StringEncoding];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:messageString]; // success callback
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.subscribeCallbackId];

} messageLostHandler:^(GNSMessage *message) {
// Remove the name from the list (?)
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR]; // error callback
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.subscribeCallbackId];
}];

// CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; // success callback
// [pluginResult setKeepCallbackAsBool: true];
// [self.commandDelegate sendPluginResult:pluginResult callbackId:self.subscribeCallbackId];
}

- (void)unsubscribe:(CDVInvokedUrlCommand*)command {
self.subscription = nil;

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; // success callback
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

@end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added src/ios/GoogleUtilities.framework/GoogleUtilities
Binary file not shown.
Loading

0 comments on commit 0cc73fd

Please sign in to comment.