flutter_p2p_connection 1.0.1
flutter_p2p_connection: ^1.0.1 copied to clipboard
A WiFi Direct Plugin for Flutter. This Plugin uses the native WiFi P2P API of Android.
flutter_p2p_connection #
A p2p wifi direct plugin.
Getting Started #
Required permissions #
Add these permissions to AndroidManifest.xml
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
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.
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
final _flutterP2pConnectionPlugin = FlutterP2pConnection();
StreamSubscription<WifiP2PInfo>? _streamWifiInfo;
StreamSubscription<List<DiscoveredPeers>>? _streamPeers;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_init();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_flutterP2pConnectionPlugin.unregister();
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.paused) {
_flutterP2pConnectionPlugin.register();
} else if (state == AppLifecycleState.resumed) {
_flutterP2pConnectionPlugin.unregister();
}
}
void _init() async {
await _flutterP2pConnectionPlugin.initialize();
await _flutterP2pConnectionPlugin.register();
_streamWifiInfo = _flutterP2pConnectionPlugin.streamWifiP2PInfo().listen((event) {
// Handle changes of the connection
});
_streamPeers = _flutterP2pConnectionPlugin.streamPeers().listen((event) {
// Handle discovered peers
});
}
}
Discover devices #
After you subscribed to the events you only need to call the _flutterP2pConnectionPlugin.discover()
method.
To stop discovery call the _flutterP2pConnectionPlugin.stopDiscovery()
method.
List<DiscoveredPeers> peers = [];
void _init() async {
/// ...
_streamPeers = _flutterP2pConnectionPlugin.streamPeers().listen((event) {
if (peers != event) {
setState(() {
peers = event;
});
}
});
/// ...
}
void discover() {
_flutterP2pConnectionPlugin.discover();
}
void stopDiscovery() {
_flutterP2pConnectionPlugin.stopDiscovery();
}
Connect to a device #
Call _flutterP2pConnectionPlugin.connect(deviceAddress);
and listen to the _flutterP2pConnectionPlugin.streamWifiP2PInfo
WifiP2PInfo? wifiP2PInfo;
List<DiscoveredPeers> peers = [];
void _init() async {
// ...
_streamWifiInfo = _flutterP2pConnectionPlugin.streamWifiP2PInfo().listen((event) {
if (wifiP2PInfo != event) {
setState(() {
wifiP2PInfo = event;
});
}
});
// ...
}
void connect() async {
await _flutterP2pConnectionPlugin.connect(peers[0].deviceAddress);
}
Disconnect from current P2P group #
Call _flutterP2pConnectionPlugin.removeGroup()
void _disconnect() async {
_flutterP2pConnectionPlugin.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:
WifiP2PInfo? wifiP2PInfo;
HttpServer? server;
List<WebSocket?> sockets = [];
// create a socket
Future startSocket() async {
await _flutterP2pConnectionPlugin.startSocket(
groupOwnerAddress: wifiP2PInfo?.groupOwnerAddress!,
onStarted: (address) {
print("opened a socket at: $address");
},
onConnect: (s, socket) {
server = s;
sockets.add(socket);
},
onRequest: (req) async {
// handle message fromm client
print(req);
},
);
}
// Write data to the client using the socket.add(dynamic) method
Future sendMessage() async {
if (server != null) server?.close();
for (WebSocket? socket in sockets) {
if (socket != null) {
socket.add("message");
}
}
}
// call socket.close() to close socket
Future closeSocketConnection() async {
for (WebSocket? socket in sockets) {
if (socket != null) {
socket.close();
}
}
sockets.clear();
}
On the client:
// Connect to socket
WifiP2PInfo? wifiP2PInfo;
List<WebSocket?> sockets = [];
Future connectToSocket() async {
WebSocket? socket = await _flutterP2pConnectionPlugin.connectToSocket(
groupOwnerAddress: wifiP2PInfo.groupOwnerAddress!,
onConnect: (address) {
print("connect to socket: $address");
},
onRequest: (req) async {
// handle message from server
print(req);
},
);
sockets.add(socket);
}
// Write data to the host using the socket.add(dynamic) method
Future sendMessage() async {
for (WebSocket? socket in sockets) {
if (socket != null) {
socket.add("message");
}
}
}
// call socket.close() to close socket
Future closeSocketConnection() async {
for (WebSocket? socket in sockets) {
if (socket != null) {
socket.close();
}
}
sockets.clear();
}
``