From 6486af5228ad51e67759e9180f9721e75b6164b6 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Sat, 16 Jul 2022 21:56:37 +0200 Subject: [PATCH] Add `FlutterBlue.name` to get the human readable device name This commit implements a new `FlutterBlue.name` property that will get the friendly bluetooth name of the local bluetooth adapter. --- .../com/pauldemarco/flutter_blue/FlutterBluePlugin.java | 9 +++++++++ ios/Classes/FlutterBluePlugin.m | 2 ++ lib/src/flutter_blue.dart | 4 ++++ 3 files changed, 15 insertions(+) diff --git a/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java b/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java index f3ffbdb9..1a433ace 100644 --- a/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java +++ b/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java @@ -230,6 +230,15 @@ public void onMethodCall(MethodCall call, Result result) { break; } + case "name": + { + String name = mBluetoothAdapter.getName(); + if (name == null) + name = ""; + result.success(name); + break; + } + case "isOn": { result.success(mBluetoothAdapter.isEnabled()); diff --git a/ios/Classes/FlutterBluePlugin.m b/ios/Classes/FlutterBluePlugin.m index 65a73b6d..21331f4b 100644 --- a/ios/Classes/FlutterBluePlugin.m +++ b/ios/Classes/FlutterBluePlugin.m @@ -82,6 +82,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { } else { result(@(NO)); } + } else if([@"name" isEqualToString:call.method]) { + result([[UIDevice currentDevice] name]); } else if([@"startScan" isEqualToString:call.method]) { // Clear any existing scan results [self.scannedPeripherals removeAllObjects]; diff --git a/lib/src/flutter_blue.dart b/lib/src/flutter_blue.dart index 6d2f76e9..320b6548 100644 --- a/lib/src/flutter_blue.dart +++ b/lib/src/flutter_blue.dart @@ -32,6 +32,10 @@ class FlutterBlue { Future get isAvailable => _channel.invokeMethod('isAvailable').then((d) => d); + /// Return the friendly Bluetooth name of the local Bluetooth adapter + Future get name => + _channel.invokeMethod('name').then((d) => d); + /// Checks if Bluetooth functionality is turned on Future get isOn => _channel.invokeMethod('isOn').then((d) => d);