added serviceId parameter to startAdvertising and startDiscovery methods

This commit is contained in:
Prerak Mann 2019-12-18 15:37:26 +05:30
parent 59ca0c8292
commit e0b80b75fc
2 changed files with 26 additions and 17 deletions

View File

@ -114,14 +114,18 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
case "startAdvertising": { case "startAdvertising": {
String userNickName = (String) call.argument("userNickName"); String userNickName = (String) call.argument("userNickName");
int strategy = (int) call.argument("strategy"); int strategy = (int) call.argument("strategy");
String serviceId = (String) call.argument("serviceId");
assert userNickName != null; assert userNickName != null;
if(serviceId==null || serviceId =="")
serviceId=SERVICE_ID;
AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder() AdvertisingOptions advertisingOptions = new AdvertisingOptions.Builder()
.setStrategy(getStrategy(strategy)).build(); .setStrategy(getStrategy(strategy)).build();
Nearby.getConnectionsClient(activity) Nearby.getConnectionsClient(activity)
.startAdvertising( .startAdvertising(
userNickName, SERVICE_ID, advertConnectionLifecycleCallback, advertisingOptions) userNickName, serviceId, advertConnectionLifecycleCallback, advertisingOptions)
.addOnSuccessListener(new OnSuccessListener<Void>() { .addOnSuccessListener(new OnSuccessListener<Void>() {
@Override @Override
public void onSuccess(Void aVoid) { public void onSuccess(Void aVoid) {
@ -140,12 +144,16 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
case "startDiscovery": { case "startDiscovery": {
String userNickName = (String) call.argument("userNickName"); String userNickName = (String) call.argument("userNickName");
int strategy = (int) call.argument("strategy"); int strategy = (int) call.argument("strategy");
String serviceId = (String) call.argument("serviceId");
assert userNickName != null; assert userNickName != null;
if(serviceId==null || serviceId =="")
serviceId=SERVICE_ID;
DiscoveryOptions discoveryOptions = DiscoveryOptions discoveryOptions =
new DiscoveryOptions.Builder().setStrategy(getStrategy(strategy)).build(); new DiscoveryOptions.Builder().setStrategy(getStrategy(strategy)).build();
Nearby.getConnectionsClient(activity) Nearby.getConnectionsClient(activity)
.startDiscovery(SERVICE_ID, endpointDiscoveryCallback, discoveryOptions) .startDiscovery(serviceId, endpointDiscoveryCallback, discoveryOptions)
.addOnSuccessListener(new OnSuccessListener<Void>() { .addOnSuccessListener(new OnSuccessListener<Void>() {
@Override @Override
public void onSuccess(Void aVoid) { public void onSuccess(Void aVoid) {

View File

@ -156,9 +156,8 @@ class Nearby {
/// Convinience method /// Convinience method
/// ///
/// Asks location permission /// Asks location permission
Future<void> askLocationPermission() async => await _channel.invokeMethod( void askLocationPermission() =>
'askLocationPermission', _channel.invokeMethod('askLocationPermission');
);
/// Convinience method /// Convinience method
/// ///
@ -171,21 +170,18 @@ class Nearby {
/// Convinience method /// Convinience method
/// ///
/// Asks external storage permission, required for file /// Asks external storage permission, required for file
Future<void> askExternalStoragePermission() async => void askExternalStoragePermission() =>
await _channel.invokeMethod( _channel.invokeMethod('askExternalStoragePermission');
'askExternalStoragePermission',
);
/// Convinience method /// Convinience method
/// ///
/// Use this instead of calling both [askLocationPermission()] and [askExternalStoragePermission()] /// Use this instead of calling both [askLocationPermission()] and [askExternalStoragePermission()]
Future<void> askLocationAndExternalStoragePermission() async => void askLocationAndExternalStoragePermission() =>
await _channel.invokeMethod( _channel.invokeMethod('askLocationAndExternalStoragePermission');
'askLocationAndExternalStoragePermission',
);
/// Start Advertising, Discoverers would be able to discover this advertiser. /// Start Advertising, Discoverers would be able to discover this advertiser.
/// ///
/// [serviceId] is a unique identifier for your app, its recommended to use your app package name only, it cannot be null
/// [userNickName] and [strategy] should not be null /// [userNickName] and [strategy] should not be null
Future<bool> startAdvertising( Future<bool> startAdvertising(
String userNickName, String userNickName,
@ -193,8 +189,9 @@ class Nearby {
@required OnConnctionInitiated onConnectionInitiated, @required OnConnctionInitiated onConnectionInitiated,
@required OnConnectionResult onConnectionResult, @required OnConnectionResult onConnectionResult,
@required OnDisconnected onDisconnected, @required OnDisconnected onDisconnected,
String serviceId = "com.pkmnapps.nearby_connections",
}) async { }) async {
assert(userNickName != null && strategy != null); assert(userNickName != null && strategy != null && serviceId != null);
this._advertConnectionInitiated = onConnectionInitiated; this._advertConnectionInitiated = onConnectionInitiated;
this._advertConnectionResult = onConnectionResult; this._advertConnectionResult = onConnectionResult;
@ -202,7 +199,8 @@ class Nearby {
return await _channel.invokeMethod('startAdvertising', <String, dynamic>{ return await _channel.invokeMethod('startAdvertising', <String, dynamic>{
'userNickName': userNickName, 'userNickName': userNickName,
'strategy': strategy.index 'strategy': strategy.index,
'serviceId': serviceId,
}); });
} }
@ -218,20 +216,23 @@ class Nearby {
/// Start Discovery, You will now be able to discover the advertisers now. /// Start Discovery, You will now be able to discover the advertisers now.
/// ///
/// [serviceId] is a unique identifier for your app, its recommended to use your app package name only, it cannot be null
/// [userNickName] and [strategy] should not be null /// [userNickName] and [strategy] should not be null
Future<bool> startDiscovery( Future<bool> startDiscovery(
String userNickName, String userNickName,
Strategy strategy, { Strategy strategy, {
@required OnEndpointFound onEndpointFound, @required OnEndpointFound onEndpointFound,
@required OnEndpointLost onEndpointLost, @required OnEndpointLost onEndpointLost,
String serviceId = "com.pkmnapps.nearby_connections",
}) async { }) async {
assert(userNickName != null && strategy != null); assert(userNickName != null && strategy != null && serviceId != null);
this._onEndpointFound = onEndpointFound; this._onEndpointFound = onEndpointFound;
this._onEndpointLost = onEndpointLost; this._onEndpointLost = onEndpointLost;
return await _channel.invokeMethod('startDiscovery', <String, dynamic>{ return await _channel.invokeMethod('startDiscovery', <String, dynamic>{
'userNickName': userNickName, 'userNickName': userNickName,
'strategy': strategy.index 'strategy': strategy.index,
'serviceId': serviceId,
}); });
} }