Hey,
I started working on a WiFi Direct (P2P) plugin for Flutter.
https://github.com/mintware-de/flutter_p2p
So far working (Android only at the moment):
- Subscribe to WiFi Events
- Discover devices
- Connect to a device
- Transfer data between the devices using web sockets and streams.
Feel free to collaborate π
Top comments (14)
THIS IS MY CODE:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_p2p/flutter_p2p.dart';
import 'package:flutter_p2p/gen/protos/protos.pb.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State 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) {
if (state == AppLifecycleState.resumed) {
_register();
} else if (state == AppLifecycleState.paused) {
_unregister();
}
}
List devices = [];
var _isConnected = false;
var _isHost = false;
List _subscriptions = [];
void _register() async {
if (!await _checkPermission()) {
return;
}
_subscriptions.add(FlutterP2p.wifiEvents.stateChange.listen((change) {
print("stateChange: ${change.isEnabled}");
}));
}
void _unregister() {
_subscriptions.forEach((subscription) => subscription.cancel());
FlutterP2p.unregister();
}
P2pSocket _socket;
void _openPortAndAccept(int port) async {
var socket = await FlutterP2p.openHostPort(port);
setState(() {
_socket = socket;
});
}
var _deviceAddress = "";
_connectToPort(int port) async {
var socket = await FlutterP2p.connectToHost(
_deviceAddress,
port,
timeout: 100000,
);
}
Future _checkPermission() async {
if (!await FlutterP2p.isLocationPermissionGranted()) {
await FlutterP2p.requestLocationPermission();
return false;
}
return true;
}
final _scaffoldKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
title: const Text('Plugin example app 2'),
),
body: Column(
children: [
Text(_isConnected
? "Connected: ${_isHost ? "Host" : "Client"}"
: "Disconnected"),
RaisedButton(
onPressed: () => FlutterP2p.discoverDevices(),
child: Text("Discover Devices"),
),
RaisedButton(
onPressed:
_isConnected && _isHost ? () => _openPortAndAccept(8888) : null,
child: Text("Open and accept data from port 8888"),
),
RaisedButton(
onPressed: _isConnected ? () => _connectToPort(8888) : null,
child: Text("Connect to port 8888"),
),
RaisedButton(
onPressed: _socket != null
? () => _socket.writeString("Hello World")
: null,
child: Text("Send hello world"),
),
RaisedButton(
onPressed: _isConnected ? () => FlutterP2p.removeGroup() : null,
child: Text("Disconnect"),
),
Expanded(
child: ListView(
children: this.devices.map((d) {
return ListTile(
title: Text(d.deviceName),
subtitle: Text(d.deviceAddress),
onTap: () {
print(
"${_isConnected ? "Disconnect" : "Connect"} to device: $_deviceAddress");
return _isConnected
? FlutterP2p.cancelConnect(d)
: FlutterP2p.connect(d);
},
);
}).toList(),
),
),
],
),
);
}
snackBar(String text) {
_scaffoldKey.currentState.showSnackBar(
SnackBar(
content: Text(text),
duration: Duration(seconds: 2),
),
);
}
}
ERROR OCCURED TO ME:
A problem occurred configuring project ':flutter_p2p'.
Dear sir thank you for providing help on wifi direct and p2p connection . i have issue in transferring data like image videos and some else format over the socket basically how the data is going to be encoded and decoded at server and client side please help
Can you please provide with sample code because discover devices showing empty List everytime.
A working sample is contained in the README. Are the other devices "visible"?
on tapping discover devices it is showing empty list of wifi networks....
Plz see to it.
I've seen it. Very nice Library thank you. But the minimum API level that it supports is API level 23. What about for API level below 23?
Thanks for your reply. Probably this was the default minimum SDK as I started working on this lib. Let me take a look if I can downgrade the version :-)
I have modified it to support android version starting from 4.1, Please accept the pull request and modify the library on pub.dev
how I can send and receive files like images, docs, etc. using p2p
Simply follow the instructions in the README github.com/mintware-de/flutter_p2p
AFAIK, iOS does not support Wi-Fi direct. Link: forums.developer.apple.com/thread/...
Hello Sir , i was working with your package provided on flutterp2p i was having a query that is can i send files (jpg , png , pdf etc.) using this package. please help
thank you
please give solution to me , my source code is below
Sir, please provide a manual how to use the plugin .