feat:搜索模式,判断设备被重置的时候重置设备缓存列表
This commit is contained in:
parent
023067cdc0
commit
7f0087d153
@ -325,7 +325,8 @@ class BlueManage {
|
||||
mac != null) {
|
||||
scanSingleTimer?.cancel();
|
||||
//兼容android 的低配手机
|
||||
await doNotSearchBLE(mac, connectStateCallBack);
|
||||
await doNotSearchBLE(mac, connectStateCallBack,
|
||||
isAddEquipment: isAddEquipment);
|
||||
scanSingleTimer = Timer(3.seconds, () {
|
||||
scanSingleTimer?.cancel();
|
||||
startScanSingle(deviceName, 15, (List<ScanResult> scanDevices) => null);
|
||||
@ -384,6 +385,7 @@ class BlueManage {
|
||||
isAddEquipment == false) {
|
||||
connectStateCallBack(BluetoothConnectionState.disconnected);
|
||||
EasyLoading.showToast('该锁已被重置'.tr, duration: 2000.milliseconds);
|
||||
scanDevices.clear();
|
||||
return;
|
||||
}
|
||||
//尝试连接设备
|
||||
@ -392,7 +394,8 @@ class BlueManage {
|
||||
|
||||
//直接给蓝牙设备写入
|
||||
Future<void> doNotSearchBLE(
|
||||
String masAdds, ConnectStateCallBack connectStateCallBack) async {
|
||||
String masAdds, ConnectStateCallBack connectStateCallBack,
|
||||
{bool isAddEquipment = false}) async {
|
||||
await FlutterBluePlus.stopScan();
|
||||
if (bluetoothConnectDevice == null ||
|
||||
bluetoothConnectDevice?.remoteId.str != masAdds) {
|
||||
@ -401,12 +404,14 @@ class BlueManage {
|
||||
_initListenConnectionState();
|
||||
}
|
||||
//尝试连接设备
|
||||
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack);
|
||||
await bluetoothDeviceConnect(bluetoothConnectDevice!, connectStateCallBack,
|
||||
isAddEquipment: isAddEquipment);
|
||||
}
|
||||
|
||||
//设备连接
|
||||
Future<void> bluetoothDeviceConnect(BluetoothDevice bluetoothConnectDevice,
|
||||
ConnectStateCallBack connectStateCallBack) async {
|
||||
ConnectStateCallBack connectStateCallBack,
|
||||
{bool isAddEquipment = false}) async {
|
||||
// 重连三次
|
||||
const int maxAttempts = 3;
|
||||
int attempt = 0;
|
||||
@ -430,23 +435,22 @@ class BlueManage {
|
||||
|
||||
if (bluetoothConnectionState == BluetoothConnectionState.connected) {
|
||||
try {
|
||||
bluetoothConnectDevice!
|
||||
.discoverServices()
|
||||
.then((List<BluetoothService> services) {
|
||||
for (final BluetoothService service in services) {
|
||||
if (service.uuid == _serviceIdConnect) {
|
||||
for (final BluetoothCharacteristic characteristic
|
||||
in service.characteristics) {
|
||||
if (characteristic.characteristicUuid ==
|
||||
_characteristicIdSubscription) {
|
||||
_subScribeToCharacteristic(characteristic);
|
||||
bluetoothConnectionState = BluetoothConnectionState.connected;
|
||||
connectStateCallBack(bluetoothConnectionState!);
|
||||
}
|
||||
final List<BluetoothService> services =
|
||||
await bluetoothConnectDevice.discoverServices();
|
||||
//循环判断服务
|
||||
for (final BluetoothService service in services) {
|
||||
if (service.uuid == _serviceIdConnect) {
|
||||
for (final BluetoothCharacteristic characteristic
|
||||
in service.characteristics) {
|
||||
if (characteristic.characteristicUuid ==
|
||||
_characteristicIdSubscription) {
|
||||
_subScribeToCharacteristic(characteristic);
|
||||
bluetoothConnectionState = BluetoothConnectionState.connected;
|
||||
connectStateCallBack(bluetoothConnectionState!);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
bluetoothConnectionState = BluetoothConnectionState.disconnected;
|
||||
connectStateCallBack(bluetoothConnectionState!);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user