|
|
@ -1,3 +1,6 @@ |
|
|
|
|
|
import 'dart:convert'; |
|
|
|
|
|
import 'dart:typed_data'; |
|
|
|
|
|
|
|
|
import 'package:app_settings/app_settings.dart'; |
|
|
import 'package:app_settings/app_settings.dart'; |
|
|
import 'package:deep_voice/data/models/user_Info.dart'; |
|
|
import 'package:deep_voice/data/models/user_Info.dart'; |
|
|
import 'package:deep_voice/data/services/network/api.dart'; |
|
|
import 'package:deep_voice/data/services/network/api.dart'; |
|
|
@ -418,41 +421,32 @@ class PairingController extends GetxController { |
|
|
// TODO: 实现API验证逻辑 |
|
|
// TODO: 实现API验证逻辑 |
|
|
// 暂时模拟验证通过 |
|
|
// 暂时模拟验证通过 |
|
|
// await Future.delayed(const Duration(seconds: 2));; |
|
|
// await Future.delayed(const Duration(seconds: 2));; |
|
|
if (await authbindingDevice()) { |
|
|
if (await authbindingDevice(device)) { |
|
|
// 检查设备名称是否包含 DeepSound 或 DeapSound |
|
|
Logger.i(_tag, '设备验证通过'); |
|
|
final deviceName = device.name ?? ''; |
|
|
_verifiedDeviceAddress = device.address; |
|
|
if (deviceName.contains('DeepSound') || |
|
|
// 检查是否已有BLE关联 |
|
|
deviceName.contains('DeapSound')) { |
|
|
final pairedMacAddress = await _bleManager.getPairedMacAddress(); |
|
|
Logger.i(_tag, '设备验证通过'); |
|
|
Logger.i(_tag, '已关联的MAC地址: $pairedMacAddress'); |
|
|
_verifiedDeviceAddress = device.address; |
|
|
|
|
|
|
|
|
if (pairedMacAddress != null && |
|
|
// 检查是否已有BLE关联 |
|
|
pairedMacAddress.toUpperCase() == device.address.toUpperCase()) { |
|
|
final pairedMacAddress = await _bleManager.getPairedMacAddress(); |
|
|
// 已有关联,检查BLE连接状态 |
|
|
Logger.i(_tag, '已关联的MAC地址: $pairedMacAddress'); |
|
|
Logger.i(_tag, '设备已关联,检查BLE连接状态'); |
|
|
|
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
if (pairedMacAddress != null && |
|
|
Logger.i(_tag, '验证时BLE状态: $currentBleState'); |
|
|
pairedMacAddress.toUpperCase() == device.address.toUpperCase()) { |
|
|
|
|
|
// 已有关联,检查BLE连接状态 |
|
|
if (currentBleState == 2) { |
|
|
Logger.i(_tag, '设备已关联,检查BLE连接状态'); |
|
|
// BleConnectionState.connected |
|
|
final currentBleState = await _bleManager.getConnectionState(); |
|
|
Logger.i(_tag, 'BLE已连接,直接完成配对'); |
|
|
Logger.i(_tag, '验证时BLE状态: $currentBleState'); |
|
|
_transition(PairingState.completed); |
|
|
|
|
|
|
|
|
if (currentBleState == 2) { |
|
|
|
|
|
// BleConnectionState.connected |
|
|
|
|
|
Logger.i(_tag, 'BLE已连接,直接完成配对'); |
|
|
|
|
|
_transition(PairingState.completed); |
|
|
|
|
|
} else { |
|
|
|
|
|
Logger.i(_tag, 'BLE未连接,等待BLE连接'); |
|
|
|
|
|
_transition(PairingState.waitingBle); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// 没有关联或关联的是其他设备,需要进行关联 |
|
|
Logger.i(_tag, 'BLE未连接,等待BLE连接'); |
|
|
Logger.i(_tag, '需要进行BLE设备关联'); |
|
|
_transition(PairingState.waitingBle); |
|
|
_transition(PairingState.associating); |
|
|
|
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
// _setError(ErrorType.deviceNotMatch, '请连接 DeepSound 设备'); |
|
|
// 没有关联或关联的是其他设备,需要进行关联 |
|
|
_transition(PairingState.completed); |
|
|
Logger.i(_tag, '需要进行BLE设备关联'); |
|
|
|
|
|
_transition(PairingState.associating); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
//验证失败 进入手动授权 |
|
|
//验证失败 进入手动授权 |
|
|
@ -531,27 +525,76 @@ class PairingController extends GetxController { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//自动绑定设备 |
|
|
//自动绑定设备 |
|
|
Future<bool> authbindingDevice() async { |
|
|
Future<bool> authbindingDevice(BluetoothDevice device) async { |
|
|
if (User.instance.earphonemac == currentDevice?.address) { |
|
|
if (User.instance.earphonemac == device.address) { |
|
|
// 已经绑定 |
|
|
// 已经绑定 |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 调用BLE管理器的自动绑定方法 |
|
|
// 检查设备名称是否包含 DeepSound 或 DeapSound |
|
|
final response = await Api.bindearphone({ |
|
|
final deviceName = device.name ?? ''; |
|
|
"earphonemac": currentDevice?.address, |
|
|
print('获取到 deviceName: $deviceName'); |
|
|
}); |
|
|
if (deviceName.contains('DeepSound') || deviceName.contains('DeapSound')) { |
|
|
Logger.i(_tag, '手动授权结果 ${response}'); |
|
|
await _bleManager.startScan(); |
|
|
if (response != null) { |
|
|
// 尝试最多读取 3 次设备信息,每次间隔 1 秒 |
|
|
Logger.i(_tag, '设备自动绑定成功'); |
|
|
Map<String, String>? deviceinfo; |
|
|
User.instance.earphonemac = currentDevice?.address ?? ''; |
|
|
for (int i = 0; i < 3; i++) { |
|
|
return true; |
|
|
deviceinfo = await _bleManager.getDeviceInfo(device.address); |
|
|
|
|
|
print('获取到 deviceinfo: $deviceinfo'); |
|
|
|
|
|
if (deviceinfo != null) { |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
await Future.delayed(Duration(seconds: 1)); |
|
|
|
|
|
} |
|
|
|
|
|
print('获取到 deviceinfo: $deviceinfo'); |
|
|
|
|
|
if (deviceinfo != null) { |
|
|
|
|
|
// final code = processHex(deviceinfo["hex"]!, 15, 25); |
|
|
|
|
|
String code = extractAndReverseBytes(deviceinfo["hex"]!, 15, 24); |
|
|
|
|
|
print('获取到 code: $code'); |
|
|
|
|
|
// 安全检查长度 |
|
|
|
|
|
if (code != "") { |
|
|
|
|
|
final response = await Api.bindearphone({ |
|
|
|
|
|
"code": code, |
|
|
|
|
|
"earphonemac": device.address, |
|
|
|
|
|
}); |
|
|
|
|
|
Logger.i(_tag, '手动授权结果 ${response}'); |
|
|
|
|
|
if (response != null) { |
|
|
|
|
|
Logger.i(_tag, '设备自动绑定成功'); |
|
|
|
|
|
User.instance.earphonemac = device.address ?? ''; |
|
|
|
|
|
return true; |
|
|
|
|
|
} else { |
|
|
|
|
|
Logger.e(_tag, '手动绑定失败!'); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
// 调用BLE管理器的自动绑定方法 |
|
|
} else { |
|
|
} else { |
|
|
Logger.e(_tag, '手动绑定失败!'); |
|
|
|
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int reverseBits(int byte) { |
|
|
|
|
|
// 这里是 nibble swap 还是 bit 反转?看你需求,先保持 nibble swap |
|
|
|
|
|
return ((byte & 0x0F) << 4) | ((byte & 0xF0) >> 4); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String extractAndReverseBytes(String hex, int start, int end) { |
|
|
|
|
|
List<int> bytes = []; |
|
|
|
|
|
for (int i = 0; i < hex.length; i += 2) { |
|
|
|
|
|
bytes.add(int.parse(hex.substring(i, i + 2), radix: 16)); |
|
|
|
|
|
} |
|
|
|
|
|
List<int> slice = bytes.sublist(start, end + 1); |
|
|
|
|
|
List<int> reversed = slice.reversed.toList(); |
|
|
|
|
|
return reversed |
|
|
|
|
|
.map((b) => b.toRadixString(16).padLeft(2, '0')) |
|
|
|
|
|
.join() |
|
|
|
|
|
.toUpperCase(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// 用户点击授权码输入框 |
|
|
/// 用户点击授权码输入框 |
|
|
void onAutoCodeInputTap() async { |
|
|
void onAutoCodeInputTap() async { |
|
|
// 调用BLE管理器的自动绑定方法 |
|
|
// 调用BLE管理器的自动绑定方法 |
|
|
|