From 6d4f7087229c1379ecb2e551e4db49576a67753a Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Sat, 11 May 2019 02:48:45 +0530 Subject: [PATCH] added askpermission and partial startAdvertising methods --- .../NearbyConnectionsPlugin.java | 78 ++++++++++++++++++- .../android/app/src/main/AndroidManifest.xml | 8 +- example/lib/main.dart | 23 +++++- example/pubspec.yaml | 1 - lib/nearby_connections.dart | 36 +++++++++ 5 files changed, 141 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java index 6a40035..e2a4359 100644 --- a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java +++ b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java @@ -7,14 +7,26 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.provider.Settings; +import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.util.Log; +import com.google.android.gms.nearby.Nearby; +import com.google.android.gms.nearby.connection.AdvertisingOptions; +import com.google.android.gms.nearby.connection.ConnectionInfo; +import com.google.android.gms.nearby.connection.ConnectionLifecycleCallback; +import com.google.android.gms.nearby.connection.ConnectionResolution; +import com.google.android.gms.nearby.connection.PayloadTransferUpdate; +import com.google.android.gms.nearby.connection.Strategy; +import com.google.android.gms.tasks.OnFailureListener; +import com.google.android.gms.tasks.OnSuccessListener; + import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; +import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry.Registrar; /** @@ -22,6 +34,8 @@ import io.flutter.plugin.common.PluginRegistry.Registrar; */ public class NearbyConnectionsPlugin implements MethodCallHandler { private Activity activity; + private static final String SERVICE_ID = "PKMNAPPS_NEARBY_CONNECTION"; + private NearbyConnectionsPlugin(Activity activity) { this.activity = activity; @@ -38,7 +52,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { } @Override - public void onMethodCall(MethodCall call, Result result) { + public void onMethodCall(MethodCall call, final Result result) { switch (call.method) { case "checkPermissions": @@ -49,10 +63,72 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { } else { result.success(true); } + break; + case "askPermissions": + ActivityCompat.requestPermissions(activity, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, + 0); + result.success(null); + break; + case "stopAdvertising": + Nearby.getConnectionsClient(activity).stopAdvertising(); + result.success(null); + break; + case "startAdvertising": + String userNickName = (String) call.argument("userNickName"); + int strategy = (int) call.argument("strategy"); + + assert userNickName != null; + Nearby.getConnectionsClient(activity).startAdvertising( + userNickName, + SERVICE_ID, + connectionLifecycleCallback, + new AdvertisingOptions.Builder().setStrategy(getStrategy(strategy)).build()) + .addOnSuccessListener(new OnSuccessListener() { + @Override + public void onSuccess(Void aVoid) { + result.success(true); + } + }) + .addOnFailureListener(new OnFailureListener() { + @Override + public void onFailure(@NonNull Exception e) { + result.error("Failure", e.getMessage(), null); + } + }); + break; default: result.notImplemented(); } + } + private final ConnectionLifecycleCallback connectionLifecycleCallback = new ConnectionLifecycleCallback() { + @Override + public void onConnectionInitiated(@NonNull String s, @NonNull ConnectionInfo connectionInfo) { + + } + + @Override + public void onConnectionResult(@NonNull String s, @NonNull ConnectionResolution connectionResolution) { + } + + @Override + public void onDisconnected(@NonNull String s) { + + } + }; + + private Strategy getStrategy(int strategy) { + switch (strategy) { + case 0: + return Strategy.P2P_CLUSTER; + case 1: + return Strategy.P2P_STAR; + case 2: + return Strategy.P2P_POINT_TO_POINT; + default: + return Strategy.P2P_CLUSTER; + } } } diff --git a/example/android/app/src/main/AndroidManifest.xml b/example/android/app/src/main/AndroidManifest.xml index dbc751e..da157b9 100644 --- a/example/android/app/src/main/AndroidManifest.xml +++ b/example/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,12 @@ - + + + + + + +