This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
103 changed files
with
5,137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: 2d2a1ffec95cc70a3218872a2cd3f8de4933c42f | ||
channel: stable | ||
|
||
project_type: plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.0.1 | ||
|
||
* Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
# flutter_p2p | ||
|
||
A Wi-Fi Direct Plugin for Flutter. | ||
|
||
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` | ||
- `android.permission.CHANGE_NETWORK_STATE` | ||
- `android.permission.INTERNET` | ||
- `android.permission.ACCESS_NETWORK_STATE` | ||
- `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<bool> _checkPermission() async { | ||
if (!await FlutterP2p.isLocationPermissionGranted()) { | ||
await FlutterP2p.requestLocationPermission(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
``` | ||
|
||
### 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<MyApp> with WidgetsBindingObserver { | ||
@override | ||
void initState() { | ||
super.initState(); | ||
_register(); | ||
WidgetsBinding.instance.addObserver(this); | ||
} | ||
@override | ||
void dispose() { | ||
WidgetsBinding.instance.removeObserver(this); | ||
super.dispose(); | ||
} | ||
@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) { | ||
_unregister(); | ||
} | ||
} | ||
List<StreamSubscription> _subscriptions = []; | ||
void _register() async { | ||
if (!await _checkPermission()) { | ||
return; | ||
} | ||
_subscriptions.add(FlutterP2p.wifiEvents.stateChange.listen((change) { | ||
// Handle wifi state change | ||
})); | ||
_subscriptions.add(FlutterP2p.wifiEvents.connectionChange.listen((change) { | ||
// Handle changes of the connection | ||
})); | ||
_subscriptions.add(FlutterP2p.wifiEvents.thisDeviceChange.listen((change) { | ||
// Handle changes of this device | ||
})); | ||
_subscriptions.add(FlutterP2p.wifiEvents.peersChange.listen((change) { | ||
// Handle discovered peers | ||
})); | ||
FlutterP2p.register(); // Register to the native events which are send to the streams above | ||
} | ||
void _unregister() { | ||
_subscriptions.forEach((subscription) => subscription.cancel()); // Cancel subscriptions | ||
FlutterP2p.unregister(); // Unregister from native events | ||
} | ||
} | ||
``` | ||
|
||
|
||
### Discover devices | ||
After you subscribed to the events you only need to call the `FlutterP2p.discoverDevices()` method. | ||
```dart | ||
List<WifiP2pDevice> _peers = []; | ||
void _register() async { | ||
/// ... | ||
_subscriptions.add(FlutterP2p.wifiEvents.peersChange.listen((change) { | ||
setState(() { | ||
_peers = change.devices; | ||
}); | ||
})); | ||
/// ... | ||
} | ||
void _discover() { | ||
FlutterP2p.discoverDevices(); | ||
} | ||
``` | ||
|
||
### Connect to a device | ||
Call `FlutterP2p.connect(device);` and listen to the `FlutterP2p.wifiEvents.connectionChange` | ||
|
||
```dart | ||
bool _isConnected = false; | ||
bool _isHost = false; | ||
String _deviceAddress = ""; | ||
void _register() async { | ||
// ... | ||
_subscriptions.add(FlutterP2p.wifiEvents.connectionChange.listen((change) { | ||
setState(() { | ||
_isConnected = change.networkInfo.isConnected; | ||
_isHost = change.wifiP2pInfo.isGroupOwner; | ||
_deviceAddress = change.wifiP2pInfo.groupOwnerAddress; | ||
}); | ||
})); | ||
// ... | ||
} | ||
``` | ||
|
||
### 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 | ||
P2pSocket _socket; | ||
void _openPortAndAccept(int port) async { | ||
var socket = await FlutterP2p.openHostPort(port); | ||
setState(() { | ||
_socket = socket; | ||
}); | ||
var buffer = ""; | ||
socket.inputStream.listen((data) { | ||
var msg = String.fromCharCodes(data.data); | ||
buffer += msg; | ||
if (data.dataAvailable == 0) { | ||
_showSnackBar("Data Received: $buffer"); | ||
buffer = ""; | ||
} | ||
}); | ||
// 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 | ||
P2pSocket _socket; | ||
_connectToPort(int port) async { | ||
var socket = await FlutterP2p.connectToHost( | ||
_deviceAddress, // see above `Connect to a device` | ||
port, | ||
timeout: 100000, // timeout in milliseconds (default 500) | ||
); | ||
setState(() { | ||
_socket = socket; | ||
}); | ||
var buffer = ""; | ||
socket.inputStream.listen((data) { | ||
var msg = String.fromCharCodes(data.data); | ||
buffer += msg; | ||
if (data.dataAvailable == 0) { | ||
_showSnackBar("Received from host: $buffer"); | ||
buffer = ""; | ||
} | ||
}); | ||
// Write data to the host using the _socket.write(UInt8List) or `_socket.writeString("Hello")` method | ||
print("_connectToPort done"); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
group 'de.mintware.flutter_p2p' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.2.71' | ||
ext.protobufVersion = '0.8.8' | ||
|
||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:3.2.1' | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
classpath "com.google.protobuf:protobuf-gradle-plugin:$protobufVersion" | ||
} | ||
} | ||
|
||
rootProject.allprojects { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'com.google.protobuf' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
main.proto.srcDirs += '../protos' | ||
} | ||
defaultConfig { | ||
minSdkVersion 23 | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
lintOptions { | ||
disable 'InvalidPackage' | ||
} | ||
} | ||
|
||
protobuf { | ||
// Configure the protoc executable | ||
protoc { | ||
// Download from repositories | ||
artifact = 'com.google.protobuf:protoc:3.6.1' | ||
} | ||
plugins { | ||
javalite { | ||
// The codegen for lite comes as a separate artifact | ||
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0' | ||
} | ||
} | ||
generateProtoTasks { | ||
all().each { task -> | ||
task.plugins { | ||
javalite {} | ||
} | ||
} | ||
} | ||
} | ||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'com.google.protobuf:protobuf-lite:3.0.1' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
org.gradle.jvmargs=-Xmx1536M | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'flutter_p2p' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="de.mintware.flutter_p2p"> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* This file is part of the flutter_p2p package. | ||
* | ||
* Copyright 2019 by Julian Finkler <julian@mintware.de> | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
package de.mintware.flutter_p2p | ||
|
||
class Config { | ||
var bufferSize: Int = 1024; | ||
var timeout: Int = 500; | ||
} |
Oops, something went wrong.