diff --git a/CHANGELOG.md b/CHANGELOG.md index b39359d..9d9e169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.0.2 + +* Added payload cancellation and other assertions + ## 1.0.1 * Changed convinience methods for asking permissions(location+storage) diff --git a/README.md b/README.md index f555e2a..0a75adc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # nearby_connections -An **android** flutter plugin for the Nearby Connections API +An **android** flutter plugin for the [Nearby Connections API]((https://developers.google.com/nearby/connections/overview)) Currently supports Bytes and Files. [![pub package](https://img.shields.io/pub/v/nearby_connections.svg)](https://pub.dartlang.org/packages/nearby_connections) diff --git a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java index b473614..d76677c 100644 --- a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java +++ b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java @@ -88,7 +88,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { break; case "askExternalStoragePermission": ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0); result.success(null); break; @@ -255,11 +255,19 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { result.success(filePayload.getId()); //return payload id to dart } catch (FileNotFoundException e) { Log.e("nearby_connections", "File not found", e); - result.error("Failure", "File Not found", null); + result.error("Failure", e.getMessage(), null); return; } break; } + case "cancelPayload": { + String payloadId = (String) call.argument("payloadId"); + + assert payloadId != null; + Nearby.getConnectionsClient(activity).cancelPayload(Long.parseLong(payloadId)); + result.success(null); + break; + } default: result.notImplemented(); } @@ -367,13 +375,12 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { args.put("endpointId", endpointId); args.put("payloadId", payload.getId()); args.put("type", payload.getType()); - + if (payload.getType() == Payload.Type.BYTES) { byte[] bytes = payload.asBytes(); assert bytes != null; args.put("bytes", bytes); - } - else if (payload.getType() == Payload.Type.FILE) { + } else if (payload.getType() == Payload.Type.FILE) { args.put("filePath", payload.asFile().asJavaFile().getAbsolutePath()); } diff --git a/example/pubspec.lock b/example/pubspec.lock index f4e1581..fb2d464 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -73,7 +73,7 @@ packages: path: ".." relative: true source: path - version: "1.0.1" + version: "1.0.2" path: dependency: transitive description: diff --git a/lib/src/classes.dart b/lib/src/classes.dart index 236ae78..bbff1b7 100644 --- a/lib/src/classes.dart +++ b/lib/src/classes.dart @@ -25,6 +25,8 @@ class Payload { }); } +/// Gives payload status (SUCCESS, FAILURE, IN_PROGRESS) +/// bytes transferred and total bytes class PayloadTransferUpdate { int id, bytesTransferred, totalBytes; PayloadStatus status; diff --git a/lib/src/nearby_connections.dart b/lib/src/nearby_connections.dart index d2674f6..8267387 100644 --- a/lib/src/nearby_connections.dart +++ b/lib/src/nearby_connections.dart @@ -159,18 +159,20 @@ class Nearby { Future askLocationPermission() async => await _channel.invokeMethod( 'askLocationPermission', ); - + /// Convinience method /// /// retruns true/false based on external storage permissions. - Future checkExternalStoragePermission() async => await _channel.invokeMethod( + Future checkExternalStoragePermission() async => + await _channel.invokeMethod( 'checkExternalStoragePermission', ); /// Convinience method /// /// Asks external storage permission, required for file - Future askExternalStoragePermission() async => await _channel.invokeMethod( + Future askExternalStoragePermission() async => + await _channel.invokeMethod( 'askExternalStoragePermission', ); @@ -252,6 +254,7 @@ class Nearby { /// this will call the onDisconnected method on callbacks of /// connected endPoint Future disconnectFromEndpoint(String endpointId) async { + assert(endpointId != null); await _channel.invokeMethod( 'disconnectFromEndpoint', {'endpointId': endpointId}); } @@ -274,6 +277,9 @@ class Nearby { this._discoverConnectionResult = onConnectionResult; this._discoverDisconnected = onDisconnected; + assert(endpointId != null); + assert(userNickName != null); + return await _channel.invokeMethod( 'requestConnection', { @@ -298,6 +304,9 @@ class Nearby { }) async { this._onPayloadReceived = onPayLoadRecieved; this._onPayloadTransferUpdate = onPayloadTransferUpdate; + + assert(endpointId != null); + return await _channel.invokeMethod( 'acceptConnection', { @@ -316,6 +325,8 @@ class Nearby { /// [OnConnectionResult] is called on both /// even if one of them rejects the connection Future rejectConnection(String endpointId) async { + assert(endpointId != null); + return await _channel.invokeMethod( 'rejectConnection', { @@ -333,6 +344,8 @@ class Nearby { /// Uint8List bytes = Uint8List.fromList(a.codeUnits); /// ``` Future sendBytesPayload(String endpointId, Uint8List bytes) async { + assert(endpointId != null); + return await _channel.invokeMethod( 'sendPayload', { @@ -350,6 +363,8 @@ class Nearby { /// so that receiver can rename the file accordingly /// Send the payloadID and filename to receiver as bytes payload Future sendFilePayload(String endpointId, String filePath) async { + assert(endpointId != null); + return await _channel.invokeMethod( 'sendFilePayload', { @@ -358,4 +373,15 @@ class Nearby { }, ); } + + Future cancelPayload(int payloadId) async { + assert(payloadId != null); + + return await _channel.invokeMethod( + 'cancelPayload', + { + 'payloadId': payloadId.toString(), + }, + ); + } } diff --git a/pubspec.yaml b/pubspec.yaml index ffbe23a..79208e2 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.1 +version: 1.0.2 author: Prerak Mann homepage: https://github.com/mannprerak2/nearby_connections