added askpermission and partial startAdvertising methods

This commit is contained in:
Prerak Mann 2019-05-11 02:48:45 +05:30
parent 6ee7aba035
commit 6d4f708722
5 changed files with 141 additions and 5 deletions

View File

@ -7,14 +7,26 @@ import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log; 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.MethodCall;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar; import io.flutter.plugin.common.PluginRegistry.Registrar;
/** /**
@ -22,6 +34,8 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
*/ */
public class NearbyConnectionsPlugin implements MethodCallHandler { public class NearbyConnectionsPlugin implements MethodCallHandler {
private Activity activity; private Activity activity;
private static final String SERVICE_ID = "PKMNAPPS_NEARBY_CONNECTION";
private NearbyConnectionsPlugin(Activity activity) { private NearbyConnectionsPlugin(Activity activity) {
this.activity = activity; this.activity = activity;
@ -38,7 +52,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
} }
@Override @Override
public void onMethodCall(MethodCall call, Result result) { public void onMethodCall(MethodCall call, final Result result) {
switch (call.method) { switch (call.method) {
case "checkPermissions": case "checkPermissions":
@ -49,10 +63,72 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
} else { } else {
result.success(true); 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<Void>() {
@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; break;
default: default:
result.notImplemented(); 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;
}
}
} }

View File

@ -1,6 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pkmnapps.nearby_connections_example"> package="com.pkmnapps.nearby_connections_example">
<!-- Required for Nearby Connections -->
<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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that <!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method. calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide In most cases you can leave this as-is, but you if you want to provide

View File

@ -52,8 +52,27 @@ class _MyBodyState extends State<Body> {
RaisedButton( RaisedButton(
child: Text("askPermission(permission handler)"), child: Text("askPermission(permission handler)"),
onPressed: () async { onPressed: () async {
// await PermissionHandler() await Nearby.instance.askPermission();
// .requestPermissions([PermissionGroup.location]); },
),
RaisedButton(
child: Text("Start Advertising"),
onPressed: () async {
try {
bool a = await Nearby.instance
.startAdvertising("pkmn", STRATEGY.P2P_STAR);
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text(a.toString())));
} catch (exception) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(exception.toString())));
}
},
),
RaisedButton(
child: Text("Stop Advertising"),
onPressed: () async {
await Nearby.instance.stopAdvertising();
}, },
), ),
], ],

View File

@ -8,7 +8,6 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2 cupertino_icons: ^0.1.2

View File

@ -2,6 +2,9 @@ import 'dart:async';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
enum STRATEGY { P2P_CLUSTER, P2P_STAR, P2P_POINT_TO_POINT }
enum Status { NONE, SUCCESS, FAILURE, IN_PROGRESS, CANCELED }
class Nearby { class Nearby {
//for maintaining only 1 instance of this class //for maintaining only 1 instance of this class
static final Nearby _instance = Nearby._(); static final Nearby _instance = Nearby._();
@ -14,4 +17,37 @@ class Nearby {
Future<bool> checkPermissions() async => await _channel.invokeMethod( Future<bool> checkPermissions() async => await _channel.invokeMethod(
'checkPermissions', 'checkPermissions',
); );
Future<void> askPermission() async {
await _channel.invokeMethod(
'askPermissions',
);
}
Future<bool> startAdvertising(String userNickName, STRATEGY strategy) async {
assert(userNickName != null && strategy != null);
return await _channel.invokeMethod('startAdvertising', <String, dynamic>{
'userNickName': userNickName,
'strategy': strategy.index
});
}
Future<void> stopAdvertising() async {
await _channel.invokeMethod('stopAdvertising');
}
}
abstract class ConnectionLifecycleCallback {
void onConnectionInitiated(String s, ConnectionInfo connectionInfo);
void onConnectionResult(String s, Status status);
void onDisconnected(String s) {}
}
class ConnectionInfo {
String endPointName, authenticationToken;
bool isIncomingConnection;
ConnectionInfo(
this.endPointName, this.authenticationToken, this.isIncomingConnection);
} }