A Flutter plugin that provides access to Android's Wi-Fi P2P (Peer-to-Peer) API, allowing devices to connect directly to each other without an access point. This plugin supports peer discovery, connection management, group creation, and socket-based messaging.
- Peer Discovery: Discover nearby Wi-Fi P2P capable devices.
- Connection Management: Connect to and disconnect from peers.
- Group Management: Create and remove Wi-Fi P2P groups.
- Socket Messaging: Send and receive messages using standard TCP sockets.
- Event Streams: Real-time updates for:
- Wi-Fi P2P state changes (enabled/disabled).
- Discovered peers list.
- Connection information.
- Device information.
- Incoming socket messages.
- Device Info: Retrieve the current device's name and address.
- ✅ Android (API 24+)
- ❌ iOS (Not supported)
- ❌ Web (Not supported)
Add android_wifi_p2p to your pubspec.yaml:
dependencies:
android_wifi_p2p: ^1.0.0Crucial Step: You must add the following permissions to your app's android/app/src/main/AndroidManifest.xml file. These are not automatically merged from the plugin.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">
<!-- Wi-Fi P2P Permissions -->
<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_FINE_LOCATION" />
<!-- Android 13+ (API 33+) requirement -->
<uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES"
android:usesPermissionFlags="neverForLocation" />
<application ...>
...
</application>
</manifest>In your Flutter app, you must request Location (for Android < 13) or Nearby Devices (for Android 13+) permissions at runtime before using the plugin. You can use the permission_handler package for this.
import 'package:permission_handler/permission_handler.dart';
Future<void> requestPermissions() async {
if (await Permission.location.request().isGranted) {
// Permission granted
}
// For Android 13+
if (await Permission.nearbyWifiDevices.request().isGranted) {
// Permission granted
}
}Import the package:
import 'package:android_wifi_p2p/android_wifi_p2p.dart';final wifiP2p = AndroidWifiP2p();
// Register the broadcast receiver (Must be called first)
await wifiP2p.register();// Start discovering peers
await wifiP2p.discoverPeers();
// Listen to discovered peers
wifiP2p.peersStream.listen((peers) {
for (var peer in peers) {
print('Found peer: ${peer.deviceName} (${peer.deviceAddress})');
}
});
// Connect to a peer
await wifiP2p.connect('AA:BB:CC:DD:EE:FF');// Get platform version
final version = await wifiP2p.getPlatformVersion();
// Get the current device name (e.g., "Pixel 7")
final deviceName = await wifiP2p.getDeviceName();
print("My Device Name: $deviceName");After a connection is established, the plugin automatically opens a socket channel between the devices. You can send and receive data using the sendMessage method and messageStream.
import 'dart:convert';
// Listen for incoming messages
wifiP2p.messageStream.listen((data) {
final msg = utf8.decode(data);
print("Received: $msg");
});
// Send a message
final text = "Hello World!";
await wifiP2p.sendMessage(utf8.encode(text));When you are done (e.g., dispose() in your widget):
await wifiP2p.unregister();| Method | Description |
|---|---|
register() |
Registers the Wi-Fi P2P broadcast receiver. |
unregister() |
Unregisters the receiver to release resources. |
discoverPeers() |
Starts searching for nearby devices. |
stopPeerDiscovery() |
Stops the discovery process. |
connect(address) |
Initiates a P2P connection to the specified address. |
cancelConnect() |
Cancels an ongoing connection attempt. |
createGroup() |
Creates a P2P group with this device as the owner. |
removeGroup() |
Removes the current P2P group. |
getDeviceName() |
Returns the current device's friendly name. |
| Method | Description |
|---|---|
sendMessage(bytes) |
Sends a byte array message to the connected socket. |
| Stream | Description |
|---|---|
wifiP2pStateStream |
Updates on P2P enabled/disabled state. |
peersStream |
List of discovered peers. |
connectionInfoStream |
Updates on connection status and group ownership. |
thisDeviceStream |
Updates on this device's status. |
messageStream |
Incoming data (bytes) from sockets. |
MIT