mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
Published 1.0.1
Fixed file transfer and completed example app
This commit is contained in:
parent
598544d7d7
commit
38dc0080d0
@ -1,3 +1,8 @@
|
|||||||
|
## 1.0.1
|
||||||
|
|
||||||
|
* Changed convinience methods for asking permissions(location+storage)
|
||||||
|
* Updated example
|
||||||
|
|
||||||
## 1.0.0
|
## 1.0.0
|
||||||
|
|
||||||
* Added support for Files (sendFilePayload)
|
* Added support for Files (sendFilePayload)
|
||||||
|
24
README.md
24
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.
|
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
|
```java
|
||||||
// returns true/false asynchronously
|
// returns true/false asynchronously
|
||||||
bool a = await Nearby().checkPermissions()
|
bool a = await Nearby().checkLocationPermissions()
|
||||||
|
// asks for permission only if its not given
|
||||||
|
Nearby().askLocationPermission()
|
||||||
|
|
||||||
// asks for permissions only if its not given
|
// OPTIONAL: if you need to transfer files and rename it on device
|
||||||
Nearby().askPermission()
|
Nearby().checkExternalStoragePermission()
|
||||||
|
// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given
|
||||||
|
Nearby().askExternalStoragePermission()
|
||||||
```
|
```
|
||||||
|
|
||||||
## Work Flow
|
## Work Flow
|
||||||
@ -60,7 +64,7 @@ try {
|
|||||||
onConnectionResult: (String id,Status status) {
|
onConnectionResult: (String id,Status status) {
|
||||||
// Called when connection is accepted/rejected
|
// Called when connection is accepted/rejected
|
||||||
},
|
},
|
||||||
onDisconnected: (id) {
|
onDisconnected: (String id) {
|
||||||
// Callled whenever a discoverer disconnects from advertiser
|
// Callled whenever a discoverer disconnects from advertiser
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@ -74,7 +78,7 @@ try {
|
|||||||
bool a = await Nearby().startDiscovery(
|
bool a = await Nearby().startDiscovery(
|
||||||
userName,
|
userName,
|
||||||
strategy,
|
strategy,
|
||||||
onEndpointFound: (String id,String name, String serviceId) {
|
onEndpointFound: (String id,String userName, String serviceId) {
|
||||||
// called when an advertiser is found
|
// called when an advertiser is found
|
||||||
},
|
},
|
||||||
onEndpointLost: (String id) {
|
onEndpointLost: (String id) {
|
||||||
@ -120,6 +124,11 @@ Nearby().acceptConnection(
|
|||||||
onPayLoadRecieved: (endid,Uint8List bytes) {
|
onPayLoadRecieved: (endid,Uint8List bytes) {
|
||||||
// called whenever a payload is recieved.
|
// 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
|
## Sending Data
|
||||||
@ -138,6 +147,7 @@ So you would need to rename the file on receivers end.
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
//creates file with generic name (without extension) in Downloads Directory
|
//creates file with generic name (without extension) in Downloads Directory
|
||||||
|
//its your responsibility to rename the file properly
|
||||||
Nearby().sendFilePayload(endpointId, filePath);
|
Nearby().sendFilePayload(endpointId, filePath);
|
||||||
|
|
||||||
//Send filename as well so that receiver can rename the file
|
//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
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,15 +2,8 @@
|
|||||||
|
|
||||||
Demonstrates how to use the nearby_connections plugin.
|
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.
|
|
||||||
|
@ -19,7 +19,7 @@ class _MyAppState extends State<MyApp> {
|
|||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
home: Scaffold(
|
home: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Plugin example app'),
|
title: const Text('Nearby Connections example app'),
|
||||||
),
|
),
|
||||||
body: Body(),
|
body: Body(),
|
||||||
),
|
),
|
||||||
@ -35,9 +35,11 @@ class Body extends StatefulWidget {
|
|||||||
class _MyBodyState extends State<Body> {
|
class _MyBodyState extends State<Body> {
|
||||||
final String userName = Random().nextInt(10000).toString();
|
final String userName = Random().nextInt(10000).toString();
|
||||||
final Strategy strategy = Strategy.P2P_STAR;
|
final Strategy strategy = Strategy.P2P_STAR;
|
||||||
|
|
||||||
String cId = "0"; //currently connected device ID
|
String cId = "0"; //currently connected device ID
|
||||||
File tempFile; //stores the file being transferred
|
File tempFile; //reference to the file currently being transferred
|
||||||
Map<int, String> map = Map();
|
Map<int, String> map =
|
||||||
|
Map(); //store filename mapped to corresponding payloadId
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -46,7 +48,9 @@ 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",),
|
Text(
|
||||||
|
"Permissions",
|
||||||
|
),
|
||||||
Wrap(
|
Wrap(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
@ -57,7 +61,8 @@ class _MyBodyState extends State<Body> {
|
|||||||
content: Text("Location permissions granted :)")));
|
content: Text("Location permissions granted :)")));
|
||||||
} else {
|
} else {
|
||||||
Scaffold.of(context).showSnackBar(SnackBar(
|
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<Body> {
|
|||||||
bool a = await Nearby().startAdvertising(
|
bool a = await Nearby().startAdvertising(
|
||||||
userName,
|
userName,
|
||||||
strategy,
|
strategy,
|
||||||
onConnectionInitiated: (id, info) {
|
onConnectionInitiated: onConnectionInit,
|
||||||
oci(id, info);
|
|
||||||
},
|
|
||||||
onConnectionResult: (id, status) {
|
onConnectionResult: (id, status) {
|
||||||
showSnackbar(status);
|
showSnackbar(status);
|
||||||
},
|
},
|
||||||
@ -110,7 +113,7 @@ class _MyBodyState extends State<Body> {
|
|||||||
showSnackbar("Disconnected: " + id);
|
showSnackbar("Disconnected: " + id);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
showSnackbar(a);
|
showSnackbar("ADVERTISING: "+a.toString());
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
showSnackbar(exception);
|
showSnackbar(exception);
|
||||||
}
|
}
|
||||||
@ -134,7 +137,7 @@ class _MyBodyState extends State<Body> {
|
|||||||
userName,
|
userName,
|
||||||
strategy,
|
strategy,
|
||||||
onEndpointFound: (id, name, serviceId) {
|
onEndpointFound: (id, name, serviceId) {
|
||||||
print("in callback");
|
// show sheet automatically to request connection
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (builder) {
|
builder: (builder) {
|
||||||
@ -152,7 +155,7 @@ class _MyBodyState extends State<Body> {
|
|||||||
userName,
|
userName,
|
||||||
id,
|
id,
|
||||||
onConnectionInitiated: (id, info) {
|
onConnectionInitiated: (id, info) {
|
||||||
oci(id, info);
|
onConnectionInit(id, info);
|
||||||
},
|
},
|
||||||
onConnectionResult: (id, status) {
|
onConnectionResult: (id, status) {
|
||||||
showSnackbar(status);
|
showSnackbar(status);
|
||||||
@ -170,10 +173,10 @@ class _MyBodyState extends State<Body> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
onEndpointLost: (id) {
|
onEndpointLost: (id) {
|
||||||
showSnackbar(id);
|
showSnackbar("Lost Endpoint:" + id);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
showSnackbar(a);
|
showSnackbar("DISCOVERING: " + a.toString());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
showSnackbar(e);
|
showSnackbar(e);
|
||||||
}
|
}
|
||||||
@ -194,7 +197,9 @@ class _MyBodyState extends State<Body> {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
Divider(),
|
Divider(),
|
||||||
Text("Sending Data",),
|
Text(
|
||||||
|
"Sending Data",
|
||||||
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("Send Random Bytes Payload"),
|
child: Text("Send Random Bytes Payload"),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
@ -231,8 +236,9 @@ class _MyBodyState extends State<Body> {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Called on a Connection request (on both devices)
|
/// Called upon Connection request (on both devices)
|
||||||
void oci(String id, ConnectionInfo info) {
|
/// Both need to accept connection to start sending/receiving
|
||||||
|
void onConnectionInit(String id, ConnectionInfo info) {
|
||||||
showModalBottomSheet(
|
showModalBottomSheet(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (builder) {
|
builder: (builder) {
|
||||||
@ -290,7 +296,7 @@ class _MyBodyState extends State<Body> {
|
|||||||
PayloadStatus.SUCCESS) {
|
PayloadStatus.SUCCESS) {
|
||||||
showSnackbar(
|
showSnackbar(
|
||||||
"success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
"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];
|
||||||
|
@ -73,7 +73,7 @@ packages:
|
|||||||
path: ".."
|
path: ".."
|
||||||
relative: true
|
relative: true
|
||||||
source: path
|
source: path
|
||||||
version: "1.0.0"
|
version: "1.0.1"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
name: nearby_connections
|
name: nearby_connections
|
||||||
description: Plugin for the android NearbyConnections API. Bytes and Files Supported.
|
description: Plugin for the android NearbyConnections API. Bytes and Files Supported.
|
||||||
version: 1.0.0
|
version: 1.0.1
|
||||||
author: Prerak Mann <mannprerak2@gmail.com>
|
author: Prerak Mann <mannprerak2@gmail.com>
|
||||||
homepage: https://github.com/mannprerak2/nearby_connections
|
homepage: https://github.com/mannprerak2/nearby_connections
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user