From 6d9f4b333eeb1b7a84405fbba6d90d3141f59e71 Mon Sep 17 00:00:00 2001 From: Chris Wales Date: Mon, 25 Nov 2019 10:34:02 -0500 Subject: [PATCH 1/2] add call to WifiP2pManager.removeGroup --- .../mintware/flutter_p2p/FlutterP2pPlugin.kt | 23 +++++++++++++++++++ example/lib/main.dart | 4 ++++ lib/plugin.dart | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/android/src/main/kotlin/de/mintware/flutter_p2p/FlutterP2pPlugin.kt b/android/src/main/kotlin/de/mintware/flutter_p2p/FlutterP2pPlugin.kt index 2e2b276..1849bfb 100644 --- a/android/src/main/kotlin/de/mintware/flutter_p2p/FlutterP2pPlugin.kt +++ b/android/src/main/kotlin/de/mintware/flutter_p2p/FlutterP2pPlugin.kt @@ -20,6 +20,7 @@ import android.content.ContentValues.TAG import android.content.Context import android.content.IntentFilter import android.net.wifi.p2p.WifiP2pManager +import android.net.wifi.p2p.WifiP2pGroup import android.os.Looper import android.util.Log import java.lang.reflect.Method @@ -234,6 +235,28 @@ class FlutterP2pPlugin(private val registrar: Registrar }) } + @Suppress("unused", "UNUSED_PARAMETER") + fun removeGroup(call: MethodCall, result: Result) { + manager.requestGroupInfo(channel, object : WifiP2pManager.GroupInfoListener { + override fun onGroupInfoAvailable(group: WifiP2pGroup) { + if (group != null) { + manager.removeGroup(channel, object : WifiP2pManager.ActionListener { + override fun onSuccess() { + result.success(true) + } + + override fun onFailure(reasonCode: Int) { + result.error(reasonCode.toString(), null, null) + } + }) + } else { + //signal success as the device is not currently a member of a group + result.success(true) + } + } + }) + } + //endregion //region Host Advertising diff --git a/example/lib/main.dart b/example/lib/main.dart index 38cfdb6..4137381 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -183,6 +183,10 @@ class _MyAppState extends State with WidgetsBindingObserver { : null, child: Text("Send hello world"), ), + RaisedButton( + onPressed: _isConnected ? () => FlutterP2p.removeGroup() : null, + child: Text("Disconnect"), + ), Expanded( child: ListView( children: this.devices.map((d) { diff --git a/lib/plugin.dart b/lib/plugin.dart index aaf1877..38e4572 100644 --- a/lib/plugin.dart +++ b/lib/plugin.dart @@ -65,6 +65,10 @@ class FlutterP2p { return await _channel.invokeMethod("cancelConnect", {}); } + static Future removeGroup() async { + return await _channel.invokeMethod("removeGroup", {}); + } + //endregion //region Host Advertising From 3063eb464ec57450be17d74aad5895b098ab7467 Mon Sep 17 00:00:00 2001 From: Chris Wales Date: Mon, 25 Nov 2019 10:38:58 -0500 Subject: [PATCH 2/2] add removeGroup example to README --- README.md | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 3ae46dd..c58b9a5 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ This plugin is in alpha and only supports android at the moment. ## Getting Started ### Required permissions + - `android.permission.CHANGE_WIFI_STATE` - `android.permission.ACCESS_FINE_LOCATION` - `android.permission.ACCESS_COARSE_LOCATION` @@ -19,7 +20,9 @@ This plugin is in alpha and only supports android at the moment. - `android.permission.ACCESS_WIFI_STATE` ### Request permission + In order to scan for devices and connect to devices you need to ask for the location Permission + ```dart Future _checkPermission() async { if (!await FlutterP2p.isLocationPermissionGranted()) { @@ -31,8 +34,10 @@ Future _checkPermission() async { ``` ### Register / unregister from WiFi events + To receive notifications for connection changes or device changes (peers discovered etc.) you have to subscribe to the wifiEvents and register the plugin to the native events. + ```dart class _MyAppState extends State with WidgetsBindingObserver { @override @@ -51,7 +56,7 @@ class _MyAppState extends State with WidgetsBindingObserver { @override void didChangeAppLifecycleState(AppLifecycleState state) { // Stop handling events when the app doesn't run to prevent battery draining - + if (state == AppLifecycleState.resumed) { _register(); } else if (state == AppLifecycleState.paused) { @@ -66,7 +71,7 @@ class _MyAppState extends State with WidgetsBindingObserver { return; } _subscriptions.add(FlutterP2p.wifiEvents.stateChange.listen((change) { - // Handle wifi state change + // Handle wifi state change })); _subscriptions.add(FlutterP2p.wifiEvents.connectionChange.listen((change) { @@ -91,9 +96,10 @@ class _MyAppState extends State with WidgetsBindingObserver { } ``` - ### Discover devices + After you subscribed to the events you only need to call the `FlutterP2p.discoverDevices()` method. + ```dart List _peers = []; @@ -112,11 +118,12 @@ void _register() async { } void _discover() { - FlutterP2p.discoverDevices(); + FlutterP2p.discoverDevices(); } ``` ### Connect to a device + Call `FlutterP2p.connect(device);` and listen to the `FlutterP2p.wifiEvents.connectionChange` ```dart @@ -140,10 +147,22 @@ Call `FlutterP2p.connect(device);` and listen to the `FlutterP2p.wifiEvents.conn } ``` +### Disconnect from current P2P group + +Call `FlutterP2p.removeGroup()` + +```dart + void _disconnect() async { + FlutterP2p.removeGroup(); + } +``` + ### Transferring data between devices + After you are connected to a device you can transfer data async in both directions (client -> host, host -> client). On the host: + ```dart // Open a port and create a socket @@ -165,18 +184,19 @@ On the host: } }); - // Write data to the client using the _socket.write(UInt8List) or `_socket.writeString("Hello")` method + // Write data to the client using the _socket.write(UInt8List) or `_socket.writeString("Hello")` method + - print("_openPort done"); // accept a connection on the created socket await FlutterP2p.acceptPort(port); print("_accept done"); } -``` +``` On the client: + ```dart // Connect to the port and create a socket @@ -202,9 +222,9 @@ On the client: buffer = ""; } }); - - // Write data to the host using the _socket.write(UInt8List) or `_socket.writeString("Hello")` method + + // Write data to the host using the _socket.write(UInt8List) or `_socket.writeString("Hello")` method print("_connectToPort done"); } -``` +```