Browse Source

上传ble蓝牙连接

newdev_shunjiawei
liwei1dao 1 year ago
parent
commit
e0a2efb424
  1. 9
      lib/data/services/ble_manager.dart
  2. 40
      lib/modules/pairing/controllers/pairing_controller.dart
  3. 11
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift
  4. 5
      local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift

9
lib/data/services/ble_manager.dart

@ -782,6 +782,15 @@ class BleManager extends GetxService {
} }
} }
/// 验证设备是否可以配对
Future<void> stopScan() async {
try {
await _bleService.stopScan();
} catch (e) {
Logger.error('停止扫描设备: ${e.toString()}');
}
}
Future<Map<String, String>?> getDeviceInfo(String deviceName) async { Future<Map<String, String>?> getDeviceInfo(String deviceName) async {
try { try {
final result = _bleService.getDeviceInfo(deviceName); final result = _bleService.getDeviceInfo(deviceName);

40
lib/modules/pairing/controllers/pairing_controller.dart

@ -487,7 +487,7 @@ class PairingController extends GetxController {
//连接ble //连接ble
Future<void> connectble(BluetoothDevice device) async { Future<void> connectble(BluetoothDevice device) async {
Logger.i(_tag, '设备验证通过'); Logger.i(_tag, '设备验证通过');
_verifiedDeviceAddress = device.address; // _verifiedDeviceAddress = device.address;
// 检查是否已有BLE关联 // 检查是否已有BLE关联
final pairedMacAddress = await _bleManager.getPairedMacAddress(); final pairedMacAddress = await _bleManager.getPairedMacAddress();
Logger.i(_tag, '已关联的MAC地址: $pairedMacAddress'); Logger.i(_tag, '已关联的MAC地址: $pairedMacAddress');
@ -588,8 +588,8 @@ class PairingController extends GetxController {
//自动绑定设备 0 未合作商 1 合作商 2,已绑定 //自动绑定设备 0 未合作商 1 合作商 2,已绑定
Future<int> authbindingDevice(BluetoothDevice device) async { Future<int> authbindingDevice(BluetoothDevice device) async {
// 检查是否已经绑定 if (User.instance.earphoneauthcode != "" &&
if (User.instance.earphonemac == device.address) { User.instance.earphonemac == device.address) {
// 已经绑定 // 已经绑定
return 9; return 9;
} }
@ -625,17 +625,39 @@ class PairingController extends GetxController {
code = extractAndReverseBytes(hexData, 13, 22); code = extractAndReverseBytes(hexData, 13, 22);
print('找到匹配设备,获取到 code: $code'); print('找到匹配设备,获取到 code: $code');
if (code != "") { if (code != "") {
_bleManager.stopScan();
deviceCode.value = code; deviceCode.value = code;
return 1; String? uuids = deviceInfo["id"];
_verifiedDeviceAddress = uuids;
print('找到匹配设备,获取到 uuids: $uuids');
// 检查是否已经绑定
if (User.instance.earphonelicense != "" &&
User.instance.earphonemac == device.address) {
// 已经绑定
return 10;
} else {
return 1;
}
} }
} else if (Platform.isAndroid) { } else if (Platform.isAndroid) {
// Android设备,提取15-24字节的授权码 // Android设备,提取15-24字节的授权码
code = extractAndReverseBytes(hexData, 15, 24); code = extractAndReverseBytes(hexData, 15, 24);
} print('找到匹配设备,获取到 code: $code');
print('找到匹配设备,获取到 code: $code'); if (code != "") {
if (code != "") { _bleManager.stopScan();
deviceCode.value = code; deviceCode.value = code;
return 1; _verifiedDeviceAddress = device.address;
// 检查是否已经绑定
if (User.instance.earphonelicense != "" &&
User.instance.earphonemac == device.address) {
// 已经绑定
return 10;
} else {
return 1;
}
}
} else {
break;
} }
} }
} }

11
local_plugins/ble_service/ios/ble_service/Sources/ble_service/BleService.swift

@ -175,6 +175,14 @@ public class BleService: NSObject {
os_log("未找到已配对设备UUID", log: logger, type: .info) os_log("未找到已配对设备UUID", log: logger, type: .info)
return nil return nil
} }
/// 保存配对设备的UUID
/// - Parameter uuid: 要保存的设备UUID
func savePairedMacAddress(_ uuid: UUID) {
let uuidString = uuid.uuidString
UserDefaults.standard.set(uuidString, forKey: pairedDeviceUUIDKey)
UserDefaults.standard.synchronize()
os_log("已保存配对设备UUID: %{public}@", log: logger, type: .info, uuidString)
}
/// 清除已保存的配对设备UUID /// 清除已保存的配对设备UUID
private func clearPairedMacAddress() { private func clearPairedMacAddress() {
UserDefaults.standard.removeObject(forKey: pairedDeviceUUIDKey) UserDefaults.standard.removeObject(forKey: pairedDeviceUUIDKey)
@ -245,11 +253,12 @@ public class BleService: NSObject {
os_log("❌ CBCentralManager未初始化", log: logger, type: .error) os_log("❌ CBCentralManager未初始化", log: logger, type: .error)
return false return false
} }
os_log("连接设备UUID: %@", log: logger, type: .error, uuid)
// 先尝试从已知外设中获取 // 先尝试从已知外设中获取
let knownPeripherals = manager.retrievePeripherals(withIdentifiers: [deviceUUID]) let knownPeripherals = manager.retrievePeripherals(withIdentifiers: [deviceUUID])
if let peripheral = knownPeripherals.first { if let peripheral = knownPeripherals.first {
connectPeripheral(peripheral) connectPeripheral(peripheral)
savePairedMacAddress(deviceUUID)
return true return true
} }

5
local_plugins/ble_service/ios/ble_service/Sources/ble_service/SwiftBleServicePlugin.swift

@ -145,8 +145,9 @@ public class SwiftBleServicePlugin: NSObject, FlutterPlugin {
let arguments = call.arguments as? [String: Any] let arguments = call.arguments as? [String: Any]
let deviceNamePattern = (arguments?["deviceNamePattern"] as? String) ?? "" let deviceNamePattern = (arguments?["deviceNamePattern"] as? String) ?? ""
let serviceUuid = (arguments?["serviceUuid"] as? String) ?? "" let serviceUuid = (arguments?["serviceUuid"] as? String) ?? ""
let macAddress = (arguments?["macAddress"] as? String) ?? "" let uuids = (arguments?["macAddress"] as? String) ?? ""
let success = BleService.shared.connectToDevice(uuid: uuids)
result(success)
case "connectToDevice": case "connectToDevice":
let arguments = call.arguments as? [String: Any] let arguments = call.arguments as? [String: Any]

Loading…
Cancel
Save