2020-04-15 09:37:28 +00:00
< img src = "https://developers.google.com/nearby/images/nearby_logo.svg" align = "right" >
2019-05-10 06:54:05 +00:00
# nearby_connections
2019-08-28 15:12:45 +00:00
An **android** flutter plugin for the [Nearby Connections API ](https://developers.google.com/nearby/connections/overview )
2019-08-15 19:30:35 +00:00
Currently supports Bytes and Files.
2019-05-10 06:54:05 +00:00
2019-08-28 15:12:45 +00:00
**Transfer Data between multiple connected devices using fully offline peer to peer networking**
2019-08-20 18:48:18 +00:00
2019-05-15 08:13:10 +00:00
[![pub package ](https://img.shields.io/pub/v/nearby_connections.svg )](https://pub.dartlang.org/packages/nearby_connections)
2020-10-11 08:18:14 +00:00
[![Stars ](https://img.shields.io/github/stars/mannprerak2/nearby_connections?logo=github&style=flat&color=green )](https://github.com/mannprerak2/nearby_connections)
[![Forks ](https://img.shields.io/github/forks/mannprerak2/nearby_connections?logo=github&style=flat&color=purple )](https://github.com/mannprerak2/nearby_connections)
2019-08-06 15:23:37 +00:00
## Table of Content
* [Setup ](#setup )
2019-08-06 15:47:11 +00:00
* [Work Flow ](#work-flow )
* [Advertise For connections ](#advertise-for-connection )
* [Discover Advertisers ](#discover-advertisers )
* [Request Connection ](#request-connection )
* [Accept Connection ](#accept-connection )
* [Sending Data ](#sending-data )
* [Sending Bytes Payload ](#sending-bytes-payload )
2019-08-11 18:33:47 +00:00
* [Sending Files ](#sending-file-payload )
2019-08-06 15:23:37 +00:00
## Setup
2019-05-10 06:54:05 +00:00
2020-04-14 16:09:10 +00:00
### Note regarding Location(GPS)
While using this,
**Location/GPS service must be turned on** or devices may disconnect
more often, some devices may disconnect immediately.
2019-05-10 07:33:41 +00:00
### Set Permissions
2019-05-14 19:45:54 +00:00
Add these to AndroidManifest.xml
2019-05-10 07:33:41 +00:00
```xml
<!-- Required for Nearby Connections -->
< uses-permission android:name = "android.permission.BLUETOOTH" / >
< uses-permission android:name = "android.permission.BLUETOOTH_ADMIN" / >
< uses-permission android:name = "android.permission.ACCESS_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_FINE_LOCATION" / >
2019-08-15 19:30:35 +00:00
2019-08-15 20:20:51 +00:00
<!-- Optional: only required for FILE payloads -->
2019-08-15 19:30:35 +00:00
< uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" / >
2019-08-15 20:20:51 +00:00
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" / >
2019-05-10 07:33:41 +00:00
```
2019-08-15 19:30:35 +00:00
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.
2019-05-10 06:54:05 +00:00
2020-08-15 12:32:14 +00:00
#### As a **convenience** this library provides methods to check and request location and external read/write permissions
2020-04-14 16:09:10 +00:00
```dart
2019-05-14 19:45:54 +00:00
// returns true/false asynchronously
2020-08-15 18:51:27 +00:00
bool a = await Nearby().checkLocationPermissions();
2019-08-16 05:04:57 +00:00
// asks for permission only if its not given
2020-08-15 18:51:27 +00:00
// returns true/false if the location permission is granted on/off resp.
bool b = Nearby().askLocationPermission();
2019-08-16 05:04:57 +00:00
// OPTIONAL: if you need to transfer files and rename it on device
2020-08-15 18:51:27 +00:00
bool b = Nearby().checkExternalStoragePermission();
2019-08-16 05:04:57 +00:00
// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given
2020-08-15 18:51:27 +00:00
Nearby().askExternalStoragePermission() ;
2019-12-18 11:37:33 +00:00
2020-08-15 18:51:27 +00:00
Nearby().askLocationAndExternalStoragePermission(); // for all permissions in one go..
2019-05-14 19:45:54 +00:00
```
2019-08-06 15:23:37 +00:00
## Work Flow
2019-05-14 19:45:54 +00:00
2019-08-15 19:30:35 +00:00
The work flow is similar to the [Android Nearby Connections library ](https://developers.google.com/nearby/connections/overview )
2019-05-14 19:45:54 +00:00
2020-04-14 16:09:10 +00:00
## NOTE
**Location/GPS service must be turned on** or devices may disconnect
more often, some devices may disconnect immediately.
2020-08-15 12:32:14 +00:00
For convenience this library provides methods to check and enable location
2020-04-14 16:09:10 +00:00
```dart
bool b = await Nearby().checkLocationEnabled();
2020-08-15 12:32:14 +00:00
// opens dialogue to enable location service
// returns true/false if the location service is turned on/off resp.
bool b = await Nearby().enableLocationServices();
2020-04-14 16:09:10 +00:00
```
2019-08-06 15:23:37 +00:00
### Advertise for connection
2019-05-14 19:45:54 +00:00
```dart
try {
bool a = await Nearby().startAdvertising(
userName,
strategy,
onConnectionInitiated: (String id,ConnectionInfo info) {
// Called whenever a discoverer requests connection
},
onConnectionResult: (String id,Status status) {
// Called when connection is accepted/rejected
},
2019-08-16 05:04:57 +00:00
onDisconnected: (String id) {
2019-05-14 19:45:54 +00:00
// Callled whenever a discoverer disconnects from advertiser
},
2019-12-18 11:37:33 +00:00
serviceId: "com.yourdomain.appname", // uniquely identifies your app
2019-05-14 19:45:54 +00:00
);
} catch (exception) {
// platform exceptions like unable to start bluetooth or insufficient permissions
}
```
2019-08-06 15:23:37 +00:00
### Discover Advertisers
2019-05-14 19:45:54 +00:00
```dart
try {
bool a = await Nearby().startDiscovery(
userName,
strategy,
2019-08-16 05:04:57 +00:00
onEndpointFound: (String id,String userName, String serviceId) {
2019-05-14 19:45:54 +00:00
// called when an advertiser is found
},
onEndpointLost: (String id) {
//called when an advertiser is lost (only if we weren't connected to it )
},
2019-12-18 11:37:33 +00:00
serviceId: "com.yourdomain.appname", // uniquely identifies your app
2019-05-14 19:45:54 +00:00
);
} catch (e) {
// platform exceptions like unable to start bluetooth or insufficient permissions
}
```
2019-08-06 15:23:37 +00:00
### Stopping Advertising and Discovery
2019-05-14 19:45:54 +00:00
```dart
Nearby().stopAdvertising();
Nearby().stopDiscovery();
2019-08-06 15:23:37 +00:00
// endpoints already discovered will still be available to connect
// even after stopping discovery
// You should stop discovery once you have found the intended advertiser
// this will reduce chances for disconnection
2019-05-14 19:45:54 +00:00
```
### Request Connection
```dart
// to be called by discover whenever an endpoint is found
// callbacks are similar to those in startAdvertising method
try{
Nearby().requestConnection(
userName,
id,
onConnectionInitiated: (id, info) {
},
onConnectionResult: (id, status) {
},
onDisconnected: (id) {
},
);
}catch(exception){
// called if request was invalid
}
```
2019-08-06 15:23:37 +00:00
### Accept Connection
2019-05-14 19:45:54 +00:00
```dart
Nearby().acceptConnection(
id,
2019-12-18 11:37:33 +00:00
onPayLoadRecieved: (endpointId, payload) {
2019-05-14 19:45:54 +00:00
// called whenever a payload is recieved.
},
2019-12-18 11:37:33 +00:00
onPayloadTransferUpdate: (endpointId, payloadTransferUpdate) {
2019-08-16 05:04:57 +00:00
// gives status of a payload
// e.g success/failure/in_progress
// bytes transferred and total bytes etc
}
2019-05-14 19:45:54 +00:00
);
```
2019-08-06 15:23:37 +00:00
## Sending Data
### Sending Bytes Payload
2019-05-14 19:45:54 +00:00
```dart
2019-08-11 18:33:47 +00:00
Nearby().sendBytesPayload(endpointId, bytes_array);
// payloads are recieved by callback given to acceptConnection method.
```
### Sending File Payload
You need to send the File Payload and File Name seperately.
File is stored in DOWNLOAD_DIRECTORY and given a generic name
So you would need to rename the file on receivers end.
```dart
//creates file with generic name (without extension) in Downloads Directory
2019-08-16 05:04:57 +00:00
//its your responsibility to rename the file properly
2019-08-11 18:33:47 +00:00
Nearby().sendFilePayload(endpointId, filePath);
//Send filename as well so that receiver can rename the file
2019-08-11 19:07:27 +00:00
Nearby().sendBytesPayload(endpointId,fileNameEncodedWithPayloadId);
2019-12-18 11:37:33 +00:00
//e.g send a string like "payloadId:FileExtensionOrFullName" as bytes
2019-05-14 19:45:54 +00:00
2019-08-11 19:07:27 +00:00
//payloads are recieved by callback given to acceptConnection method.
2019-05-14 19:45:54 +00:00
```
2019-08-11 19:07:27 +00:00
Every payload has an **ID** which is same for sender and receiver.
You can get the absolute FilePath from Payload in *onPayloadReceived* function
2019-08-16 05:04:57 +00:00
Checkout the [**Example** ](https://github.com/mannprerak2/nearby_connections/tree/master/example ) in Repository for more details
2019-05-14 19:45:54 +00:00