mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
added askpermission and partial startAdvertising methods
This commit is contained in:
parent
6ee7aba035
commit
6d4f708722
@ -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<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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,12 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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
|
||||
calls FlutterMain.startInitialization(this); in its onCreate method.
|
||||
In most cases you can leave this as-is, but you if you want to provide
|
||||
|
@ -52,8 +52,27 @@ class _MyBodyState extends State<Body> {
|
||||
RaisedButton(
|
||||
child: Text("askPermission(permission handler)"),
|
||||
onPressed: () async {
|
||||
// await PermissionHandler()
|
||||
// .requestPermissions([PermissionGroup.location]);
|
||||
await Nearby.instance.askPermission();
|
||||
},
|
||||
),
|
||||
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();
|
||||
},
|
||||
),
|
||||
],
|
||||
|
@ -8,7 +8,6 @@ environment:
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
cupertino_icons: ^0.1.2
|
||||
|
@ -2,6 +2,9 @@ import 'dart:async';
|
||||
|
||||
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 {
|
||||
//for maintaining only 1 instance of this class
|
||||
static final Nearby _instance = Nearby._();
|
||||
@ -14,4 +17,37 @@ class Nearby {
|
||||
Future<bool> checkPermissions() async => await _channel.invokeMethod(
|
||||
'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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user