From d89c8b3211d92fbc75b01d1e60eb475c74572dcd Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Tue, 14 Apr 2020 21:39:10 +0530 Subject: [PATCH] updated nearby to version 17.0.0, added location enabling and checking utility method, version 1.1.0 published --- CHANGELOG.md | 7 ++ README.md | 19 +++- android/.classpath | 6 ++ android/.project | 23 ++++ .../org.eclipse.buildship.core.prefs | 13 +++ android/build.gradle | 2 +- .../NearbyConnectionsPlugin.java | 102 ++++++++++-------- example/android/.project | 17 +++ .../org.eclipse.buildship.core.prefs | 13 +++ example/android/app/.classpath | 6 ++ example/android/app/.project | 23 ++++ .../org.eclipse.buildship.core.prefs | 2 + example/android/gradle.properties | 1 + example/lib/main.dart | 34 +++++- example/pubspec.lock | 2 +- lib/src/nearby.dart | 19 +++- pubspec.yaml | 2 +- 17 files changed, 237 insertions(+), 54 deletions(-) create mode 100644 android/.classpath create mode 100644 android/.project create mode 100644 android/.settings/org.eclipse.buildship.core.prefs create mode 100644 example/android/.project create mode 100644 example/android/.settings/org.eclipse.buildship.core.prefs create mode 100644 example/android/app/.classpath create mode 100644 example/android/app/.project create mode 100644 example/android/app/.settings/org.eclipse.buildship.core.prefs diff --git a/CHANGELOG.md b/CHANGELOG.md index bf18f39..6d12349 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.1.0 +* Updated Android Nearby version from 16.0.0 to 17.0.0 +* Updated Example +* **Location/GPS service must be turned on** or devices may disconnect +more often, some devices may disconnect immediately. 2 convinience methods are added +`enableLocationServices` and `checkLocationEnabled` + ## 1.0.3 * Added serviceId parameter in startAdvertising and startDiscovery diff --git a/README.md b/README.md index b99847e..69e82c4 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ Currently supports Bytes and Files. ## 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 ```xml @@ -38,7 +43,7 @@ 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. #### As a **convinience** this library provides methods to check and request location and external read/write permissions -```java +```dart // returns true/false asynchronously bool a = await Nearby().checkLocationPermissions() // asks for permission only if its not given @@ -56,6 +61,18 @@ Nearby().askLocationAndExternalStoragePermission() // for all permissions in one The work flow is similar to the [Android Nearby Connections library](https://developers.google.com/nearby/connections/overview) +## NOTE + +**Location/GPS service must be turned on** or devices may disconnect +more often, some devices may disconnect immediately. + +For convinience this library provides methods to check and enable location +```dart +bool b = await Nearby().checkLocationEnabled(); + +// opens settings where user can enable it +Nearby().enableLocationServices(); +``` ### Advertise for connection ```dart try { diff --git a/android/.classpath b/android/.classpath new file mode 100644 index 0000000..4a04201 --- /dev/null +++ b/android/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/android/.project b/android/.project new file mode 100644 index 0000000..102ef9b --- /dev/null +++ b/android/.project @@ -0,0 +1,23 @@ + + + nearby_connections + Project android created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..b0e4650 --- /dev/null +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3)) +connection.project.dir=../example/android +eclipse.preferences.version=1 +gradle.user.home= +java.home=/usr/lib/jvm/java-11-openjdk-amd64 +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/android/build.gradle b/android/build.gradle index cd303ae..47ecd1f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -32,6 +32,6 @@ android { disable 'InvalidPackage' } dependencies{ - api 'com.google.android.gms:play-services-nearby:16.0.0' + api 'com.google.android.gms:play-services-nearby:17.0.0' } } 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 44fc9dc..82fb683 100644 --- a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java +++ b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java @@ -2,7 +2,10 @@ package com.pkmnapps.nearby_connections; import android.Manifest; import android.app.Activity; +import android.content.Intent; +import android.location.LocationManager; import android.content.pm.PackageManager; +import android.provider.Settings; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; @@ -63,42 +66,61 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { switch (call.method) { case "checkLocationPermission": - if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) - == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) - == PackageManager.PERMISSION_GRANTED) { + if (ContextCompat.checkSelfPermission(activity, + Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED + && ContextCompat.checkSelfPermission(activity, + Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { result.success(true); } else { result.success(false); } break; case "askLocationPermission": - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, - 0); - Log.d("nearby_connections", "askLocationPermission"); + ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION }, 0); + Log.d("nearby_connections", "askLocationPermission"); result.success(null); break; + case "checkLocationEnabled": + LocationManager lm = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE); + boolean gps_enabled = false; + boolean network_enabled = false; + try { + gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); + } catch (Exception ex) { + } + try { + network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER); + } catch (Exception ex) { + } + result.success(gps_enabled || network_enabled); + break; + case "enableLocationServices": + activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); + break; case "checkExternalStoragePermission": - if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) - == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) - == PackageManager.PERMISSION_GRANTED) { + if (ContextCompat.checkSelfPermission(activity, + Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED + && ContextCompat.checkSelfPermission(activity, + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { result.success(true); } else { result.success(false); } break; case "askExternalStoragePermission": - ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, - 1); - Log.d("nearby_connections", "askExternalStoragePermission"); + ActivityCompat.requestPermissions(activity, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); + Log.d("nearby_connections", "askExternalStoragePermission"); result.success(null); break; case "askLocationAndExternalStoragePermission": ActivityCompat.requestPermissions(activity, - new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, + new String[] { Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); - Log.d("nearby_connections", "askExternalStoragePermission"); + Log.d("nearby_connections", "askExternalStoragePermission"); result.success(null); break; case "stopAdvertising": @@ -117,23 +139,21 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { String serviceId = (String) call.argument("serviceId"); assert userNickName != null; - if(serviceId==null || serviceId =="") - serviceId=SERVICE_ID; + if (serviceId == null || serviceId == "") + serviceId = SERVICE_ID; AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder() .setStrategy(getStrategy(strategy)).build(); - Nearby.getConnectionsClient(activity) - .startAdvertising( - userNickName, serviceId, advertConnectionLifecycleCallback, advertisingOptions) + Nearby.getConnectionsClient(activity).startAdvertising(userNickName, serviceId, + advertConnectionLifecycleCallback, advertisingOptions) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void aVoid) { Log.d("nearby_connections", "startAdvertising"); result.success(true); } - }) - .addOnFailureListener(new OnFailureListener() { + }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Failure", e.getMessage(), null); @@ -147,11 +167,11 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { String serviceId = (String) call.argument("serviceId"); assert userNickName != null; - if(serviceId==null || serviceId =="") - serviceId=SERVICE_ID; + if (serviceId == null || serviceId == "") + serviceId = SERVICE_ID; - DiscoveryOptions discoveryOptions = - new DiscoveryOptions.Builder().setStrategy(getStrategy(strategy)).build(); + DiscoveryOptions discoveryOptions = new DiscoveryOptions.Builder().setStrategy(getStrategy(strategy)) + .build(); Nearby.getConnectionsClient(activity) .startDiscovery(serviceId, endpointDiscoveryCallback, discoveryOptions) .addOnSuccessListener(new OnSuccessListener() { @@ -160,8 +180,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { Log.d("nearby_connections", "startDiscovery"); result.success(true); } - }) - .addOnFailureListener(new OnFailureListener() { + }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Failure", e.getMessage(), null); @@ -196,8 +215,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { public void onSuccess(Void aVoid) { result.success(true); } - }) - .addOnFailureListener(new OnFailureListener() { + }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Failure", e.getMessage(), null); @@ -209,15 +227,13 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { String endpointId = (String) call.argument("endpointId"); assert endpointId != null; - Nearby.getConnectionsClient(activity) - .acceptConnection(endpointId, payloadCallback) + Nearby.getConnectionsClient(activity).acceptConnection(endpointId, payloadCallback) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void aVoid) { result.success(true); } - }) - .addOnFailureListener(new OnFailureListener() { + }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Failure", e.getMessage(), null); @@ -229,15 +245,13 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { String endpointId = (String) call.argument("endpointId"); assert endpointId != null; - Nearby.getConnectionsClient(activity) - .rejectConnection(endpointId) + Nearby.getConnectionsClient(activity).rejectConnection(endpointId) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void aVoid) { result.success(true); } - }) - .addOnFailureListener(new OnFailureListener() { + }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { result.error("Failure", e.getMessage(), null); @@ -269,7 +283,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { Payload filePayload = Payload.fromFile(file); Nearby.getConnectionsClient(activity).sendPayload(endpointId, filePayload); Log.d("nearby_connections", "sentFilePayload"); - result.success(filePayload.getId()); //return payload id to dart + result.success(filePayload.getId()); // return payload id to dart } catch (FileNotFoundException e) { Log.e("nearby_connections", "File not found", e); result.error("Failure", e.getMessage(), null); @@ -405,8 +419,9 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { } @Override - public void onPayloadTransferUpdate(@NonNull String endpointId, @NonNull PayloadTransferUpdate payloadTransferUpdate) { - //required for files and streams + public void onPayloadTransferUpdate(@NonNull String endpointId, + @NonNull PayloadTransferUpdate payloadTransferUpdate) { + // required for files and streams Log.d("nearby_connections", "onPayloadTransferUpdate"); Map args = new HashMap<>(); @@ -422,7 +437,8 @@ public class NearbyConnectionsPlugin implements MethodCallHandler { private final EndpointDiscoveryCallback endpointDiscoveryCallback = new EndpointDiscoveryCallback() { @Override - public void onEndpointFound(@NonNull String endpointId, @NonNull DiscoveredEndpointInfo discoveredEndpointInfo) { + public void onEndpointFound(@NonNull String endpointId, + @NonNull DiscoveredEndpointInfo discoveredEndpointInfo) { Log.d("nearby_connections", "onEndpointFound"); Map args = new HashMap<>(); args.put("endpointId", endpointId); diff --git a/example/android/.project b/example/android/.project new file mode 100644 index 0000000..17c95d4 --- /dev/null +++ b/example/android/.project @@ -0,0 +1,17 @@ + + + android + Project android_ created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/example/android/.settings/org.eclipse.buildship.core.prefs b/example/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..2b6d83b --- /dev/null +++ b/example/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 +gradle.user.home= +java.home=/usr/lib/jvm/java-11-openjdk-amd64 +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/example/android/app/.classpath b/example/android/app/.classpath new file mode 100644 index 0000000..4a04201 --- /dev/null +++ b/example/android/app/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/example/android/app/.project b/example/android/app/.project new file mode 100644 index 0000000..ac485d7 --- /dev/null +++ b/example/android/app/.project @@ -0,0 +1,23 @@ + + + app + Project app created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/example/android/app/.settings/org.eclipse.buildship.core.prefs b/example/android/app/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..b1886ad --- /dev/null +++ b/example/android/app/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir=.. +eclipse.preferences.version=1 diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 53ae0ae..b6e61b6 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,3 +1,4 @@ android.enableJetifier=true android.useAndroidX=true org.gradle.jvmargs=-Xmx1536M +android.enableR8=true diff --git a/example/lib/main.dart b/example/lib/main.dart index 935927d..856f854 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -68,8 +68,8 @@ class _MyBodyState extends State { ), RaisedButton( child: Text("askLocationPermission"), - onPressed: () async { - await Nearby().askLocationPermission(); + onPressed: () { + Nearby().askLocationPermission(); }, ), RaisedButton( @@ -88,8 +88,34 @@ class _MyBodyState extends State { ), RaisedButton( child: Text("askExternalStoragePermission"), + onPressed: () { + Nearby().askExternalStoragePermission(); + }, + ), + ], + ), + Divider(), + Text("Location Enabled"), + Wrap( + children: [ + RaisedButton( + child: Text("checkLocationEnabled"), onPressed: () async { - await Nearby().askExternalStoragePermission(); + if (await Nearby().checkLocationEnabled()) { + Scaffold.of(context).showSnackBar(SnackBar( + content: + Text("Location is ON :)"))); + } else { + Scaffold.of(context).showSnackBar(SnackBar( + content: Text( + "Location is OFF :("))); + } + }, + ), + RaisedButton( + child: Text("enableLocationServices"), + onPressed: () { + Nearby().enableLocationServices(); }, ), ], @@ -113,7 +139,7 @@ class _MyBodyState extends State { showSnackbar("Disconnected: " + id); }, ); - showSnackbar("ADVERTISING: "+a.toString()); + showSnackbar("ADVERTISING: " + a.toString()); } catch (exception) { showSnackbar(exception); } diff --git a/example/pubspec.lock b/example/pubspec.lock index 36df5aa..2b8b57a 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -108,7 +108,7 @@ packages: path: ".." relative: true source: path - version: "1.0.3" + version: "1.1.0" path: dependency: transitive description: diff --git a/lib/src/nearby.dart b/lib/src/nearby.dart index 6e099c9..1daa2bb 100644 --- a/lib/src/nearby.dart +++ b/lib/src/nearby.dart @@ -163,9 +163,22 @@ class Nearby { /// /// retruns true/false based on external storage permissions. Future checkExternalStoragePermission() async => - await _channel.invokeMethod( - 'checkExternalStoragePermission', - ); + await _channel.invokeMethod('checkExternalStoragePermission'); + + /// Convinience method + /// + /// Checks if Location/GPS is enabled + /// + /// If Location isn't enabled, devices may disconnect often. + /// Some devices may immediately disconnect + Future checkLocationEnabled() async => + await _channel.invokeMethod('checkLocationEnabled'); + + /// Convinience method + /// + /// directs user to Location Settings, so they can turn on their Location/GPS + void enableLocationServices() => + _channel.invokeMethod('enableLocationServices'); /// Convinience method /// diff --git a/pubspec.yaml b/pubspec.yaml index 478e1ee..bc0e901 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.3 +version: 1.1.0 homepage: https://github.com/mannprerak2/nearby_connections environment: