All Projects → mannprerak2 → nearby_connections

mannprerak2 / nearby_connections

Licence: BSD-2-Clause license
Flutter plugin (android) for sharing bytes and files Offline, (Based on the android Nearby Connections API)

Programming Languages

dart
5743 projects
java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to nearby connections

Flip box bar
A 3D Bottom Navigation Bar in Flutter
Stars: ✭ 236 (+268.75%)
Mutual labels:  flutter-plugin
flutter shortcuts
Flutter plugin for creating static & dynamic app shortcuts on the home screen.
Stars: ✭ 47 (-26.56%)
Mutual labels:  flutter-plugin
flutter-scankit
Flutter QR code scanning
Stars: ✭ 107 (+67.19%)
Mutual labels:  flutter-plugin
Flutter Nfc Reader
Flutter NFC reader plugin for iOS and Android
Stars: ✭ 240 (+275%)
Mutual labels:  flutter-plugin
playify
Playify is a Flutter plugin for play/pause/seek songs, fetching music metadata, and browsing music library.
Stars: ✭ 32 (-50%)
Mutual labels:  flutter-plugin
flutter opencv
Flutter plug-in providing (a few) basic bindings to OpenCV-4.x. OpenCV methods implemented without the Core packages. WIP.
Stars: ✭ 119 (+85.94%)
Mutual labels:  flutter-plugin
Betterplayer
Better video player for Flutter, with multiple configuration options. Solving typical use cases!
Stars: ✭ 205 (+220.31%)
Mutual labels:  flutter-plugin
flutter contest
Flutter project submitted on Flutter contest
Stars: ✭ 14 (-78.12%)
Mutual labels:  flutter-plugin
flutter wechat
flutter wechat
Stars: ✭ 76 (+18.75%)
Mutual labels:  flutter-plugin
flutter ume
UME is an in-app debug kits platform for Flutter. Produced by Flutter Infra team of ByteDance
Stars: ✭ 1,792 (+2700%)
Mutual labels:  flutter-plugin
flutter bolg manage
Flutter实战项目,采用Getx框架管理,遵循Material design设计风格,适合您实战参考或练手
Stars: ✭ 373 (+482.81%)
Mutual labels:  flutter-plugin
FlutterToastPlugin
A new Flutter plugin for showing toast in android and ios.
Stars: ✭ 21 (-67.19%)
Mutual labels:  flutter-plugin
flutter cameraview
A Flutter plugin for Android and iOS allowing access to the device cameras, a bit deeper!!
Stars: ✭ 18 (-71.87%)
Mutual labels:  flutter-plugin
Sqfentity
SqfEntity ORM for Flutter/Dart lets you build and execute SQL commands on SQLite database easily and quickly with the help of fluent methods similar to .Net Entity Framework. SqfEntity also generates add/edit forms with validations and special controls (DropDown List, DateTime pickers, Checkboxes.. etc) for your table.
Stars: ✭ 237 (+270.31%)
Mutual labels:  flutter-plugin
Flutter-Mobile-Number-Plugin
Flutter Plugin to get the mobile number
Stars: ✭ 22 (-65.62%)
Mutual labels:  flutter-plugin
Flutter fluid slider
A fluid design slider that works just like the Slider material widget.
Stars: ✭ 232 (+262.5%)
Mutual labels:  flutter-plugin
flutter sliding tutorial
User onboarding library with smooth animation of objects and background colors
Stars: ✭ 127 (+98.44%)
Mutual labels:  flutter-plugin
flutter easyloading
✨A clean and lightweight loading/toast widget for Flutter, easy to use without context, support iOS、Android and Web
Stars: ✭ 1,021 (+1495.31%)
Mutual labels:  flutter-plugin
material-about
An about screen to use in your Mobile apps.
Stars: ✭ 37 (-42.19%)
Mutual labels:  flutter-plugin
scalable image
A widget that shows an image which can be scaled and dragged using gestures.
Stars: ✭ 15 (-76.56%)
Mutual labels:  flutter-plugin

