使用flutter控制蓝牙通讯_Flutter blue 蓝牙扫描连接不稳定问题

@[Flutter Blue] @[Android]

Flutter blue Android 蓝牙扫描连接不稳定问题

主要和gatt close bug 有关

https://github.com/pauldemarco/flutter_blue/blob/master/android/src/main/java/com/pauldemarco/flutter_blue/FlutterBluePlugin.java

line: 318

case “disconnect”:

{

String deviceId = (String)call.arguments;

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceId);

int state = mBluetoothManager.getConnectionState(device, BluetoothProfile.GATT);

BluetoothDeviceCache cache = mDevices.remove(deviceId);

BluetoothDeviceCache cache = mDevices.get(deviceId);

if(cache != null) {

BluetoothGatt gattServer = cache.gatt;

gattServer.disconnect();

if(state == BluetoothProfile.STATE_DISCONNECTED) {

//这里基本上不会到,因为状态还来不及改变

gattServer.close();

mDevice

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是简单的 Flutter Blue demo: 首先需要在 `pubspec.yaml` 中添加依赖: ```yaml dependencies: flutter_blue: ^0.7.3 ``` 然后在代码中导入 `flutter_blue`: ```dart import 'package:flutter_blue/flutter_blue.dart'; ``` 接下来,我们可以创建一个 `BluetoothManager` 类来管理蓝牙扫描连接: ```dart class BluetoothManager { FlutterBlue flutterBlue = FlutterBlue.instance; StreamSubscription<ScanResult> _scanSubscription; BluetoothDevice _connectedDevice; Future<bool> scanForDevices() async { try { _scanSubscription = flutterBlue.scan().listen((scanResult) { print('Found device: ${scanResult.device.name}'); }); return true; } catch (e) { print(e.toString()); return false; } } Future<bool> connectToDevice(String deviceId) async { try { var device = await flutterBlue.connect(deviceId); _connectedDevice = device; return true; } catch (e) { print(e.toString()); return false; } } void disconnectFromDevice() { if (_connectedDevice != null) { _connectedDevice.disconnect(); _connectedDevice = null; } } void stopScan() { _scanSubscription?.cancel(); _scanSubscription = null; } } ``` 此时,我们就可以在 UI 中使用上述方法了。例如,我们可以创建一个 `ScanButton`,当用户点击按钮时开始扫描: ```dart class ScanButton extends StatefulWidget { @override _ScanButtonState createState() => _ScanButtonState(); } class _ScanButtonState extends State<ScanButton> { bool isScanning = false; @override Widget build(BuildContext context) { return RaisedButton( child: Text(isScanning ? 'Stop scan' : 'Start scan'), onPressed: () async { if (isScanning) { BluetoothManager().stopScan(); } else { BluetoothManager().scanForDevices(); } setState(() { isScanning = !isScanning; }); }, ); } } ``` 我们也可以创建一个 `ConnectButton`,当用户点击按钮时连接到设备: ```dart class ConnectButton extends StatefulWidget { final String deviceId; ConnectButton({this.deviceId}); @override _ConnectButtonState createState() => _ConnectButtonState(); } class _ConnectButtonState extends State<ConnectButton> { bool isConnected = false; @override Widget build(BuildContext context) { return RaisedButton( child: Text(isConnected ? 'Disconnect' : 'Connect'), onPressed: () async { if (isConnected) { BluetoothManager().disconnectFromDevice(); } else { BluetoothManager().connectToDevice(widget.deviceId); } setState(() { isConnected = !isConnected; }); }, ); } } ``` 最后,我们可以在 UI 中使用这两个按钮: ```dart class BluetoothDemo extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Bluetooth Demo')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ScanButton(), SizedBox(height: 16), ConnectButton(deviceId: 'device-id-here'), ], ), ), ); } } ``` 请注意,这只是一个简单的 demo,并没有处理错误处理和连接状态更新等问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值