Skip to content

Commit

Permalink
Add method for checking whether native client exists (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolak authored Apr 12, 2021
1 parent 8e6cf72 commit aaa2009
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.4.0

* Add `BleManager#isClientCreated()` for checking whether native client exists


## 2.3.2

* Fix lack of disconnection event on iOS if connection failed to be established
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,22 @@ public void onMethodCall(MethodCall call, Result result) {
case MethodName.CANCEL_TRANSACTION:
cancelTransaction(call, result);
break;
case MethodName.IS_CLIENT_CREATED:
isClientCreated(result);
break;
default:
result.notImplemented();
}
}

private void isClientCreated(Result result) {
result.success(bleAdapter != null);
}

private void createClient(MethodCall call, Result result) {
if (bleAdapter != null) {
Log.w(TAG, "Overwriting existing native client. Use BleManager#isClientCreated to check whether a client already exists.");
}
setupAdapter(context);
bleAdapter.createClient(call.<String>argument(ArgumentKey.RESTORE_STATE_IDENTIFIER),
new OnEventCallback<String>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.polidea.flutter_ble_lib.constant;

public interface MethodName {
String IS_CLIENT_CREATED = "isClientCreated";
String CREATE_CLIENT = "createClient";
String DESTROY_CLIENT = "destroyClient";

Expand Down
2 changes: 0 additions & 2 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,11 @@
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh",
"${PODS_ROOT}/../Flutter/Flutter.framework",
"${BUILT_PRODUCTS_DIR}/MultiplatformBleAdapter/MultiplatformBleAdapter.framework",
"${BUILT_PRODUCTS_DIR}/flutter_ble_lib/flutter_ble_lib.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MultiplatformBleAdapter.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_ble_lib.framework",
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions example/lib/devices_list/devices_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class DevicesBloc {

DevicesBloc(this._deviceRepository, this._bleManager);

bool clientCreated = false;

void _handlePickedDevice(BleDevice bleDevice) {
_deviceRepository.pickDevice(bleDevice);
}
Expand All @@ -43,7 +41,6 @@ class DevicesBloc {
_visibleDevicesController.close();
_devicePickerController.close();
_scanSubscription?.cancel();
_bleManager.destroyClient();
}

void init() {
Expand All @@ -69,15 +66,16 @@ class DevicesBloc {
_devicePickerSubscription = _devicePickerController.stream.listen(_handlePickedDevice);
}

Future<void> maybeCreateClient() {
if (clientCreated) {
Fimber.d("Client already exists");
Future<void> maybeCreateClient() async {
Fimber.d('Checking if client exists...');
final clientAlreadyExists = await _bleManager.isClientCreated();
Fimber.d('Client exists: $clientAlreadyExists');

if (clientAlreadyExists) {
Fimber.d("Client already exists");
return Future.value();
}

clientCreated = true;

Fimber.d("Create client");

return _bleManager
Expand All @@ -90,7 +88,6 @@ class DevicesBloc {
}
)
.catchError((e) {
clientCreated = false;
return Fimber.d("Couldn't create BLE client", ex: e);
});
}
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Constants/MethodName.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern NSString * const METHOD_NAME_IS_CLIENT_CREATED;
extern NSString * const METHOD_NAME_CREATE_CLIENT;
extern NSString * const METHOD_NAME_DESTROY_CLIENT;

Expand Down
1 change: 1 addition & 0 deletions ios/Classes/Constants/MethodName.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
NSString * const METHOD_NAME_IS_CLIENT_CREATED = @"isClientCreated";
NSString * const METHOD_NAME_CREATE_CLIENT = @"createClient";
NSString * const METHOD_NAME_DESTROY_CLIENT = @"destroyClient";

Expand Down
9 changes: 9 additions & 0 deletions ios/Classes/FlutterBleLibPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
[self createClient:call result:result];
} else if ([METHOD_NAME_DESTROY_CLIENT isEqualToString:call.method]) {
[self destroyClient];
} else if ([METHOD_NAME_IS_CLIENT_CREATED isEqualToString:call.method]) {
[self isClientCreated:call result:result];
} else if ([METHOD_NAME_ENABLE_RADIO isEqualToString:call.method]) {
[self enable:call result:result];
} else if ([METHOD_NAME_DISABLE_RADIO isEqualToString:call.method]) {
Expand Down Expand Up @@ -158,7 +160,14 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result

// MARK: - MBA Methods - BleClient lifecycle

- (void)isClientCreated:(FlutterMethodCall *)call result:(FlutterResult)result {
result([NSNumber numberWithBool:_adapter != nil]);
}

- (void)createClient:(FlutterMethodCall *)call result:(FlutterResult)result {
if (_adapter != nil) {
NSLog(@"Overwriting existing native client. Use BleManager#isClientCreated to check whether a client already exists.");
}
_adapter = [BleAdapterFactory getNewAdapterWithQueue:dispatch_get_main_queue()
restoreIdentifierKey:[ArgumentHandler stringOrNil:call.arguments[ARGUMENT_KEY_RESTORE_STATE_IDENTIFIER]]];
_adapter.delegate = self;
Expand Down
3 changes: 3 additions & 0 deletions lib/ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ abstract class BleManager {
/// for example, the result is no longer useful due to user's actions.
Future<void> cancelTransaction(String transactionId);

/// Checks whether the native client exists.
Future<bool> isClientCreated();

/// Allocates native resources.
///
/// [restoreStateIdentifier] and [restoreStateAction] are iOS-specific.
Expand Down
1 change: 1 addition & 0 deletions lib/src/_constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
abstract class MethodName {
static const String isClientCreated = "isClientCreated";
static const String createClient = "createClient";
static const String destroyClient = "destroyClient";

Expand Down
3 changes: 3 additions & 0 deletions lib/src/bridge/lib_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class FlutterBleLib extends FlutterBLE
.take(1)
.single;

Future<bool> isClientCreated() =>
_methodChannel.invokeMethod<bool>(MethodName.isClientCreated);

Future<void> createClient(String restoreStateIdentifier) async {
await _methodChannel.invokeMethod(MethodName.createClient, <String, String>{
ArgumentName.restoreStateIdentifier: restoreStateIdentifier
Expand Down
3 changes: 3 additions & 0 deletions lib/src/internal_ble_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class InternalBleManager
_bleLib.registerManager(this);
}

@override
Future<bool> isClientCreated() => _bleLib.isClientCreated();

@override
Future<void> createClient({
String restoreStateIdentifier,
Expand Down

0 comments on commit aaa2009

Please sign in to comment.