feat:添加蓝牙 debug 日志
This commit is contained in:
parent
3da93adfb5
commit
23f0a19fb8
@ -1,13 +1,9 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:app_settings/app_settings.dart';
|
|
||||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:star_lock/app_settings/app_settings.dart';
|
import 'package:star_lock/app_settings/app_settings.dart';
|
||||||
import 'package:star_lock/tools/showTipView.dart';
|
|
||||||
import 'package:star_lock/widget/permission/permission_dialog.dart';
|
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
|
||||||
|
|
||||||
import 'io_tool/io_model.dart';
|
import 'io_tool/io_model.dart';
|
||||||
import 'io_tool/io_tool.dart';
|
import 'io_tool/io_tool.dart';
|
||||||
@ -21,19 +17,19 @@ typedef ConnectStateCallBack = Function(
|
|||||||
typedef ScanDevicesCallBack = Function(List<ScanResult>);
|
typedef ScanDevicesCallBack = Function(List<ScanResult>);
|
||||||
|
|
||||||
class BlueManage {
|
class BlueManage {
|
||||||
final List<ScanResult> scanDevices = [];
|
final List<ScanResult> scanDevices = <ScanResult>[];
|
||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdConnect = Guid("fff0");
|
final Guid _serviceIdConnect = Guid('fff0');
|
||||||
|
|
||||||
// 用来写入的服务id
|
// 用来写入的服务id
|
||||||
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
final Guid _serviceIdWrite = Guid('0000FFF0-0000-1000-8000-00805F9B34FB');
|
||||||
|
|
||||||
// 用来订阅的特征id
|
// 用来订阅的特征id
|
||||||
final Guid _characteristicIdSubscription = Guid("fff1");
|
final Guid _characteristicIdSubscription = Guid('fff1');
|
||||||
|
|
||||||
// 用来写入的特征id
|
// 用来写入的特征id
|
||||||
final Guid _characteristicIdWrite = Guid("fff2");
|
final Guid _characteristicIdWrite = Guid('fff2');
|
||||||
|
|
||||||
// 监听发送事件
|
// 监听发送事件
|
||||||
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
StreamSubscription<EventSendModel>? _sendStreamSubscription;
|
||||||
@ -50,10 +46,10 @@ class BlueManage {
|
|||||||
int? _mtuSize = 20;
|
int? _mtuSize = 20;
|
||||||
|
|
||||||
// 当前连接设备的名字
|
// 当前连接设备的名字
|
||||||
String connectDeviceName = "";
|
String connectDeviceName = '';
|
||||||
|
|
||||||
// 当前连接设备的mac地址
|
// 当前连接设备的mac地址
|
||||||
String connectDeviceMacAddress = "";
|
String connectDeviceMacAddress = '';
|
||||||
|
|
||||||
// 当前连接的设备
|
// 当前连接的设备
|
||||||
BluetoothDevice? bluetoothConnectDevice;
|
BluetoothDevice? bluetoothConnectDevice;
|
||||||
@ -93,7 +89,7 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _initGetMtuSubscription() {
|
void _initGetMtuSubscription() {
|
||||||
_mtuSubscription ??= bluetoothConnectDevice!.mtu.listen((value) {
|
_mtuSubscription ??= bluetoothConnectDevice!.mtu.listen((int value) {
|
||||||
_mtuSize = value - 3;
|
_mtuSize = value - 3;
|
||||||
AppLog.log('_mtuSizeValue:$value mtuSize:$_mtuSize');
|
AppLog.log('_mtuSizeValue:$value mtuSize:$_mtuSize');
|
||||||
});
|
});
|
||||||
@ -101,7 +97,7 @@ class BlueManage {
|
|||||||
|
|
||||||
void _initAdapterStateStateSubscription() {
|
void _initAdapterStateStateSubscription() {
|
||||||
_adapterStateStateSubscription ??=
|
_adapterStateStateSubscription ??=
|
||||||
FlutterBluePlus.adapterState.listen((state) {
|
FlutterBluePlus.adapterState.listen((BluetoothAdapterState state) {
|
||||||
_adapterState = state;
|
_adapterState = state;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -149,8 +145,8 @@ class BlueManage {
|
|||||||
_connectionStateSubscription?.cancel();
|
_connectionStateSubscription?.cancel();
|
||||||
_connectionStateSubscription = null;
|
_connectionStateSubscription = null;
|
||||||
|
|
||||||
_connectionStateSubscription =
|
_connectionStateSubscription = bluetoothConnectDevice!.connectionState
|
||||||
bluetoothConnectDevice!.connectionState.listen((state) async {
|
.listen((BluetoothConnectionState state) async {
|
||||||
bluetoothConnectionState = state;
|
bluetoothConnectionState = state;
|
||||||
// AppLog.log("蓝牙连接状态:$state");
|
// AppLog.log("蓝牙连接状态:$state");
|
||||||
});
|
});
|
||||||
@ -162,18 +158,18 @@ class BlueManage {
|
|||||||
.on<EventSendModel>()
|
.on<EventSendModel>()
|
||||||
.listen((EventSendModel model) {
|
.listen((EventSendModel model) {
|
||||||
if (model.sendChannel == DataChannel.ble) {
|
if (model.sendChannel == DataChannel.ble) {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((bool isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
// 蓝牙已开启,可以进行蓝牙操作
|
// 蓝牙已开启,可以进行蓝牙操作
|
||||||
writeCharacteristicWithResponse(model.data);
|
writeCharacteristicWithResponse(model.data);
|
||||||
} else {
|
} else {
|
||||||
try {} catch (e) {
|
try {} catch (e) {
|
||||||
AppLog.log("蓝牙打开失败");
|
AppLog.log('蓝牙打开失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AppLog.log("写入数据 蓝牙不可用,不能进行蓝牙操作");
|
AppLog.log('写入数据 蓝牙不可用,不能进行蓝牙操作');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -183,31 +179,33 @@ class BlueManage {
|
|||||||
/// 开始指定设备名称的扫描蓝牙设备
|
/// 开始指定设备名称的扫描蓝牙设备
|
||||||
Future<void> startScanSingle(String deviceName, int timeout,
|
Future<void> startScanSingle(String deviceName, int timeout,
|
||||||
ScanDevicesCallBack scanDevicesCallBack) async {
|
ScanDevicesCallBack scanDevicesCallBack) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((bool isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
try {
|
try {
|
||||||
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||||
Completer<dynamic> completer = Completer<dynamic>();
|
final Completer<dynamic> completer = Completer<dynamic>();
|
||||||
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
final StreamSubscription<List<ScanResult>> subscription =
|
||||||
// AppLog.log("startScanSingle扫描到的设备:$results");
|
FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||||
bool isExit = results.any((element) =>
|
final bool isExit = results.any((ScanResult element) =>
|
||||||
(element.device.platformName == deviceName) ||
|
(element.device.platformName == deviceName) ||
|
||||||
(element.advertisementData.advName == deviceName));
|
(element.advertisementData.advName == deviceName));
|
||||||
|
AppLog.log('扫描到的设备数:${results.length} 是否查找到 $isExit');
|
||||||
if (isExit) {
|
if (isExit) {
|
||||||
for (var scanResult in results) {
|
for (final ScanResult scanResult in results) {
|
||||||
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
? scanResult.advertisementData.serviceUuids[0]
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
: "")
|
: '')
|
||||||
.toString()
|
.toString()
|
||||||
.contains("758824")) &&
|
.contains('758824')) &&
|
||||||
(scanResult.rssi >= -100)) {
|
(scanResult.rssi >= -100)) {
|
||||||
// 查询id相同的元素
|
// 查询id相同的元素
|
||||||
final knownDeviceIndex = scanDevices.indexWhere((d) =>
|
final int knownDeviceIndex = scanDevices.indexWhere(
|
||||||
(d.device.platformName ==
|
(ScanResult d) =>
|
||||||
scanResult.device.platformName) ||
|
(d.device.platformName ==
|
||||||
(d.advertisementData.advName ==
|
scanResult.device.platformName) ||
|
||||||
scanResult.advertisementData.advName));
|
(d.advertisementData.advName ==
|
||||||
|
scanResult.advertisementData.advName));
|
||||||
// 不存在的时候返回-1
|
// 不存在的时候返回-1
|
||||||
if (knownDeviceIndex >= 0) {
|
if (knownDeviceIndex >= 0) {
|
||||||
scanDevices[knownDeviceIndex] = scanResult;
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
@ -220,7 +218,7 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
AppLog.log(
|
AppLog.log(
|
||||||
"扫描失败:$e",
|
'扫描失败:$e',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
@ -228,7 +226,7 @@ class BlueManage {
|
|||||||
scanDevicesCallBack(scanDevices);
|
scanDevicesCallBack(scanDevices);
|
||||||
subscription.cancel();
|
subscription.cancel();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("扫描失败");
|
AppLog.log('扫描失败');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@ -236,11 +234,11 @@ class BlueManage {
|
|||||||
await FlutterBluePlus.turnOn();
|
await FlutterBluePlus.turnOn();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("蓝牙打开失败");
|
AppLog.log('蓝牙打开失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AppLog.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
AppLog.log('开始扫描 蓝牙不可用,不能进行蓝牙操作');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -248,15 +246,16 @@ class BlueManage {
|
|||||||
/// 开始扫描蓝牙设备
|
/// 开始扫描蓝牙设备
|
||||||
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack,
|
Future<void> startScan(int timeout, ScanDevicesCallBack scanDevicesCallBack,
|
||||||
{List<Guid>? idList}) async {
|
{List<Guid>? idList}) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((bool isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
try {
|
try {
|
||||||
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
FlutterBluePlus.startScan(timeout: Duration(seconds: timeout));
|
||||||
|
|
||||||
var subscription = FlutterBluePlus.scanResults.listen((results) {
|
final StreamSubscription<List<ScanResult>> subscription =
|
||||||
|
FlutterBluePlus.scanResults.listen((List<ScanResult> results) {
|
||||||
scanDevices.clear();
|
scanDevices.clear();
|
||||||
for (var scanResult in results) {
|
for (final ScanResult scanResult in results) {
|
||||||
// 判断名字为空的直接剔除
|
// 判断名字为空的直接剔除
|
||||||
// if (scanResult.device.advName.isEmpty) {
|
// if (scanResult.device.advName.isEmpty) {
|
||||||
// return;
|
// return;
|
||||||
@ -266,16 +265,17 @@ class BlueManage {
|
|||||||
// " rssi:${scanResult.rssi}");
|
// " rssi:${scanResult.rssi}");
|
||||||
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
if (((scanResult.advertisementData.serviceUuids.isNotEmpty
|
||||||
? scanResult.advertisementData.serviceUuids[0]
|
? scanResult.advertisementData.serviceUuids[0]
|
||||||
: "")
|
: '')
|
||||||
.toString()
|
.toString()
|
||||||
.contains("758824")) &&
|
.contains('758824')) &&
|
||||||
(scanResult.rssi >= -100)) {
|
(scanResult.rssi >= -100)) {
|
||||||
// 查询id相同的元素
|
// 查询id相同的元素
|
||||||
final knownDeviceIndex = scanDevices.indexWhere((d) =>
|
final int knownDeviceIndex = scanDevices.indexWhere(
|
||||||
(d.device.platformName ==
|
(ScanResult d) =>
|
||||||
scanResult.device.platformName) ||
|
(d.device.platformName ==
|
||||||
(d.advertisementData.advName ==
|
scanResult.device.platformName) ||
|
||||||
scanResult.advertisementData.advName));
|
(d.advertisementData.advName ==
|
||||||
|
scanResult.advertisementData.advName));
|
||||||
// 不存在的时候返回-1
|
// 不存在的时候返回-1
|
||||||
if (knownDeviceIndex >= 0) {
|
if (knownDeviceIndex >= 0) {
|
||||||
scanDevices[knownDeviceIndex] = scanResult;
|
scanDevices[knownDeviceIndex] = scanResult;
|
||||||
@ -303,13 +303,13 @@ class BlueManage {
|
|||||||
// FlutterBluePlus.stopScan();
|
// FlutterBluePlus.stopScan();
|
||||||
}, onError: (e) {
|
}, onError: (e) {
|
||||||
AppLog.log(
|
AppLog.log(
|
||||||
"扫描失败:$e",
|
'扫描失败:$e',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
FlutterBluePlus.cancelWhenScanComplete(subscription);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("扫描失败");
|
AppLog.log('扫描失败');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
@ -317,11 +317,11 @@ class BlueManage {
|
|||||||
await FlutterBluePlus.turnOn();
|
await FlutterBluePlus.turnOn();
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("蓝牙打开失败");
|
AppLog.log('蓝牙打开失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AppLog.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
AppLog.log('开始扫描 蓝牙不可用,不能进行蓝牙操作');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -330,12 +330,13 @@ class BlueManage {
|
|||||||
Future<void> bludSendData(
|
Future<void> bludSendData(
|
||||||
String deviceName, ConnectStateCallBack stateCallBack,
|
String deviceName, ConnectStateCallBack stateCallBack,
|
||||||
{bool isAddEquipment = false}) async {
|
{bool isAddEquipment = false}) async {
|
||||||
FlutterBluePlus.isSupported.then((isAvailable) async {
|
FlutterBluePlus.isSupported.then((bool isAvailable) async {
|
||||||
if (isAvailable) {
|
if (isAvailable) {
|
||||||
|
AppLog.log('蓝牙状态 系统蓝牙状态:$_adapterState 蓝牙连接状态:$bluetoothConnectionState');
|
||||||
if (_adapterState == BluetoothAdapterState.on) {
|
if (_adapterState == BluetoothAdapterState.on) {
|
||||||
// 蓝牙已开启,可以进行蓝牙操作
|
// 蓝牙已开启,可以进行蓝牙操作
|
||||||
if (bluetoothConnectionState != BluetoothConnectionState.connected) {
|
if (bluetoothConnectionState != BluetoothConnectionState.connected) {
|
||||||
_connect(deviceName, (state) {
|
_connect(deviceName, (BluetoothConnectionState state) {
|
||||||
stateCallBack(bluetoothConnectionState!);
|
stateCallBack(bluetoothConnectionState!);
|
||||||
}, isAddEquipment: isAddEquipment);
|
}, isAddEquipment: isAddEquipment);
|
||||||
} else {
|
} else {
|
||||||
@ -346,11 +347,11 @@ class BlueManage {
|
|||||||
stateCallBack(BluetoothConnectionState.disconnected);
|
stateCallBack(BluetoothConnectionState.disconnected);
|
||||||
openBlue();
|
openBlue();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("蓝牙打开失败");
|
AppLog.log('蓝牙打开失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AppLog.log("开始扫描 蓝牙不可用,不能进行蓝牙操作");
|
AppLog.log('开始扫描 蓝牙不可用,不能进行蓝牙操作');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -360,9 +361,9 @@ class BlueManage {
|
|||||||
String deviceName, ConnectStateCallBack connectStateCallBack,
|
String deviceName, ConnectStateCallBack connectStateCallBack,
|
||||||
{bool isAddEquipment = false}) async {
|
{bool isAddEquipment = false}) async {
|
||||||
connectDeviceName = deviceName;
|
connectDeviceName = deviceName;
|
||||||
List<ScanResult> devicesList = scanDevices;
|
final List<ScanResult> devicesList = scanDevices;
|
||||||
|
|
||||||
bool isExistDevice = scanDevices.any((element) =>
|
final bool isExistDevice = scanDevices.any((ScanResult element) =>
|
||||||
element.device.platformName == connectDeviceName ||
|
element.device.platformName == connectDeviceName ||
|
||||||
element.advertisementData.advName == connectDeviceName);
|
element.advertisementData.advName == connectDeviceName);
|
||||||
|
|
||||||
@ -383,7 +384,7 @@ class BlueManage {
|
|||||||
{bool isAddEquipment = false}) async {
|
{bool isAddEquipment = false}) async {
|
||||||
// 判断数组列表里面是否有这个设备
|
// 判断数组列表里面是否有这个设备
|
||||||
// AppLog.log("devicesList:$devicesList");
|
// AppLog.log("devicesList:$devicesList");
|
||||||
final knownDeviceIndex = devicesList.indexWhere((d) =>
|
final int knownDeviceIndex = devicesList.indexWhere((ScanResult d) =>
|
||||||
(d.device.platformName == deviceName) ||
|
(d.device.platformName == deviceName) ||
|
||||||
(d.advertisementData.advName == deviceName));
|
(d.advertisementData.advName == deviceName));
|
||||||
|
|
||||||
@ -412,15 +413,15 @@ class BlueManage {
|
|||||||
// AppLog.log("调用了停止扫描的方法");
|
// AppLog.log("调用了停止扫描的方法");
|
||||||
await stopScan();
|
await stopScan();
|
||||||
|
|
||||||
if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == "0") &&
|
if ((scanResult.advertisementData.serviceUuids[0].toString()[31] == '0') &&
|
||||||
isAddEquipment == false) {
|
isAddEquipment == false) {
|
||||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||||
EasyLoading.showToast("该锁已被重置".tr, duration: 2000.milliseconds);
|
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重连三次
|
// 重连三次
|
||||||
int maxAttempts = 3;
|
final int maxAttempts = 3;
|
||||||
int attempt = 0;
|
int attempt = 0;
|
||||||
while (attempt < maxAttempts) {
|
while (attempt < maxAttempts) {
|
||||||
try {
|
try {
|
||||||
@ -443,14 +444,16 @@ class BlueManage {
|
|||||||
// await bluetoothConnectDevice!.connect();
|
// await bluetoothConnectDevice!.connect();
|
||||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||||
try {
|
try {
|
||||||
bluetoothConnectDevice!.discoverServices().then((services) {
|
bluetoothConnectDevice!
|
||||||
for (BluetoothService service in services) {
|
.discoverServices()
|
||||||
|
.then((List<BluetoothService> services) {
|
||||||
|
for (final BluetoothService service in services) {
|
||||||
// AppLog.log("11111service.remoteId:${service.remoteId}"
|
// AppLog.log("11111service.remoteId:${service.remoteId}"
|
||||||
// " service.uuid:${service.uuid}"
|
// " service.uuid:${service.uuid}"
|
||||||
// " service.characteristics:${service.characteristics}"
|
// " service.characteristics:${service.characteristics}"
|
||||||
// " service.includedServices:${service.includedServices}");
|
// " service.includedServices:${service.includedServices}");
|
||||||
if (service.uuid == _serviceIdConnect) {
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic
|
for (final BluetoothCharacteristic characteristic
|
||||||
in service.characteristics) {
|
in service.characteristics) {
|
||||||
// Get.log("22222characteristic.remoteId:${characteristic.remoteId}"
|
// Get.log("22222characteristic.remoteId:${characteristic.remoteId}"
|
||||||
// " characteristic.uuid:${characteristic.uuid}"
|
// " characteristic.uuid:${characteristic.uuid}"
|
||||||
@ -478,14 +481,15 @@ class BlueManage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 听上报来的数据,参数来自前面扫描到的结果
|
// 听上报来的数据,参数来自前面扫描到的结果
|
||||||
var allData = <int>[];
|
List<int> allData = <int>[];
|
||||||
|
|
||||||
// 保存上一次的数据,用来判断是否收到重复的数据
|
// 保存上一次的数据,用来判断是否收到重复的数据
|
||||||
var lastTimeData = <int>[];
|
List<int> lastTimeData = <int>[];
|
||||||
int? dataLen;
|
int? dataLen;
|
||||||
|
|
||||||
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
_subScribeToCharacteristic(BluetoothCharacteristic characteristic) async {
|
||||||
final subscription = characteristic.onValueReceived.listen((data) {
|
final StreamSubscription<List<int>> subscription =
|
||||||
|
characteristic.onValueReceived.listen((List<int> data) {
|
||||||
// AppLog.log("订阅获取的数据:$data");
|
// AppLog.log("订阅获取的数据:$data");
|
||||||
if (data == lastTimeData || data.isEmpty) {
|
if (data == lastTimeData || data.isEmpty) {
|
||||||
return;
|
return;
|
||||||
@ -580,15 +584,15 @@ class BlueManage {
|
|||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
Future<void> writeCharacteristicWithResponse(List<int> value) async {
|
Future<void> writeCharacteristicWithResponse(List<int> value) async {
|
||||||
List<BluetoothService> services =
|
final List<BluetoothService> services =
|
||||||
await bluetoothConnectDevice!.discoverServices();
|
await bluetoothConnectDevice!.discoverServices();
|
||||||
for (BluetoothService service in services) {
|
for (final BluetoothService service in services) {
|
||||||
// AppLog.log("33333 service.remoteId:${service.remoteId}"
|
// AppLog.log("33333 service.remoteId:${service.remoteId}"
|
||||||
// " service.uuid:${service.uuid}\n\n"
|
// " service.uuid:${service.uuid}\n\n"
|
||||||
// " service.characteristics:${service.characteristics}\n\n"
|
// " service.characteristics:${service.characteristics}\n\n"
|
||||||
// " service.includedServices:${service.includedServices}");
|
// " service.includedServices:${service.includedServices}");
|
||||||
if (service.uuid == _serviceIdConnect) {
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic
|
for (final BluetoothCharacteristic characteristic
|
||||||
in service.characteristics) {
|
in service.characteristics) {
|
||||||
// AppLog.log("44444 characteristic.remoteId:${characteristic.remoteId}"
|
// AppLog.log("44444 characteristic.remoteId:${characteristic.remoteId}"
|
||||||
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
// " characteristic.uuid:${characteristic.uuid}\n\n"
|
||||||
@ -598,8 +602,8 @@ class BlueManage {
|
|||||||
// .characteristicUuid}");
|
// .characteristicUuid}");
|
||||||
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
||||||
try {
|
try {
|
||||||
List<int> valueList = value;
|
final List<int> valueList = value;
|
||||||
List subData = splitList(valueList, _mtuSize!);
|
final List subData = splitList(valueList, _mtuSize!);
|
||||||
// AppLog.log('writeCharacteristicWithResponse _mtuSize:$_mtuSize 得到的分割数据:$subData');
|
// AppLog.log('writeCharacteristicWithResponse _mtuSize:$_mtuSize 得到的分割数据:$subData');
|
||||||
for (int i = 0; i < subData.length; i++) {
|
for (int i = 0; i < subData.length; i++) {
|
||||||
if (characteristic.properties.writeWithoutResponse) {
|
if (characteristic.properties.writeWithoutResponse) {
|
||||||
@ -660,15 +664,15 @@ class BlueManage {
|
|||||||
|
|
||||||
// 写入
|
// 写入
|
||||||
Future<void> writeNull() async {
|
Future<void> writeNull() async {
|
||||||
List<BluetoothService> services =
|
final List<BluetoothService> services =
|
||||||
await bluetoothConnectDevice!.discoverServices();
|
await bluetoothConnectDevice!.discoverServices();
|
||||||
for (BluetoothService service in services) {
|
for (final BluetoothService service in services) {
|
||||||
if (service.uuid == _serviceIdConnect) {
|
if (service.uuid == _serviceIdConnect) {
|
||||||
for (BluetoothCharacteristic characteristic
|
for (final BluetoothCharacteristic characteristic
|
||||||
in service.characteristics) {
|
in service.characteristics) {
|
||||||
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
if (characteristic.characteristicUuid == _characteristicIdWrite) {
|
||||||
try {
|
try {
|
||||||
List<int> valueList = [1];
|
final List<int> valueList = <int>[1];
|
||||||
AppLog.log('APP写入 writeNull ');
|
AppLog.log('APP写入 writeNull ');
|
||||||
await characteristic.write(valueList);
|
await characteristic.write(valueList);
|
||||||
} on Exception catch (e, s) {
|
} on Exception catch (e, s) {
|
||||||
@ -713,7 +717,7 @@ class BlueManage {
|
|||||||
try {
|
try {
|
||||||
await FlutterBluePlus.stopScan();
|
await FlutterBluePlus.stopScan();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
AppLog.log("停止扫描失败");
|
AppLog.log('停止扫描失败');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -721,17 +725,17 @@ class BlueManage {
|
|||||||
Future<void> disconnect() async {
|
Future<void> disconnect() async {
|
||||||
try {
|
try {
|
||||||
// if(bluetoothConnectDevice != null && bluetoothConnectDevice!.connectionState == BluetoothConnectionState.connected){
|
// if(bluetoothConnectDevice != null && bluetoothConnectDevice!.connectionState == BluetoothConnectionState.connected){
|
||||||
connectDeviceMacAddress = "";
|
connectDeviceMacAddress = '';
|
||||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||||
// await writeNull();
|
// await writeNull();
|
||||||
// await Future.delayed(const Duration(milliseconds: 1000));
|
// await Future.delayed(const Duration(milliseconds: 1000));
|
||||||
//加快蓝牙断连
|
//加快蓝牙断连
|
||||||
await bluetoothConnectDevice!.disconnect(timeout: 2);
|
await bluetoothConnectDevice!.disconnect(timeout: 2);
|
||||||
AppLog.log("断开连接成功");
|
AppLog.log('断开连接成功');
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
} on Exception catch (e, _) {
|
} on Exception catch (e, _) {
|
||||||
AppLog.log("断开连接失败: $e");
|
AppLog.log('断开连接失败: $e');
|
||||||
} finally {
|
} finally {
|
||||||
bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
||||||
}
|
}
|
||||||
@ -742,7 +746,7 @@ class BlueManage {
|
|||||||
await FlutterBluePlus.turnOn();
|
await FlutterBluePlus.turnOn();
|
||||||
}
|
}
|
||||||
if (Platform.isIOS) {
|
if (Platform.isIOS) {
|
||||||
EasyLoading.showToast("请开启蓝牙".tr, duration: 2000.milliseconds);
|
EasyLoading.showToast('请开启蓝牙'.tr, duration: 2000.milliseconds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user