有没有办法检查我的设备是否通过dart代码支持BLE?我正在寻找这样的东西。
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.";
}
答案 0 :(得分:0)
我知道这是一个旧线程,无论如何都要进行更新,因为这可能会对其他人有所帮助。
您可以尝试使用FlutterBlue蓝牙插件,它是Flutter的新SDK。
FlutterBlue旨在从这两个平台(iOS和Android)中提供最多的收益。 FlutterBlue应该适用于Android和IOS平台。该插件还应有助于扫描和连接附近的设备。 这是文档中的示例: [https://github.com/pauldemarco/flutter_blue#obtain-an-instance][1]
获取实例:
FlutterBlue flutterBlue = FlutterBlue.instance;
isAvailable 属性检查设备是否支持蓝牙
Future<bool> get isAvailable =>
_channel.invokeMethod('isAvailable').then<bool>((d) => d);
isOn属性检查蓝牙功能是否打开:
Future<bool> get isOn => _channel.invokeMethod('isOn').then<bool>((d) => d);
扫描设备:[https://github.com/pauldemarco/flutter_blue#scan-for-devices][2]
//开始扫描
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();
连接/断开与设备的连接
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
});
您可以使用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.');
}
....
您还可以在FlutterBlue的帮助下阅读,编写字符和描述符,或执行其他操作。希望能有所帮助。
[1]: https://github.com/pauldemarco/flutter_blue#obtain-an-instance
[2]: https://github.com/pauldemarco/flutter_blue#scan-for-devices