3

Is there any way to check if my device supports BLE through dart code? I am looking for something like this.

switch ([_manager state])
{
case CBCentralManagerStateUnsupported:
    state = @"This device does not support Bluetooth Low Energy.";
    break;
case CBCentralManagerStateUnauthorized:
    state = @"This app is not authorized to use Bluetooth Low Energy.";
    break;
case CBCentralManagerStatePoweredOff:
    state = @"Bluetooth on this device is currently powered off.";
    break;
case CBCentralManagerStateResetting:
    state = @"The BLE Manager is resetting; a state update is pending.";
    break;
case CBCentralManagerStatePoweredOn:
    state = @"Bluetooth LE is turned on and ready for communication.";
    break;
case CBCentralManagerStateUnknown:
    state = @"The state of the BLE Manager is unknown.";
    break;
default:
    state = @"The state of the BLE Manager is unknown.";

}
1

1 Answer 1

2

I know this is an old thread, Thought of updating this anyway as it might help someone else.

You can try using FlutterBlue bluetooth plugin, which is a new SDK from Flutter.

FlutterBlue aims to offer the most from both platforms (iOS and Android). FlutterBlue should work for both Android and IOS platform. The plugin should also help to scan and connect nearby devices. Here is the sample from the documentation: [https://github.com/pauldemarco/flutter_blue#obtain-an-instance][1]

Obtain an instance:

FlutterBlue flutterBlue = FlutterBlue.instance;

isAvailable property checks if whether the device supports Bluetooth

Future<bool> get isAvailable =>
    _channel.invokeMethod('isAvailable').then<bool>((d) => d);

isOn property Checks if Bluetooth functionality is turned on :

Future<bool> get isOn => _channel.invokeMethod('isOn').then<bool>((d) => d);

Scan for devices :[https://github.com/pauldemarco/flutter_blue#scan-for-devices][2]

// Start scanning

flutterBlue.startScan(timeout: Duration(seconds: 4));

// Listen to scan results
var subscription = flutterBlue.scanResults.listen((scanResult) {
    // do something with scan result
    device = scanResult.device;
    print('${device.name} found! rssi: ${scanResult.rssi}');
});

// Stop scanning
flutterBlue.stopScan();

Connect/disconnect to a device

https://github.com/pauldemarco/flutter_blue#connect-to-a-device
// Connect to the device
await device.connect();
// Disconnect from device
device.disconnect();
Discover services:
https://github.com/pauldemarco/flutter_blue#discover-services
List<BluetoothService> services = await device.discoverServices();
services.forEach((service) {
    // do something with service
});

You can scan for low energy devices with ScanMode.lowLatency :

Stream<ScanResult> scan({
 ScanMode scanMode = ScanMode.lowLatency,
 List<Guid> withServices = const [],
 List<Guid> withDevices = const [],
 Duration timeout,
}) async* {
 var settings = protos.ScanSettings.create()
   ..androidScanMode = scanMode.value
   ..serviceUuids.addAll(withServices.map((g) => g.toString()).toList());

 if (_isScanning.value == true) {
   throw Exception('Another scan is already in progress.');
 }
....

You can also read, write the characters and discriptors or do other stuff with the help of FlutterBlue. Hope that helps.

  [1]: https://github.com/pauldemarco/flutter_blue#obtain-an-instance
  [2]: https://github.com/pauldemarco/flutter_blue#scan-for-devices
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.