flutter_p2p_connection 1.0.1 copy "flutter_p2p_connection: ^1.0.1" to clipboard
flutter_p2p_connection: ^1.0.1 copied to clipboard

outdated

A WiFi Direct Plugin for Flutter. This Plugin uses the native WiFi P2P API of Android.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:async';

import 'package:flutter_p2p_connection/flutter_p2p_connection.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
  final TextEditingController msgText = TextEditingController();
  final _flutterP2pConnectionPlugin = FlutterP2pConnection();
  List<DiscoveredPeers> peers = [];
  String myIpAddress = "unkown";
  WifiP2PInfo wifiP2PInfo = const WifiP2PInfo(
    groupFormed: false,
    groupOwnerAddress: null,
    isConnected: false,
    isGroupOwner: false,
    clients: [],
  );
  StreamSubscription<WifiP2PInfo>? _streamWifiInfo;
  StreamSubscription<List<DiscoveredPeers>>? _streamPeers;

  HttpServer? server;
  List<WebSocket?> sockets = [];

  @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) {
      if (wifiP2PInfo != event) {
        setState(() {
          wifiP2PInfo = event;
        });
      }
    });
    _streamPeers = _flutterP2pConnectionPlugin.streamPeers().listen((event) {
      if (peers != event) {
        setState(() {
          peers = event;
        });
      }
    });
  }

  Future startSocket() async {
    await _flutterP2pConnectionPlugin.startSocket(
      groupOwnerAddress: wifiP2PInfo.groupOwnerAddress!,
      onStarted: (s, address) {
        server = s;
        snack("opened a socket at: $address");
      },
      onConnect: (socket) {
        sockets.add(socket);
      },
      onRequest: (req) async {
        request(req);
      },
    );
  }

  Future connectToSocket() async {
    WebSocket? socket = await _flutterP2pConnectionPlugin.connectToSocket(
      groupOwnerAddress: wifiP2PInfo.groupOwnerAddress!,
      onConnect: (address) {
        snack("connected to socket: $address");
      },
      onRequest: (req) async {
        request(req);
      },
    );
    sockets.add(socket);
  }

  Future closeSocketConnection() async {
    if (server != null) server?.close();
    for (WebSocket? socket in sockets) {
      if (socket != null) {
        socket.close();
      }
    }
    sockets.clear();
    ScaffoldMessenger.of(context).showSnackBar(
      const SnackBar(
        content: Text(
          "closed",
        ),
      ),
    );
    setState(() {});
  }

  void request(dynamic req) async {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
          req,
        ),
      ),
    );
  }

  Future sendMessage() async {
    try {
      for (WebSocket? socket in sockets) {
        if (socket != null) {
          socket.add(msgText.text.trim());
          // print(socket.closeCode);
        }
      }
    } catch (e) {
      print(e);
    }
  }

  void snack(String msg) async {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
          msg,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Flutter p2p connection plugin'),
      ),
      body: SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text("IP: ${wifiP2PInfo.groupOwnerAddress}"),
            Text(
                "connected: ${wifiP2PInfo.isConnected}, isGroupOwner: ${wifiP2PInfo.isGroupOwner}, groupFormed: ${wifiP2PInfo.groupFormed}, groupOwnerAddress: ${wifiP2PInfo.groupOwnerAddress}, clients: ${wifiP2PInfo.clients}"),
            const SizedBox(height: 10),
            const Text("PEERS:"),
            SizedBox(
              height: 100,
              width: MediaQuery.of(context).size.width,
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: peers.length,
                itemBuilder: (context, index) => Center(
                  child: GestureDetector(
                    onTap: () {
                      showDialog(
                        context: context,
                        builder: (context) => Center(
                          child: AlertDialog(
                            content: SizedBox(
                              height: 200,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text("name: ${peers[index].deviceName}"),
                                  Text(
                                      "address: ${peers[index].deviceAddress}"),
                                  Text(
                                      "isGroupOwner: ${peers[index].isGroupOwner}"),
                                  Text(
                                      "isServiceDiscoveryCapable: ${peers[index].isServiceDiscoveryCapable}"),
                                  Text(
                                      "primaryDeviceType: ${peers[index].primaryDeviceType}"),
                                  Text(
                                      "secondaryDeviceType: ${peers[index].secondaryDeviceType}"),
                                  Text("status: ${peers[index].status}"),
                                ],
                              ),
                            ),
                            actions: [
                              TextButton(
                                onPressed: () async {
                                  bool? bo = await _flutterP2pConnectionPlugin
                                      .connect(peers[index].deviceAddress);
                                  snack("connected: $bo");
                                },
                                child: const Text("connect"),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                    child: Container(
                      height: 80,
                      width: 80,
                      decoration: BoxDecoration(
                        color: Colors.grey,
                        borderRadius: BorderRadius.circular(50),
                      ),
                      child: Center(
                        child: Text(
                          peers[index]
                              .deviceName
                              .toString()
                              .characters
                              .first
                              .toUpperCase(),
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 30,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            ),
            ElevatedButton(
              onPressed: () async {
                await Permission.location.request();
                print(await Permission.location.status);
              },
              child: const Text("ask location permission"),
            ),
            ElevatedButton(
              onPressed: () async {
                bool? created = await _flutterP2pConnectionPlugin.createGroup();
                snack("created group: $created");
              },
              child: const Text("create group"),
            ),
            ElevatedButton(
              onPressed: () async {
                bool? removed = await _flutterP2pConnectionPlugin.removeGroup();
                snack("removed group: $removed");
              },
              child: const Text("remove group/disconnect"),
            ),
            ElevatedButton(
              onPressed: () async {
                var info = await _flutterP2pConnectionPlugin.groupInfo();
                showDialog(
                  context: context,
                  builder: (context) => Center(
                    child: Dialog(
                      child: SizedBox(
                        height: 200,
                        child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 10),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                  "groupNetworkName: ${info?.groupNetworkName}"),
                              Text("passPhrase: ${info?.passPhrase}"),
                              Text("isGroupOwner: ${info?.isGroupOwner}"),
                              Text("clients: ${info?.clients}"),
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                );
              },
              child: const Text("get group info"),
            ),
            ElevatedButton(
              onPressed: () async {
                bool? discovering =
                    await _flutterP2pConnectionPlugin.discover();
                snack("discovering $discovering");
              },
              child: const Text("discover"),
            ),
            ElevatedButton(
              onPressed: () async {
                bool? stopped =
                    await _flutterP2pConnectionPlugin.stopDiscovery();
                snack("stopped discovering $stopped");
              },
              child: const Text("stop discovery"),
            ),
            ElevatedButton(
              onPressed: () async {
                startSocket();
              },
              child: const Text("open a socket"),
            ),
            ElevatedButton(
              onPressed: () async {
                connectToSocket();
              },
              child: const Text("connect to socket"),
            ),
            ElevatedButton(
              onPressed: () async {
                closeSocketConnection();
              },
              child: const Text("close socket"),
            ),
            TextField(
              controller: msgText,
              decoration: const InputDecoration(
                hintText: "message",
              ),
            ),
            ElevatedButton(
              onPressed: () async {
                sendMessage();
              },
              child: const Text("send msg"),
            ),
          ],
        ),
      ),
    );
  }
}
33
likes
0
points
663
downloads

Publisher

unverified uploader

Weekly Downloads

A WiFi Direct Plugin for Flutter. This Plugin uses the native WiFi P2P API of Android.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on flutter_p2p_connection

Packages that implement flutter_p2p_connection