gpt4 book ai didi

swift - Flutter 中的 BLE 支持

转载 作者:行者123 更新时间:2023-12-01 16:49:30 25 4
gpt4 key购买 nike

有什么方法可以通过 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.";

}

最佳答案

我知道这是一个旧线程,无论如何都要更新它,因为它可能对其他人有帮助。

您可以尝试使用 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

关于swift - Flutter 中的 BLE 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49508243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com