@ -35,32 +35,40 @@ class Body extends StatefulWidget {
class _MyBodyState extends State < Body > {
final String userName = Random ( ) . nextInt ( 1000 ) . toString ( ) ;
final Strategy strategy = Strategy . P2P_STAR ;
String cId = " 0 " ;
String cId = " 0 " ; / / currently connected device ID
File tempFile ; / / stores the file being transferred
Map < int , String > map = Map ( ) ;
@ override
Widget build ( BuildContext context ) {
return Center (
child: Column (
children: < Widget > [
Wrap (
children: < Widget > [
RaisedButton (
child: Text ( " checkPermission " ) ,
onPressed: ( ) async {
if ( await Nearby ( ) . checkPermissions ( ) ) {
Scaffold . of ( context )
. showSnackBar ( SnackBar ( content: Text ( " yes " ) ) ) ;
Scaffold . of ( context ) . showSnackBar ( SnackBar (
content: Text ( " Location permissions granted :) " ) ) ) ;
} else {
Scaffold . of ( context )
. showSnackBar ( SnackBar ( content: Text ( " No " ) ) ) ;
Scaffold . of ( context ) . showSnackBar ( SnackBar (
content: Text ( " Location permissions not granted :( " ) ) ) ;
}
} ,
) ,
RaisedButton (
child: Text ( " askPermission (location) " ) ,
child: Text ( " askPermission " ) ,
onPressed: ( ) async {
await Nearby ( ) . askPermission ( ) ;
} ,
) ,
Text ( " UserName: " + userName ) ,
] ,
) ,
Text ( " User Name: " + userName ) ,
Wrap (
children: < Widget > [
RaisedButton (
child: Text ( " Start Advertising " ) ,
onPressed: ( ) async {
@ -75,7 +83,7 @@ class _MyBodyState extends State<Body> {
showSnackbar ( status ) ;
} ,
onDisconnected: ( id ) {
showSnackbar ( id ) ;
showSnackbar ( " Disconnected: " + id ) ;
} ,
) ;
showSnackbar ( a ) ;
@ -90,6 +98,10 @@ class _MyBodyState extends State<Body> {
await Nearby ( ) . stopAdvertising ( ) ;
} ,
) ,
] ,
) ,
Wrap (
children: < Widget > [
RaisedButton (
child: Text ( " Start Discovery " ) ,
onPressed: ( ) async {
@ -149,6 +161,8 @@ class _MyBodyState extends State<Body> {
await Nearby ( ) . stopDiscovery ( ) ;
} ,
) ,
] ,
) ,
RaisedButton (
child: Text ( " Stop All Endpoints " ) ,
onPressed: ( ) async {
@ -171,8 +185,12 @@ class _MyBodyState extends State<Body> {
if ( file = = null ) return ;
Nearby ( ) . sendFilePayload ( cId , file . path ) ;
int payloadId = await Nearby ( ) . sendFilePayload ( cId , file . path ) ;
showSnackbar ( " Sending file to $ cId " ) ;
Nearby ( ) . sendBytesPayload (
cId ,
Uint8List . fromList (
" $ payloadId : ${ file . path . split ( ' / ' ) . last } " . codeUnits ) ) ;
} ,
) ,
] ,
@ -205,12 +223,32 @@ class _MyBodyState extends State<Body> {
cId = id ;
Nearby ( ) . acceptConnection (
id ,
onPayLoadRecieved: ( endid , payload ) {
onPayLoadRecieved: ( endid , payload ) async {
if ( payload . type = = PayloadType . BYTES ) {
showSnackbar (
endid + " : " + String . fromCharCodes ( payload . bytes ) ) ;
String str = String . fromCharCodes ( payload . bytes ) ;
showSnackbar ( endid + " : " + str ) ;
if ( str . contains ( ' : ' ) ) {
/ / used for file payload as file payload is mapped as
/ / payloadId: filename
int payloadId = int . parse ( str . split ( ' : ' ) [ 0 ] ) ;
String fileName = ( str . split ( ' : ' ) [ 1 ] ) ;
if ( map . containsKey ( payloadId ) ) {
if ( await tempFile . exists ( ) ) {
tempFile . rename (
tempFile . parent . path + " / " + fileName ) ;
} else {
showSnackbar ( " File doesnt exist " ) ;
}
} else {
/ / add to map if not already
map [ payloadId ] = fileName ;
}
}
} else if ( payload . type = = PayloadType . FILE ) {
showSnackbar ( endid + " : File transfer started " ) ;
tempFile = File ( payload . filePath ) ;
}
} ,
onPayloadTransferUpdate: ( endid , payloadTransferUpdate ) {
@ -227,6 +265,15 @@ class _MyBodyState extends State<Body> {
" success, total bytes = ${ payloadTransferUpdate . totalBytes } " ) ;
showSnackbar ( endid +
" : SUCCESS in file transfer (file is un-named in downloads) " ) ;
if ( map . containsKey ( payloadTransferUpdate . id ) ) {
/ / rename the file now
String name = map [ payloadTransferUpdate . id ] ;
tempFile . rename ( tempFile . parent . path + " / " + name ) ;
} else {
/ / bytes not received till yet
map [ payloadTransferUpdate . id ] = " " ;
}
}
} ,
) ;