mirror of
				https://github.com/hackku21/nearby_connections.git
				synced 2025-06-13 12:53:50 +00:00 
			
		
		
		
	working auto connect
This commit is contained in:
		
							parent
							
								
									c21fa52248
								
							
						
					
					
						commit
						e19633f846
					
				@ -2,6 +2,8 @@ import 'dart:io';
 | 
				
			|||||||
import 'dart:math';
 | 
					import 'dart:math';
 | 
				
			||||||
import 'dart:typed_data';
 | 
					import 'dart:typed_data';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:flutter_udid/flutter_udid.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
import 'package:image_picker/image_picker.dart';
 | 
					import 'package:image_picker/image_picker.dart';
 | 
				
			||||||
import 'package:nearby_connections/nearby_connections.dart';
 | 
					import 'package:nearby_connections/nearby_connections.dart';
 | 
				
			||||||
@ -33,14 +35,88 @@ class Body extends StatefulWidget {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class _MyBodyState extends State<Body> {
 | 
					class _MyBodyState extends State<Body> {
 | 
				
			||||||
  final String userName = Random().nextInt(10000).toString();
 | 
					  @override
 | 
				
			||||||
 | 
					  void initState() {
 | 
				
			||||||
 | 
					    super.initState();
 | 
				
			||||||
 | 
					    WidgetsBinding.instance!.addPostFrameCallback((_) {
 | 
				
			||||||
 | 
					      // _userName = await FlutterUdid.consistentUdid;
 | 
				
			||||||
 | 
					      startAdver();
 | 
				
			||||||
 | 
					      startDisco();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  final String userName =
 | 
				
			||||||
 | 
					      "00000 11111 22222 33333 44444 55555 66666 77777 88888 99999 aaaaa bbbbb ccccc ddddd eeeee"; // Random().nextInt(10000).toString();
 | 
				
			||||||
  final Strategy strategy = Strategy.P2P_STAR;
 | 
					  final Strategy strategy = Strategy.P2P_STAR;
 | 
				
			||||||
  Map<String, ConnectionInfo> endpointMap = Map();
 | 
					  Map<String, ConnectionInfo> endpointMap = Map();
 | 
				
			||||||
 | 
					  static String _userName = "";
 | 
				
			||||||
  File? tempFile; //reference to the file currently being transferred
 | 
					  File? tempFile; //reference to the file currently being transferred
 | 
				
			||||||
  Map<int, String> map =
 | 
					  Map<int, String> map =
 | 
				
			||||||
      Map(); //store filename mapped to corresponding payloadId
 | 
					      Map(); //store filename mapped to corresponding payloadId
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void startAdver() async {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      bool a = await Nearby().startAdvertising(
 | 
				
			||||||
 | 
					        "adv" + userName,
 | 
				
			||||||
 | 
					        strategy,
 | 
				
			||||||
 | 
					        onConnectionInitiated: discoverOnConnectionInit,
 | 
				
			||||||
 | 
					        onConnectionResult: (id, status) {
 | 
				
			||||||
 | 
					          print("userName = $userName");
 | 
				
			||||||
 | 
					          print("id = $id");
 | 
				
			||||||
 | 
					          showSnackbar(status);
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        onDisconnected: (id) {
 | 
				
			||||||
 | 
					          showSnackbar(
 | 
				
			||||||
 | 
					              "Disconnected: ${endpointMap[id]!.endpointName}, id $id");
 | 
				
			||||||
 | 
					          setState(() {
 | 
				
			||||||
 | 
					            endpointMap.remove(id);
 | 
				
			||||||
 | 
					          });
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					      showSnackbar("ADVERTISING: " + a.toString());
 | 
				
			||||||
 | 
					    } catch (exception) {
 | 
				
			||||||
 | 
					      showSnackbar(exception);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void startDisco() async {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      bool a = await Nearby().startDiscovery(
 | 
				
			||||||
 | 
					        "dis" + userName,
 | 
				
			||||||
 | 
					        strategy,
 | 
				
			||||||
 | 
					        onEndpointFound: (id, name, serviceId) {
 | 
				
			||||||
 | 
					          print("userName = $userName");
 | 
				
			||||||
 | 
					          print("id = $id");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					          Nearby().requestConnection(
 | 
				
			||||||
 | 
					            userName,
 | 
				
			||||||
 | 
					            id,
 | 
				
			||||||
 | 
					            onConnectionInitiated: (id, info) {
 | 
				
			||||||
 | 
					              discoverOnConnectionInit(id, info);
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            onConnectionResult: (id, status) {
 | 
				
			||||||
 | 
					              showSnackbar(status);
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            onDisconnected: (id) {
 | 
				
			||||||
 | 
					              setState(() {
 | 
				
			||||||
 | 
					                endpointMap.remove(id);
 | 
				
			||||||
 | 
					              });
 | 
				
			||||||
 | 
					              showSnackbar(
 | 
				
			||||||
 | 
					                  "Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					          );
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        onEndpointLost: (id) {
 | 
				
			||||||
 | 
					          showSnackbar(
 | 
				
			||||||
 | 
					              "Lost discovered Endpoint: ${endpointMap[id]!.endpointName}, id $id");
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					      showSnackbar("DISCOVERING: " + a.toString());
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					      showSnackbar(e);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) {
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
    return Center(
 | 
					    return Center(
 | 
				
			||||||
@ -48,117 +124,34 @@ class _MyBodyState extends State<Body> {
 | 
				
			|||||||
        padding: const EdgeInsets.all(8.0),
 | 
					        padding: const EdgeInsets.all(8.0),
 | 
				
			||||||
        child: ListView(
 | 
					        child: ListView(
 | 
				
			||||||
          children: <Widget>[
 | 
					          children: <Widget>[
 | 
				
			||||||
            Text(
 | 
					 | 
				
			||||||
              "Permissions",
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
            Wrap(
 | 
					 | 
				
			||||||
              children: <Widget>[
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("checkLocationPermission"),
 | 
					 | 
				
			||||||
                  onPressed: () async {
 | 
					 | 
				
			||||||
                    if (await Nearby().checkLocationPermission()) {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content: Text("Location permissions granted :)")));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content:
 | 
					 | 
				
			||||||
                              Text("Location permissions not granted :(")));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("askLocationPermission"),
 | 
					 | 
				
			||||||
                  onPressed: () async {
 | 
					 | 
				
			||||||
                    if (await Nearby().askLocationPermission()) {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content: Text("Location Permission granted :)")));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content:
 | 
					 | 
				
			||||||
                              Text("Location permissions not granted :(")));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("checkExternalStoragePermission"),
 | 
					 | 
				
			||||||
                  onPressed: () async {
 | 
					 | 
				
			||||||
                    if (await Nearby().checkExternalStoragePermission()) {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content:
 | 
					 | 
				
			||||||
                              Text("External Storage permissions granted :)")));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content: Text(
 | 
					 | 
				
			||||||
                              "External Storage permissions not granted :(")));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("askExternalStoragePermission"),
 | 
					 | 
				
			||||||
                  onPressed: () {
 | 
					 | 
				
			||||||
                    Nearby().askExternalStoragePermission();
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
              ],
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
            Divider(),
 | 
					 | 
				
			||||||
            Text("Location Enabled"),
 | 
					 | 
				
			||||||
            Wrap(
 | 
					 | 
				
			||||||
              children: <Widget>[
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("checkLocationEnabled"),
 | 
					 | 
				
			||||||
                  onPressed: () async {
 | 
					 | 
				
			||||||
                    if (await Nearby().checkLocationEnabled()) {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(
 | 
					 | 
				
			||||||
                          SnackBar(content: Text("Location is ON :)")));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(
 | 
					 | 
				
			||||||
                          SnackBar(content: Text("Location is OFF :(")));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
                ElevatedButton(
 | 
					 | 
				
			||||||
                  child: Text("enableLocationServices"),
 | 
					 | 
				
			||||||
                  onPressed: () async {
 | 
					 | 
				
			||||||
                    if (await Nearby().enableLocationServices()) {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content: Text("Location Service Enabled :)")));
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
 | 
					 | 
				
			||||||
                          content:
 | 
					 | 
				
			||||||
                              Text("Enabling Location Service Failed :(")));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                  },
 | 
					 | 
				
			||||||
                ),
 | 
					 | 
				
			||||||
              ],
 | 
					 | 
				
			||||||
            ),
 | 
					 | 
				
			||||||
            Divider(),
 | 
					 | 
				
			||||||
            Text("User Name: " + userName),
 | 
					            Text("User Name: " + userName),
 | 
				
			||||||
            Wrap(
 | 
					            Wrap(
 | 
				
			||||||
              children: <Widget>[
 | 
					              children: <Widget>[
 | 
				
			||||||
                ElevatedButton(
 | 
					                ElevatedButton(
 | 
				
			||||||
                  child: Text("Start Advertising"),
 | 
					                  child: Text("Start Advertising"),
 | 
				
			||||||
                  onPressed: () async {
 | 
					                  onPressed: () async {
 | 
				
			||||||
                    try {
 | 
					                    // try {
 | 
				
			||||||
                      bool a = await Nearby().startAdvertising(
 | 
					                    //   bool a = await Nearby().startAdvertising(
 | 
				
			||||||
                        userName,
 | 
					                    //     "adv" + userName,
 | 
				
			||||||
                        strategy,
 | 
					                    //     strategy,
 | 
				
			||||||
                        onConnectionInitiated: onConnectionInit,
 | 
					                    //     onConnectionInitiated: discoverOnConnectionInit,
 | 
				
			||||||
                        onConnectionResult: (id, status) {
 | 
					                    //     onConnectionResult: (id, status) {
 | 
				
			||||||
                          showSnackbar(status);
 | 
					                    //       print("userName = $userName");
 | 
				
			||||||
                        },
 | 
					                    //       print("id = $id");
 | 
				
			||||||
                        onDisconnected: (id) {
 | 
					                    //       showSnackbar(status);
 | 
				
			||||||
                          showSnackbar(
 | 
					                    //     },
 | 
				
			||||||
                              "Disconnected: ${endpointMap[id]!.endpointName}, id $id");
 | 
					                    //     onDisconnected: (id) {
 | 
				
			||||||
                          setState(() {
 | 
					                    //       showSnackbar(
 | 
				
			||||||
                            endpointMap.remove(id);
 | 
					                    //           "Disconnected: ${endpointMap[id]!.endpointName}, id $id");
 | 
				
			||||||
                          });
 | 
					                    //       setState(() {
 | 
				
			||||||
                        },
 | 
					                    //         endpointMap.remove(id);
 | 
				
			||||||
                      );
 | 
					                    //       });
 | 
				
			||||||
                      showSnackbar("ADVERTISING: " + a.toString());
 | 
					                    //     },
 | 
				
			||||||
                    } catch (exception) {
 | 
					                    //   );
 | 
				
			||||||
                      showSnackbar(exception);
 | 
					                    //   showSnackbar("ADVERTISING: " + a.toString());
 | 
				
			||||||
                    }
 | 
					                    // } catch (exception) {
 | 
				
			||||||
 | 
					                    //   showSnackbar(exception);
 | 
				
			||||||
 | 
					                    // }
 | 
				
			||||||
                  },
 | 
					                  },
 | 
				
			||||||
                ),
 | 
					                ),
 | 
				
			||||||
                ElevatedButton(
 | 
					                ElevatedButton(
 | 
				
			||||||
@ -176,45 +169,27 @@ class _MyBodyState extends State<Body> {
 | 
				
			|||||||
                  onPressed: () async {
 | 
					                  onPressed: () async {
 | 
				
			||||||
                    try {
 | 
					                    try {
 | 
				
			||||||
                      bool a = await Nearby().startDiscovery(
 | 
					                      bool a = await Nearby().startDiscovery(
 | 
				
			||||||
                        userName,
 | 
					                        "dis" + userName,
 | 
				
			||||||
                        strategy,
 | 
					                        strategy,
 | 
				
			||||||
                        onEndpointFound: (id, name, serviceId) {
 | 
					                        onEndpointFound: (id, name, serviceId) {
 | 
				
			||||||
                          // show sheet automatically to request connection
 | 
					                          print("userName = $userName");
 | 
				
			||||||
                          showModalBottomSheet(
 | 
					                          print("id = $id");
 | 
				
			||||||
                            context: context,
 | 
					
 | 
				
			||||||
                            builder: (builder) {
 | 
					                          Nearby().requestConnection(
 | 
				
			||||||
                              return Center(
 | 
					                            userName,
 | 
				
			||||||
                                child: Column(
 | 
					                            id,
 | 
				
			||||||
                                  children: <Widget>[
 | 
					                            onConnectionInitiated: (id, info) {
 | 
				
			||||||
                                    Text("id: " + id),
 | 
					                              discoverOnConnectionInit(id, info);
 | 
				
			||||||
                                    Text("Name: " + name),
 | 
					                            },
 | 
				
			||||||
                                    Text("ServiceId: " + serviceId),
 | 
					                            onConnectionResult: (id, status) {
 | 
				
			||||||
                                    ElevatedButton(
 | 
					                              showSnackbar(status);
 | 
				
			||||||
                                      child: Text("Request Connection"),
 | 
					                            },
 | 
				
			||||||
                                      onPressed: () {
 | 
					                            onDisconnected: (id) {
 | 
				
			||||||
                                        Navigator.pop(context);
 | 
					                              setState(() {
 | 
				
			||||||
                                        Nearby().requestConnection(
 | 
					                                endpointMap.remove(id);
 | 
				
			||||||
                                          userName,
 | 
					                              });
 | 
				
			||||||
                                          id,
 | 
					                              showSnackbar(
 | 
				
			||||||
                                          onConnectionInitiated: (id, info) {
 | 
					                                  "Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
 | 
				
			||||||
                                            onConnectionInit(id, info);
 | 
					 | 
				
			||||||
                                          },
 | 
					 | 
				
			||||||
                                          onConnectionResult: (id, status) {
 | 
					 | 
				
			||||||
                                            showSnackbar(status);
 | 
					 | 
				
			||||||
                                          },
 | 
					 | 
				
			||||||
                                          onDisconnected: (id) {
 | 
					 | 
				
			||||||
                                            setState(() {
 | 
					 | 
				
			||||||
                                              endpointMap.remove(id);
 | 
					 | 
				
			||||||
                                            });
 | 
					 | 
				
			||||||
                                            showSnackbar(
 | 
					 | 
				
			||||||
                                                "Disconnected from: ${endpointMap[id]!.endpointName}, id $id");
 | 
					 | 
				
			||||||
                                          },
 | 
					 | 
				
			||||||
                                        );
 | 
					 | 
				
			||||||
                                      },
 | 
					 | 
				
			||||||
                                    ),
 | 
					 | 
				
			||||||
                                  ],
 | 
					 | 
				
			||||||
                                ),
 | 
					 | 
				
			||||||
                              );
 | 
					 | 
				
			||||||
                            },
 | 
					                            },
 | 
				
			||||||
                          );
 | 
					                          );
 | 
				
			||||||
                        },
 | 
					                        },
 | 
				
			||||||
@ -255,7 +230,8 @@ class _MyBodyState extends State<Body> {
 | 
				
			|||||||
              child: Text("Send Random Bytes Payload"),
 | 
					              child: Text("Send Random Bytes Payload"),
 | 
				
			||||||
              onPressed: () async {
 | 
					              onPressed: () async {
 | 
				
			||||||
                endpointMap.forEach((key, value) {
 | 
					                endpointMap.forEach((key, value) {
 | 
				
			||||||
                  String a = Random().nextInt(100).toString();
 | 
					                  String a =
 | 
				
			||||||
 | 
					                      "00000 11111 22222 33333 44444 55555 66666 77777 88888 99999 aaaaa bbbbb ccccc ddddd eeeee"; //Random().nextInt(100).toString();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                  showSnackbar("Sending $a to ${value.endpointName}, id: $key");
 | 
					                  showSnackbar("Sending $a to ${value.endpointName}, id: $key");
 | 
				
			||||||
                  Nearby()
 | 
					                  Nearby()
 | 
				
			||||||
@ -297,95 +273,64 @@ class _MyBodyState extends State<Body> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  /// Called upon Connection request (on both devices)
 | 
					  /// Called upon Connection request (on both devices)
 | 
				
			||||||
  /// Both need to accept connection to start sending/receiving
 | 
					  /// Both need to accept connection to start sending/receiving
 | 
				
			||||||
  void onConnectionInit(String id, ConnectionInfo info) {
 | 
					  void discoverOnConnectionInit(String id, ConnectionInfo info) {
 | 
				
			||||||
    showModalBottomSheet(
 | 
					    setState(() {
 | 
				
			||||||
      context: context,
 | 
					      endpointMap[id] = info;
 | 
				
			||||||
      builder: (builder) {
 | 
					    });
 | 
				
			||||||
        return Center(
 | 
					    Nearby().acceptConnection(
 | 
				
			||||||
          child: Column(
 | 
					      id,
 | 
				
			||||||
            children: <Widget>[
 | 
					      onPayLoadRecieved: (endid, payload) async {
 | 
				
			||||||
              Text("id: " + id),
 | 
					        print(payload);
 | 
				
			||||||
              Text("Token: " + info.authenticationToken),
 | 
					        if (payload.type == PayloadType.BYTES) {
 | 
				
			||||||
              Text("Name" + info.endpointName),
 | 
					          String str = String.fromCharCodes(payload.bytes!);
 | 
				
			||||||
              Text("Incoming: " + info.isIncomingConnection.toString()),
 | 
					          showSnackbar(endid + ": " + str);
 | 
				
			||||||
              ElevatedButton(
 | 
					 | 
				
			||||||
                child: Text("Accept Connection"),
 | 
					 | 
				
			||||||
                onPressed: () {
 | 
					 | 
				
			||||||
                  Navigator.pop(context);
 | 
					 | 
				
			||||||
                  setState(() {
 | 
					 | 
				
			||||||
                    endpointMap[id] = info;
 | 
					 | 
				
			||||||
                  });
 | 
					 | 
				
			||||||
                  Nearby().acceptConnection(
 | 
					 | 
				
			||||||
                    id,
 | 
					 | 
				
			||||||
                    onPayLoadRecieved: (endid, payload) async {
 | 
					 | 
				
			||||||
                      if (payload.type == PayloadType.BYTES) {
 | 
					 | 
				
			||||||
                        String str = String.fromCharCodes(payload.bytes!);
 | 
					 | 
				
			||||||
                        showSnackbar(endid + ": " + str);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if (str.contains(':')) {
 | 
					          if (str.contains(':')) {
 | 
				
			||||||
                          // used for file payload as file payload is mapped as
 | 
					            // used for file payload as file payload is mapped as
 | 
				
			||||||
                          // payloadId:filename
 | 
					            // payloadId:filename
 | 
				
			||||||
                          int payloadId = int.parse(str.split(':')[0]);
 | 
					            int payloadId = int.parse(str.split(':')[0]);
 | 
				
			||||||
                          String fileName = (str.split(':')[1]);
 | 
					            String fileName = (str.split(':')[1]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                          if (map.containsKey(payloadId)) {
 | 
					            if (map.containsKey(payloadId)) {
 | 
				
			||||||
                            if (await tempFile!.exists()) {
 | 
					              if (await tempFile!.exists()) {
 | 
				
			||||||
                              tempFile!.rename(
 | 
					                tempFile!.rename(tempFile!.parent.path + "/" + fileName);
 | 
				
			||||||
                                  tempFile!.parent.path + "/" + fileName);
 | 
					              } else {
 | 
				
			||||||
                            } else {
 | 
					                showSnackbar("File doesn't exist");
 | 
				
			||||||
                              showSnackbar("File doesn't exist");
 | 
					              }
 | 
				
			||||||
                            }
 | 
					            } else {
 | 
				
			||||||
                          } else {
 | 
					              //add to map if not already
 | 
				
			||||||
                            //add to map if not already
 | 
					              map[payloadId] = fileName;
 | 
				
			||||||
                            map[payloadId] = fileName;
 | 
					            }
 | 
				
			||||||
                          }
 | 
					          }
 | 
				
			||||||
                        }
 | 
					        } else if (payload.type == PayloadType.FILE) {
 | 
				
			||||||
                      } else if (payload.type == PayloadType.FILE) {
 | 
					          showSnackbar(endid + ": File transfer started");
 | 
				
			||||||
                        showSnackbar(endid + ": File transfer started");
 | 
					          tempFile = File(payload.filePath!);
 | 
				
			||||||
                        tempFile = File(payload.filePath!);
 | 
					        }
 | 
				
			||||||
                      }
 | 
					      },
 | 
				
			||||||
                    },
 | 
					      onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
 | 
				
			||||||
                    onPayloadTransferUpdate: (endid, payloadTransferUpdate) {
 | 
					        if (payloadTransferUpdate.status == PayloadStatus.IN_PROGRESS) {
 | 
				
			||||||
                      if (payloadTransferUpdate.status ==
 | 
					          print(payloadTransferUpdate.bytesTransferred);
 | 
				
			||||||
                          PayloadStatus.IN_PROGRESS) {
 | 
					        } else if (payloadTransferUpdate.status == PayloadStatus.FAILURE) {
 | 
				
			||||||
                        print(payloadTransferUpdate.bytesTransferred);
 | 
					          print("failed");
 | 
				
			||||||
                      } else if (payloadTransferUpdate.status ==
 | 
					          showSnackbar(endid + ": FAILED to transfer file");
 | 
				
			||||||
                          PayloadStatus.FAILURE) {
 | 
					        } else if (payloadTransferUpdate.status == PayloadStatus.SUCCESS) {
 | 
				
			||||||
                        print("failed");
 | 
					          showSnackbar(
 | 
				
			||||||
                        showSnackbar(endid + ": FAILED to transfer file");
 | 
					              "$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
 | 
				
			||||||
                      } else if (payloadTransferUpdate.status ==
 | 
					 | 
				
			||||||
                          PayloadStatus.SUCCESS) {
 | 
					 | 
				
			||||||
                        showSnackbar(
 | 
					 | 
				
			||||||
                            "$endid success, total bytes = ${payloadTransferUpdate.totalBytes}");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                        if (map.containsKey(payloadTransferUpdate.id)) {
 | 
					          if (map.containsKey(payloadTransferUpdate.id)) {
 | 
				
			||||||
                          //rename the file now
 | 
					            //rename the file now
 | 
				
			||||||
                          String name = map[payloadTransferUpdate.id]!;
 | 
					            String name = map[payloadTransferUpdate.id]!;
 | 
				
			||||||
                          tempFile!.rename(tempFile!.parent.path + "/" + name);
 | 
					            tempFile!.rename(tempFile!.parent.path + "/" + name);
 | 
				
			||||||
                        } else {
 | 
					          } else {
 | 
				
			||||||
                          //bytes not received till yet
 | 
					            //bytes not received till yet
 | 
				
			||||||
                          map[payloadTransferUpdate.id] = "";
 | 
					            map[payloadTransferUpdate.id] = "";
 | 
				
			||||||
                        }
 | 
					          }
 | 
				
			||||||
                      }
 | 
					        }
 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                  );
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
              ),
 | 
					 | 
				
			||||||
              ElevatedButton(
 | 
					 | 
				
			||||||
                child: Text("Reject Connection"),
 | 
					 | 
				
			||||||
                onPressed: () async {
 | 
					 | 
				
			||||||
                  Navigator.pop(context);
 | 
					 | 
				
			||||||
                  try {
 | 
					 | 
				
			||||||
                    await Nearby().rejectConnection(id);
 | 
					 | 
				
			||||||
                  } catch (e) {
 | 
					 | 
				
			||||||
                    showSnackbar(e);
 | 
					 | 
				
			||||||
                  }
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
              ),
 | 
					 | 
				
			||||||
            ],
 | 
					 | 
				
			||||||
          ),
 | 
					 | 
				
			||||||
        );
 | 
					 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
 | 
					  ///
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -43,6 +43,13 @@ packages:
 | 
				
			|||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "1.15.0"
 | 
					    version: "1.15.0"
 | 
				
			||||||
 | 
					  crypto:
 | 
				
			||||||
 | 
					    dependency: transitive
 | 
				
			||||||
 | 
					    description:
 | 
				
			||||||
 | 
					      name: crypto
 | 
				
			||||||
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
 | 
					    source: hosted
 | 
				
			||||||
 | 
					    version: "3.0.1"
 | 
				
			||||||
  fake_async:
 | 
					  fake_async:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
@ -67,6 +74,13 @@ packages:
 | 
				
			|||||||
    description: flutter
 | 
					    description: flutter
 | 
				
			||||||
    source: sdk
 | 
					    source: sdk
 | 
				
			||||||
    version: "0.0.0"
 | 
					    version: "0.0.0"
 | 
				
			||||||
 | 
					  flutter_udid:
 | 
				
			||||||
 | 
					    dependency: "direct main"
 | 
				
			||||||
 | 
					    description:
 | 
				
			||||||
 | 
					      name: flutter_udid
 | 
				
			||||||
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
 | 
					    source: hosted
 | 
				
			||||||
 | 
					    version: "2.0.0"
 | 
				
			||||||
  flutter_web_plugins:
 | 
					  flutter_web_plugins:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description: flutter
 | 
					    description: flutter
 | 
				
			||||||
@ -219,4 +233,4 @@ packages:
 | 
				
			|||||||
    version: "2.1.0"
 | 
					    version: "2.1.0"
 | 
				
			||||||
sdks:
 | 
					sdks:
 | 
				
			||||||
  dart: ">=2.12.0 <3.0.0"
 | 
					  dart: ">=2.12.0 <3.0.0"
 | 
				
			||||||
  flutter: ">=1.20.0"
 | 
					  flutter: ">=2.0.0"
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,8 @@ dependencies:
 | 
				
			|||||||
  flutter:
 | 
					  flutter:
 | 
				
			||||||
    sdk: flutter
 | 
					    sdk: flutter
 | 
				
			||||||
  image_picker: ^0.7.4
 | 
					  image_picker: ^0.7.4
 | 
				
			||||||
 | 
					  flutter_udid: ^2.0.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  nearby_connections:
 | 
					  nearby_connections:
 | 
				
			||||||
    # When depending on this package from a real application you should use:
 | 
					    # When depending on this package from a real application you should use:
 | 
				
			||||||
    #   nearby_connections: ^x.y.z
 | 
					    #   nearby_connections: ^x.y.z
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user