Add API to disable Bluetooth (#113)
* Add api to disable Bluetooth * Implement Android support * Fix apple build * Update Readme * Combine rows --------- Co-authored-by: Foti Dim <fdimanidis@gmail.com>
This commit is contained in:
@@ -321,6 +321,15 @@ class UniversalBle {
|
||||
);
|
||||
}
|
||||
|
||||
/// Disable Bluetooth.
|
||||
/// It might throw errors if Bluetooth is not available.
|
||||
/// Not supported on `Web` and `Apple`.
|
||||
static Future<bool> disableBluetooth() async {
|
||||
return await _bleCommandQueue.queueCommand(
|
||||
() => _platform.disableBluetooth(),
|
||||
);
|
||||
}
|
||||
|
||||
/// [receivesAdvertisements] returns true on web if the browser supports receiving advertisements from a certain `deviceId`.
|
||||
/// The rest of the platforms will always return true.
|
||||
/// If true, then you will be getting scanResult updates for this device.
|
||||
|
||||
@@ -53,6 +53,23 @@ class UniversalBleLinux extends UniversalBlePlatform {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> disableBluetooth() async {
|
||||
await _ensureInitialized();
|
||||
var adapter = _activeAdapter;
|
||||
if (adapter == null) {
|
||||
throw "Adapter not available";
|
||||
}
|
||||
if (!adapter.powered) return true;
|
||||
try {
|
||||
await adapter.setPowered(false);
|
||||
return !adapter.powered;
|
||||
} catch (e) {
|
||||
UniversalLogger.logError('Error disabling bluetooth: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> startScan({
|
||||
ScanFilter? scanFilter,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Autogenerated from Pigeon (v22.4.0), do not edit directly.
|
||||
// Autogenerated from Pigeon (v22.6.1), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import, no_leading_underscores_for_local_identifiers
|
||||
|
||||
@@ -332,6 +332,33 @@ class UniversalBlePlatformChannel {
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> disableBluetooth() async {
|
||||
final String pigeonVar_channelName = 'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.disableBluetooth$pigeonVar_messageChannelSuffix';
|
||||
final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
|
||||
pigeonVar_channelName,
|
||||
pigeonChannelCodec,
|
||||
binaryMessenger: pigeonVar_binaryMessenger,
|
||||
);
|
||||
final List<Object?>? pigeonVar_replyList =
|
||||
await pigeonVar_channel.send(null) as List<Object?>?;
|
||||
if (pigeonVar_replyList == null) {
|
||||
throw _createConnectionError(pigeonVar_channelName);
|
||||
} else if (pigeonVar_replyList.length > 1) {
|
||||
throw PlatformException(
|
||||
code: pigeonVar_replyList[0]! as String,
|
||||
message: pigeonVar_replyList[1] as String?,
|
||||
details: pigeonVar_replyList[2],
|
||||
);
|
||||
} else if (pigeonVar_replyList[0] == null) {
|
||||
throw PlatformException(
|
||||
code: 'null-error',
|
||||
message: 'Host platform returned null value for non-null return value.',
|
||||
);
|
||||
} else {
|
||||
return (pigeonVar_replyList[0] as bool?)!;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> startScan(UniversalScanFilter? filter) async {
|
||||
final String pigeonVar_channelName = 'dev.flutter.pigeon.universal_ble.UniversalBlePlatformChannel.startScan$pigeonVar_messageChannelSuffix';
|
||||
final BasicMessageChannel<Object?> pigeonVar_channel = BasicMessageChannel<Object?>(
|
||||
|
||||
@@ -28,6 +28,14 @@ class UniversalBlePigeonChannel extends UniversalBlePlatform {
|
||||
return _channel.enableBluetooth();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> disableBluetooth() {
|
||||
if (!BleCapabilities.supportsBluetoothEnableApi) {
|
||||
throw UnsupportedError("Not supported");
|
||||
}
|
||||
return _channel.disableBluetooth();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> startScan({
|
||||
ScanFilter? scanFilter,
|
||||
|
||||
@@ -12,6 +12,8 @@ abstract class UniversalBlePlatform {
|
||||
|
||||
Future<bool> enableBluetooth();
|
||||
|
||||
Future<bool> disableBluetooth();
|
||||
|
||||
Future<void> startScan({
|
||||
ScanFilter? scanFilter,
|
||||
PlatformConfig? platformConfig,
|
||||
|
||||
@@ -335,6 +335,11 @@ class UniversalBleWeb extends UniversalBlePlatform {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> disableBluetooth() {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
RequestOptionsBuilder _getRequestOptionBuilder(
|
||||
ScanFilter? scanFilter,
|
||||
WebOptions? webOptions,
|
||||
|
||||
Reference in New Issue
Block a user