mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
added file example, updated readme
This commit is contained in:
parent
dcd86e885f
commit
cd7e9bc63d
@ -1,6 +1,7 @@
|
|||||||
# nearby_connections
|
# nearby_connections
|
||||||
|
|
||||||
An **android** flutter plugin for the Nearby Connections API
|
An **android** flutter plugin for the Nearby Connections API
|
||||||
|
Currently supports Bytes and Files.
|
||||||
|
|
||||||
[![pub package](https://img.shields.io/pub/v/nearby_connections.svg)](https://pub.dartlang.org/packages/nearby_connections)
|
[![pub package](https://img.shields.io/pub/v/nearby_connections.svg)](https://pub.dartlang.org/packages/nearby_connections)
|
||||||
|
|
||||||
@ -27,8 +28,11 @@ Add these to AndroidManifest.xml
|
|||||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||||
|
|
||||||
|
<!-- Optional: only required for FILE payloads -->
|
||||||
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||||
```
|
```
|
||||||
Since ACCESS_FINE_LOCATION 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 the library provides methods to check and request location permissions
|
||||||
```java
|
```java
|
||||||
@ -41,7 +45,7 @@ Nearby().askPermission()
|
|||||||
|
|
||||||
## Work Flow
|
## Work Flow
|
||||||
|
|
||||||
The work flow is similar to the [android nearby connections library](https://developers.google.com/nearby/connections/overviewoverview)
|
The work flow is similar to the [Android Nearby Connections library](https://developers.google.com/nearby/connections/overview)
|
||||||
|
|
||||||
### Advertise for connection
|
### Advertise for connection
|
||||||
```dart
|
```dart
|
||||||
|
@ -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"),
|
||||||
|
onPressed: () async {
|
||||||
|
await Nearby().askPermission();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
RaisedButton(
|
Text("User Name: " + userName),
|
||||||
child: Text("askPermission(location)"),
|
Wrap(
|
||||||
onPressed: () async {
|
children: <Widget>[
|
||||||
await Nearby().askPermission();
|
RaisedButton(
|
||||||
},
|
child: Text("Start Advertising"),
|
||||||
),
|
onPressed: () async {
|
||||||
Text("UserName: " + userName),
|
try {
|
||||||
RaisedButton(
|
bool a = await Nearby().startAdvertising(
|
||||||
child: Text("Start Advertising"),
|
userName,
|
||||||
onPressed: () async {
|
strategy,
|
||||||
try {
|
onConnectionInitiated: (id, info) {
|
||||||
bool a = await Nearby().startAdvertising(
|
oci(id, info);
|
||||||
userName,
|
},
|
||||||
strategy,
|
onConnectionResult: (id, status) {
|
||||||
onConnectionInitiated: (id, info) {
|
showSnackbar(status);
|
||||||
oci(id, info);
|
},
|
||||||
},
|
onDisconnected: (id) {
|
||||||
onConnectionResult: (id, status) {
|
showSnackbar("Disconnected: "+id);
|
||||||
showSnackbar(status);
|
|
||||||
},
|
|
||||||
onDisconnected: (id) {
|
|
||||||
showSnackbar(id);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
showSnackbar(a);
|
|
||||||
} catch (exception) {
|
|
||||||
showSnackbar(exception);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
RaisedButton(
|
|
||||||
child: Text("Stop Advertising"),
|
|
||||||
onPressed: () async {
|
|
||||||
await Nearby().stopAdvertising();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
RaisedButton(
|
|
||||||
child: Text("Start Discovery"),
|
|
||||||
onPressed: () async {
|
|
||||||
try {
|
|
||||||
bool a = await Nearby().startDiscovery(
|
|
||||||
userName,
|
|
||||||
strategy,
|
|
||||||
onEndpointFound: (id, name, serviceId) {
|
|
||||||
print("in callback");
|
|
||||||
showModalBottomSheet(
|
|
||||||
context: context,
|
|
||||||
builder: (builder) {
|
|
||||||
return Center(
|
|
||||||
child: Column(
|
|
||||||
children: <Widget>[
|
|
||||||
Text("id: " + id),
|
|
||||||
Text("Name: " + name),
|
|
||||||
Text("ServiceId: " + serviceId),
|
|
||||||
RaisedButton(
|
|
||||||
child: Text("Request Connection"),
|
|
||||||
onPressed: () {
|
|
||||||
Navigator.pop(context);
|
|
||||||
Nearby().requestConnection(
|
|
||||||
userName,
|
|
||||||
id,
|
|
||||||
onConnectionInitiated: (id, info) {
|
|
||||||
oci(id, info);
|
|
||||||
},
|
|
||||||
onConnectionResult: (id, status) {
|
|
||||||
showSnackbar(status);
|
|
||||||
},
|
|
||||||
onDisconnected: (id) {
|
|
||||||
showSnackbar(id);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
},
|
showSnackbar(a);
|
||||||
onEndpointLost: (id) {
|
} catch (exception) {
|
||||||
showSnackbar(id);
|
showSnackbar(exception);
|
||||||
},
|
}
|
||||||
);
|
},
|
||||||
showSnackbar(a);
|
),
|
||||||
} catch (e) {
|
RaisedButton(
|
||||||
showSnackbar(e);
|
child: Text("Stop Advertising"),
|
||||||
}
|
onPressed: () async {
|
||||||
},
|
await Nearby().stopAdvertising();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
RaisedButton(
|
Wrap(
|
||||||
child: Text("Stop Discovery"),
|
children: <Widget>[
|
||||||
onPressed: () async {
|
RaisedButton(
|
||||||
await Nearby().stopDiscovery();
|
child: Text("Start Discovery"),
|
||||||
},
|
onPressed: () async {
|
||||||
|
try {
|
||||||
|
bool a = await Nearby().startDiscovery(
|
||||||
|
userName,
|
||||||
|
strategy,
|
||||||
|
onEndpointFound: (id, name, serviceId) {
|
||||||
|
print("in callback");
|
||||||
|
showModalBottomSheet(
|
||||||
|
context: context,
|
||||||
|
builder: (builder) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
children: <Widget>[
|
||||||
|
Text("id: " + id),
|
||||||
|
Text("Name: " + name),
|
||||||
|
Text("ServiceId: " + serviceId),
|
||||||
|
RaisedButton(
|
||||||
|
child: Text("Request Connection"),
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
Nearby().requestConnection(
|
||||||
|
userName,
|
||||||
|
id,
|
||||||
|
onConnectionInitiated: (id, info) {
|
||||||
|
oci(id, info);
|
||||||
|
},
|
||||||
|
onConnectionResult: (id, status) {
|
||||||
|
showSnackbar(status);
|
||||||
|
},
|
||||||
|
onDisconnected: (id) {
|
||||||
|
showSnackbar(id);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
onEndpointLost: (id) {
|
||||||
|
showSnackbar(id);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
showSnackbar(a);
|
||||||
|
} catch (e) {
|
||||||
|
showSnackbar(e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
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] = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/// https://pub.dev/packages/nearby_connections#-readme-tab-
|
/// https://pub.dev/packages/nearby_connections#-readme-tab-
|
||||||
library nearby_connections;
|
library nearby_connections;
|
||||||
|
|
||||||
export 'src/classes.dart';
|
export 'src/classes.dart';
|
||||||
export 'src/defs.dart';
|
export 'src/defs.dart';
|
||||||
export 'src/nearby_connections.dart';
|
export 'src/nearby_connections.dart';
|
||||||
|
@ -3,17 +3,17 @@ import 'dart:typed_data';
|
|||||||
|
|
||||||
import 'package:nearby_connections/src/defs.dart';
|
import 'package:nearby_connections/src/defs.dart';
|
||||||
|
|
||||||
/// Bytes may be null if [Payload.type] is not [PayloadType.BYTES]
|
/// Bytes may be null if [Payload.type] is not [PayloadType.BYTES]
|
||||||
/// File may be null if [Payload.type] is not [PayloadType.FILE]
|
/// File may be null if [Payload.type] is not [PayloadType.FILE]
|
||||||
///
|
///
|
||||||
/// Filepath is the complete filepath(with name) of the file
|
/// Filepath is the complete filepath(with name) of the file
|
||||||
///
|
///
|
||||||
/// The file at this location is incomplete until payloadTransferUpdate
|
/// The file at this location is incomplete until payloadTransferUpdate
|
||||||
/// gives SUCCESS for this payloadId
|
/// gives SUCCESS for this payloadId
|
||||||
class Payload {
|
class Payload {
|
||||||
int id;
|
int id;
|
||||||
PayloadType type;
|
PayloadType type;
|
||||||
|
|
||||||
Uint8List bytes;
|
Uint8List bytes;
|
||||||
String filePath;
|
String filePath;
|
||||||
|
|
||||||
@ -51,4 +51,4 @@ class ConnectionInfo {
|
|||||||
|
|
||||||
ConnectionInfo(
|
ConnectionInfo(
|
||||||
this.endpointName, this.authenticationToken, this.isIncomingConnection);
|
this.endpointName, this.authenticationToken, this.isIncomingConnection);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user