diff --git a/CHANGELOG.md b/CHANGELOG.md index 37ce36c..b39359d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.0.1 + +* Changed convinience methods for asking permissions(location+storage) +* Updated example + ## 1.0.0 * Added support for Files (sendFilePayload) diff --git a/README.md b/README.md index c3c79fb..f555e2a 100644 --- a/README.md +++ b/README.md @@ -35,13 +35,17 @@ Add these to AndroidManifest.xml ``` Since ACCESS_FINE_LOCATION and READ_EXTERNAL_STORAGE is considered to be dangerous system permissions, in addition to adding them to your manifest, you must request these permissions at runtime. -##### As a convinience the library provides methods to check and request location permissions +#### As a **convinience** this library provides methods to check and request location and external read/write permissions ```java // returns true/false asynchronously -bool a = await Nearby().checkPermissions() - -// asks for permissions only if its not given -Nearby().askPermission() +bool a = await Nearby().checkLocationPermissions() +// asks for permission only if its not given +Nearby().askLocationPermission() + +// OPTIONAL: if you need to transfer files and rename it on device +Nearby().checkExternalStoragePermission() +// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given +Nearby().askExternalStoragePermission() ``` ## Work Flow @@ -60,7 +64,7 @@ try { onConnectionResult: (String id,Status status) { // Called when connection is accepted/rejected }, - onDisconnected: (id) { + onDisconnected: (String id) { // Callled whenever a discoverer disconnects from advertiser }, ); @@ -74,7 +78,7 @@ try { bool a = await Nearby().startDiscovery( userName, strategy, - onEndpointFound: (String id,String name, String serviceId) { + onEndpointFound: (String id,String userName, String serviceId) { // called when an advertiser is found }, onEndpointLost: (String id) { @@ -120,6 +124,11 @@ Nearby().acceptConnection( onPayLoadRecieved: (endid,Uint8List bytes) { // called whenever a payload is recieved. }, + onPayloadTransferUpdate: (endid, payloadTransferUpdate) { + // gives status of a payload + // e.g success/failure/in_progress + // bytes transferred and total bytes etc + } ); ``` ## Sending Data @@ -138,6 +147,7 @@ So you would need to rename the file on receivers end. ```dart //creates file with generic name (without extension) in Downloads Directory +//its your responsibility to rename the file properly Nearby().sendFilePayload(endpointId, filePath); //Send filename as well so that receiver can rename the file @@ -150,7 +160,7 @@ Every payload has an **ID** which is same for sender and receiver. You can get the absolute FilePath from Payload in *onPayloadReceived* function -Check **Example** in Repository for more details +Checkout the [**Example**](https://github.com/mannprerak2/nearby_connections/tree/master/example) in Repository for more details diff --git a/example/README.md b/example/README.md index 5b64c6e..eb9393f 100644 --- a/example/README.md +++ b/example/README.md @@ -2,15 +2,8 @@ Demonstrates how to use the nearby_connections plugin. -## Getting Started +Checkout the [**Example**](https://github.com/mannprerak2/nearby_connections/tree/master/example) repository for implementation of sending bytes and files. -This project is a starting point for a Flutter application. +Other examples using this is + - [Monoply Money Handler](https://github.com/mannprerak2/monopoly_money_game) -A few resources to get you started if this is your first Flutter project: - -- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab) -- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook) - -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. diff --git a/example/lib/main.dart b/example/lib/main.dart index 80de29b..935927d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,7 +19,7 @@ class _MyAppState extends State { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('Plugin example app'), + title: const Text('Nearby Connections example app'), ), body: Body(), ), @@ -35,9 +35,11 @@ class Body extends StatefulWidget { class _MyBodyState extends State { final String userName = Random().nextInt(10000).toString(); final Strategy strategy = Strategy.P2P_STAR; + String cId = "0"; //currently connected device ID - File tempFile; //stores the file being transferred - Map map = Map(); + File tempFile; //reference to the file currently being transferred + Map map = + Map(); //store filename mapped to corresponding payloadId @override Widget build(BuildContext context) { @@ -46,7 +48,9 @@ class _MyBodyState extends State { padding: const EdgeInsets.all(8.0), child: ListView( children: [ - Text("Permissions",), + Text( + "Permissions", + ), Wrap( children: [ RaisedButton( @@ -57,7 +61,8 @@ class _MyBodyState extends State { content: Text("Location permissions granted :)"))); } else { Scaffold.of(context).showSnackBar(SnackBar( - content: Text("Location permissions not granted :("))); + content: + Text("Location permissions not granted :("))); } }, ), @@ -100,9 +105,7 @@ class _MyBodyState extends State { bool a = await Nearby().startAdvertising( userName, strategy, - onConnectionInitiated: (id, info) { - oci(id, info); - }, + onConnectionInitiated: onConnectionInit, onConnectionResult: (id, status) { showSnackbar(status); }, @@ -110,7 +113,7 @@ class _MyBodyState extends State { showSnackbar("Disconnected: " + id); }, ); - showSnackbar(a); + showSnackbar("ADVERTISING: "+a.toString()); } catch (exception) { showSnackbar(exception); } @@ -134,7 +137,7 @@ class _MyBodyState extends State { userName, strategy, onEndpointFound: (id, name, serviceId) { - print("in callback"); + // show sheet automatically to request connection showModalBottomSheet( context: context, builder: (builder) { @@ -152,7 +155,7 @@ class _MyBodyState extends State { userName, id, onConnectionInitiated: (id, info) { - oci(id, info); + onConnectionInit(id, info); }, onConnectionResult: (id, status) { showSnackbar(status); @@ -170,10 +173,10 @@ class _MyBodyState extends State { ); }, onEndpointLost: (id) { - showSnackbar(id); + showSnackbar("Lost Endpoint:" + id); }, ); - showSnackbar(a); + showSnackbar("DISCOVERING: " + a.toString()); } catch (e) { showSnackbar(e); } @@ -194,7 +197,9 @@ class _MyBodyState extends State { }, ), Divider(), - Text("Sending Data",), + Text( + "Sending Data", + ), RaisedButton( child: Text("Send Random Bytes Payload"), onPressed: () async { @@ -231,8 +236,9 @@ class _MyBodyState extends State { )); } - /// Called on a Connection request (on both devices) - void oci(String id, ConnectionInfo info) { + /// Called upon Connection request (on both devices) + /// Both need to accept connection to start sending/receiving + void onConnectionInit(String id, ConnectionInfo info) { showModalBottomSheet( context: context, builder: (builder) { @@ -290,7 +296,7 @@ class _MyBodyState extends State { PayloadStatus.SUCCESS) { showSnackbar( "success, total bytes = ${payloadTransferUpdate.totalBytes}"); - + if (map.containsKey(payloadTransferUpdate.id)) { //rename the file now String name = map[payloadTransferUpdate.id]; diff --git a/example/pubspec.lock b/example/pubspec.lock index 3a0dfaf..f4e1581 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -73,7 +73,7 @@ packages: path: ".." relative: true source: path - version: "1.0.0" + version: "1.0.1" path: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 5d3faff..ffbe23a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: nearby_connections description: Plugin for the android NearbyConnections API. Bytes and Files Supported. -version: 1.0.0 +version: 1.0.1 author: Prerak Mann homepage: https://github.com/mannprerak2/nearby_connections