@ -35,119 +35,133 @@ class Body extends StatefulWidget {
class _MyBodyState extends State < Body > {
class _MyBodyState extends State < Body > {
final String userName = Random ( ) . nextInt ( 1000 ) . toString ( ) ;
final String userName = Random ( ) . nextInt ( 1000 ) . toString ( ) ;
final Strategy strategy = Strategy . P2P_STAR ;
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
@ override
Widget build ( BuildContext context ) {
Widget build ( BuildContext context ) {
return Center (
return Center (
child: Column (
child: Column (
children: < Widget > [
children: < Widget > [
RaisedButton (
Wrap (
child: Text ( " checkPermission " ) ,
children: < Widget > [
onPressed: ( ) async {
RaisedButton (
if ( await Nearby ( ) . checkPermissions ( ) ) {
child: Text ( " checkPermission " ) ,
Scaffold . of ( context )
onPressed: ( ) async {
. showSnackBar ( SnackBar ( content: Text ( " yes " ) ) ) ;
if ( await Nearby ( ) . checkPermissions ( ) ) {
} else {
Scaffold . of ( context ) . showSnackBar ( SnackBar (
Scaffold . of ( context )
content: Text ( " Location permissions granted :) " ) ) ) ;
. showSnackBar ( SnackBar ( content: Text ( " No " ) ) ) ;
} else {
}
Scaffold . of ( context ) . showSnackBar ( SnackBar (
} ,
content: Text ( " Location permissions not granted :( " ) ) ) ;
) ,
}
RaisedButton (
} ,
child: Text ( " askPermission(location) " ) ,
) ,
onPressed: ( ) async {
RaisedButton (
await Nearby ( ) . askPermission ( ) ;
child: Text ( " askPermission " ) ,
} ,
onPressed: ( ) async {
) ,
await Nearby ( ) . askPermission ( ) ;
Text ( " UserName: " + userName ) ,
} ,
RaisedButton (
) ,
child: Text ( " Start Advertising " ) ,
] ,
onPressed: ( ) async {
try {
bool a = await Nearby ( ) . startAdvertising (
userName ,
strategy ,
onConnectionInitiated: ( id , info ) {
oci ( id , info ) ;
} ,
onConnectionResult: ( id , status ) {
showSnackbar ( status ) ;
} ,
onDisconnected: ( id ) {
showSnackbar ( id ) ;
} ,
) ;
showSnackbar ( a ) ;
} catch ( exception ) {
showSnackbar ( exception ) ;
}
} ,
) ,
) ,
RaisedButton (
Text ( " User Name: " + userName ) ,
child: Text ( " Stop Advertising " ) ,
Wrap (
onPressed: ( ) async {
children: < Widget > [
await Nearby ( ) . stopAdvertising ( ) ;
RaisedButton (
} ,
child: Text ( " Start Advertising " ) ,
onPressed: ( ) async {
try {
bool a = await Nearby ( ) . startAdvertising (
userName ,
strategy ,
onConnectionInitiated: ( id , info ) {
oci ( id , info ) ;
} ,
onConnectionResult: ( id , status ) {
showSnackbar ( status ) ;
} ,
onDisconnected: ( id ) {
showSnackbar ( " Disconnected: " + id ) ;
} ,
) ;
showSnackbar ( a ) ;
} catch ( exception ) {
showSnackbar ( exception ) ;
}
} ,
) ,
RaisedButton (
child: Text ( " Stop Advertising " ) ,
onPressed: ( ) async {
await Nearby ( ) . stopAdvertising ( ) ;
} ,
) ,
] ,
) ,
) ,
RaisedButton (
Wrap (
child: Text ( " Start Discovery " ) ,
children: < Widget > [
onPressed: ( ) async {
RaisedButton (
try {
child: Text ( " Start Discovery " ) ,
bool a = await Nearby ( ) . startDiscovery (
onPressed: ( ) async {
userName ,
try {
strategy ,
bool a = await Nearby ( ) . startDiscovery (
onEndpointFound: ( id , name , serviceId ) {
userName ,
print ( " in callback " ) ;
strategy ,
showModalBottomSheet (
onEndpointFound: ( id , name , serviceId ) {
context: context ,
print ( " in callback " ) ;
builder: ( builder ) {
showModalBottomSheet (
return Center (
context: context ,
child: Column (
builder: ( builder ) {
children: < Widget > [
return Center (
Text ( " id: " + id ) ,
child: Column (
Text ( " Name: " + name ) ,
children: < Widget > [
Text ( " ServiceId: " + serviceId ) ,
Text ( " id: " + id ) ,
RaisedButton (
Text ( " Name: " + name ) ,
child: Text ( " Request Connection " ) ,
Text ( " ServiceId: " + serviceId ) ,
onPressed: ( ) {
RaisedButton (
Navigator . pop ( context ) ;
child: Text ( " Request Connection " ) ,
Nearby ( ) . requestConnection (
onPressed: ( ) {
userName ,
Navigator . pop ( context ) ;
id ,
Nearby ( ) . requestConnection (
onConnectionInitiated: ( id , info ) {
userName ,
oci ( id , info ) ;
id ,
} ,
onConnectionInitiated: ( id , info ) {
onConnectionResult: ( id , status ) {
oci ( id , info ) ;
showSnackbar ( status ) ;
} ,
} ,
onConnectionResult: ( id , status ) {
onDisconnected: ( id ) {
showSnackbar ( status ) ;
showSnackbar ( id ) ;
} ,
onDisconnected: ( id ) {
showSnackbar ( id ) ;
} ,
) ;
} ,
} ,
) ;
) ,
} ,
] ,
) ,
) ,
],
);
) ,
} ,
) ;
) ;
} ,
} ,
onEndpointLost: ( id ) {
showSnackbar ( id ) ;
} ,
) ;
) ;
} ,
showSnackbar ( a ) ;
onEndpointLost: ( id ) {
} catch ( e ) {
showSnackbar ( id ) ;
showSnackbar ( e ) ;
} ,
}
) ;
} ,
showSnackbar ( a ) ;
) ,
} catch ( e ) {
RaisedButton (
showSnackbar ( e ) ;
child: Text ( " Stop Discovery " ) ,
}
onPressed: ( ) async {
} ,
await Nearby ( ) . stopDiscovery ( ) ;
) ,
} ,
RaisedButton (
) ,
child: Text ( " Stop Discovery " ) ,
] ,
onPressed: ( ) async {
await Nearby ( ) . stopDiscovery ( ) ;
} ,
) ,
) ,
RaisedButton (
RaisedButton (
child: Text ( " Stop All Endpoints " ) ,
child: Text ( " Stop All Endpoints " ) ,
@ -171,8 +185,12 @@ class _MyBodyState extends State<Body> {
if ( file = = null ) return ;
if ( file = = null ) return ;
Nearby ( ) . sendFilePayload ( cId , file . path ) ;
int payloadId = await Nearby ( ) . sendFilePayload ( cId , file . path ) ;
showSnackbar ( " Sending file to $ cId " ) ;
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 ;
cId = id ;
Nearby ( ) . acceptConnection (
Nearby ( ) . acceptConnection (
id ,
id ,
onPayLoadRecieved: ( endid , payload ) {
onPayLoadRecieved: ( endid , payload ) async {
if ( payload . type = = PayloadType . BYTES ) {
if ( payload . type = = PayloadType . BYTES ) {
showSnackbar (
String str = String . fromCharCodes ( payload . bytes ) ;
endid + " : " + 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 ) {
} else if ( payload . type = = PayloadType . FILE ) {
showSnackbar ( endid + " : File transfer started " ) ;
showSnackbar ( endid + " : File transfer started " ) ;
tempFile = File ( payload . filePath ) ;
}
}
} ,
} ,
onPayloadTransferUpdate: ( endid , payloadTransferUpdate ) {
onPayloadTransferUpdate: ( endid , payloadTransferUpdate ) {
@ -227,6 +265,15 @@ class _MyBodyState extends State<Body> {
" success, total bytes = ${ payloadTransferUpdate . totalBytes } " ) ;
" success, total bytes = ${ payloadTransferUpdate . totalBytes } " ) ;
showSnackbar ( endid +
showSnackbar ( endid +
" : SUCCESS in file transfer (file is un-named in downloads) " ) ;
" : 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 ] = " " ;
}
}
}
} ,
} ,
) ;
) ;