nearby_connections

An android flutter plugin for the Nearby Connections API Currently supports Bytes and Files.

Transfer Data between multiple connected devices using fully offline peer to peer networking

pub package Stars Forks

Table of Content

Setup

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.

Set Permissions

Add these to AndroidManifest.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" />

<!-- Optional: only required for FILE payloads-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- For Android 12+ support-->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

Note: Android 12+ has introduced some new bluetooh permissions - BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, BLUETOOTH_SCAN, which need to be handled as well. You may also need to set compileSdkVersion 32 in your build.gradle file.

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.

You can use the permission_handler package to handle all these permissions.

Otherwise, as a convenience this library provides methods to check and request location and external read/write permissions

// returns true/false asynchronously.
bool a = await Nearby().checkLocationPermissions();
// asks for permission only if its not given
// returns true/false if the location permission is granted on/off resp.
bool b = await Nearby().askLocationPermission();

// OPTIONAL: if you need to transfer files and rename it on device
bool c = await  Nearby().checkExternalStoragePermission();
// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given
Nearby().askExternalStoragePermission();

Nearby().askLocationAndExternalStoragePermission(); // for all permissions in one go..

// For bluetooth permissions on Android 12+.
bool d = await Nearby().checkBluetoothPermission();
// asks for BLUETOOTH_ADVERTISE, BLUETOOTH_CONNECT, BLUETOOTH_SCAN permissions.
Nearby().askBluetoothPermission();

Work Flow

The work flow is similar to the Android Nearby Connections library

NOTE

Location/GPS service must be turned on or devices may disconnect more often, some devices may disconnect immediately.

For convenience this library provides methods to check and enable location

bool b = await Nearby().checkLocationEnabled();

// opens dialogue to enable location service
// returns true/false if the location service is turned on/off resp.
bool b = await Nearby().enableLocationServices();

Advertise for connection

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
        },
        onDisconnected: (String id) {
        // Callled whenever a discoverer disconnects from advertiser
        },
        serviceId: "com.yourdomain.appname", // uniquely identifies your app
    );
} catch (exception) {
    // platform exceptions like unable to start bluetooth or insufficient permissions 
}

Discover Advertisers

try {
    bool a = await Nearby().startDiscovery(
        userName,
        strategy,
        onEndpointFound: (String id,String userName, String serviceId) {
            // called when an advertiser is found
        },
        onEndpointLost: (String id) {
            //called when an advertiser is lost (only if we weren't connected to it )
        },
        serviceId: "com.yourdomain.appname", // uniquely identifies your app
    );
} catch (e) {
    // platform exceptions like unable to start bluetooth or insufficient permissions
}

Stopping Advertising and Discovery

Nearby().stopAdvertising();
Nearby().stopDiscovery();
// 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

Request Connection

// 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
}

Accept Connection

Nearby().acceptConnection(
    id,
    onPayLoadRecieved: (endpointId, payload) {
        // called whenever a payload is recieved.
    },
    onPayloadTransferUpdate: (endpointId, payloadTransferUpdate) {
        // gives status of a payload
        // e.g success/failure/in_progress
        // bytes transferred and total bytes etc
    }
);

Sending Data

Sending Bytes Payload

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/.nearby/ and given a generic name. You need to copy the file to another directory of your choice.

//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 move and rename the file
Nearby().sendBytesPayload(endpointId,fileNameEncodedWithPayloadId);
//e.g send a string like "payloadId:FileExtensionOrFullName" as bytes

//payloads are recieved by callback given to acceptConnection method.

Every payload has an ID which is same for sender and receiver.

You can get the uri of the file from Payload in onPayloadReceived function. We have a convinience method to copy the file to a location you want-

// Convinience method to copy file using it's `uri`.
final newPath = '${await getExternalStorageDirectory}/$fileName';
await Nearby().copyFileAndDeleteOriginal(uri, newPath);

Checkout the Example in Repository for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].