From 20fe5366e5edc0ae6fe0ddfc82c081ccf672f2fc Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Wed, 4 Jun 2025 20:48:19 +0800 Subject: [PATCH] 1 --- .../controllers/ota_upgrade_controller.dart | 2 +- .../yunqiinnovation/ble_service/BleService.kt | 8 +- .../main/kotlin/com/example/ota/OtaPlugin.kt | 93 ++++++++++++++++--- .../com/example/ota/tool/ota/OTAManager.kt | 26 +++++- .../example/ota/tool/ota/ble/BleManager.java | 8 +- 5 files changed, 115 insertions(+), 22 deletions(-) diff --git a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart index 778dc1ddf..fa623f5fd 100644 --- a/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart +++ b/lib/modules/ota_upgrade/controllers/ota_upgrade_controller.dart @@ -294,7 +294,7 @@ class OtaUpgradeController extends GetxController { // 开始升级 void _installApp(String path) async { jlota.searchDevices(); - jlota.startUpgrade(); + // jlota.startUpgrade(); // JlOtaPlugin() // final File file = File(path); // if (await file.exists()) { diff --git a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt index b3810ea94..2ab2d1e30 100644 --- a/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt +++ b/local_plugins/ble_service/android/src/main/kotlin/com/yunqiinnovation/ble_service/BleService.kt @@ -89,7 +89,7 @@ object BleService { private lateinit var scanHandler: Handler private val scanResults = ArrayList() private var isScanning = false - + var isota = false // 连接状态 val connectionState = MutableLiveData(BleConst.STATE_DISCONNECTED) @@ -355,9 +355,12 @@ object BleService { } Log.i(TAG, "检测到已配对设备,MAC: $pairedMac,尝试连接") - + if(!isota) + { // 调用现有的 connect 方法 connect(pairedMac) + } + } catch (e: Exception) { Log.e(TAG, "自动连接已配对设备失败: ${e.message}", e) updateConnectionState(BleConst.STATE_ERROR) @@ -427,7 +430,6 @@ object BleService { audioChar = null } } - // fun startOTA() { // try { // otaManager?.startOTA(CustomUpdateCallback(context, null)) diff --git a/local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt b/local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt index cabac136a..61f954136 100644 --- a/local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt +++ b/local_plugins/ota/android/src/main/kotlin/com/example/ota/OtaPlugin.kt @@ -25,6 +25,13 @@ import android.util.Log import androidx.annotation.NonNull import com.jieli.otasdk.tool.config.ConfigHelper import com.yunqiinnovation.ble_service.BleService +import com.yunqiinnovation.ble_service.BleConst +import android.os.Handler +import android.os.Looper + +import java.io.File +import java.io.FileOutputStream +import java.io.RandomAccessFile class CustomUpdateCallback( val device: BluetoothDevice? @@ -71,14 +78,14 @@ import com.yunqiinnovation.ble_service.BleService } /** OtaPlugin */ -class OtaPlugin: FlutterPlugin, MethodCallHandler { +class OtaPlugin : BleService.Callback, FlutterPlugin, MethodCallHandler { /// The MethodChannel that will the communication between Flutter and native Android /// /// This local reference serves to register the plugin with the Flutter Engine and unregister it /// when the Flutter Engine is detached from the Activity private lateinit var channel : MethodChannel private lateinit var context: Context - + private lateinit var device1: BluetoothDevice private lateinit var bluetoothHelper : BluetoothHelper val otaConnectionMLD = MutableLiveData() val mandatoryUpgradeMLD = MutableLiveData() @@ -90,11 +97,29 @@ private lateinit var bluetoothHelper : BluetoothHelper if ((status == StateCode.CONNECTION_FAILED || status == StateCode.CONNECTION_DISCONNECT) && bluetoothHelper.getConnectedDevice() != null) { bluetoothHelper.disconnectDevice(bluetoothHelper.getConnectedDevice()) } + // 更新连接状态的 LiveData,用于通知 UI 或其他逻辑 otaConnectionMLD.value = DeviceConnection(device, status) + Log.d("bluetoothCallback", "onConnection,status${status}") + if( status == StateCode.CONNECTION_OK) { + Log.d("TAG", "startUpgrade") + val directoryName = "ota_upgrades" + val fileName = "1.ufw" + // 创建WAV输出文件路径 + val filePath = context.getExternalFilesDir(null)?.absolutePath + "/downloads/ota_upgrades/$fileName" + + Log.d("TAG", "startUpgrade,filePath=${filePath}") + otaManager.bluetoothOption.firmwareFilePath = filePath + otaManager.startOTA( + CustomUpdateCallback( + otaManager.bluetoothHelper.getConnectedDevice() + ) + ) + } } override fun onMandatoryUpgrade(device: BluetoothDevice?) { mandatoryUpgradeMLD.postValue(device!!) + Log.d("bluetoothCallback", "onMandatoryUpgrade") } } override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { @@ -115,21 +140,26 @@ private lateinit var bluetoothHelper : BluetoothHelper bluetoothHelper = BluetoothHelper(context) // 如果需要额外的初始化逻辑,请在这里添加 otaManager.registerBluetoothCallback(bluetoothCallback) - + BleService.addCallback(this) // otaManager.registerBluetoothCallback(bluetoothCallback) result.success("Android ${android.os.Build.VERSION.RELEASE}") }else if (call.method == "searchDevices") { Log.d("TAG", "searchDevices") - // otaManager.configHelper - BleService.disconnect() - // 确保先初始化 ConfigHelper - otaManager.bluetoothHelper.startScan(BleService.bluetoothGatt?.device) - - // otaManager.registerBluetoothCallback(bluetoothCallback) - result.success("Android ${android.os.Build.VERSION.RELEASE}") + BleService.isota=true + device1 = BleService.bluetoothGatt?.device!! + BleService.disconnect() + + } else if (call.method == "startUpgrade") { Log.d("TAG", "startUpgrade") - var filePath = "/storage/emulated/0/Download/JieLiOTA/upgrade/update - v0.1.0_添加杰理OTA,优化主从切换拾音丢失问题.ufw" + val directoryName = "ota_upgrades" + val fileName = "1.ufw" + // 创建WAV输出文件路径 + val filePath = context.getExternalFilesDir(null)?.absolutePath + "/downloads/ota_upgrades/$fileName" + + Log.d("TAG", "startUpgrade,filePath=${filePath}") + +//var filePath = "/storage/emulated/0/Download/JieLiOTA/upgrade/update - v0.1.0_添加杰理OTA,优化主从切换拾音丢失问题.ufw" otaManager.bluetoothOption.firmwareFilePath = filePath otaManager.startOTA( CustomUpdateCallback( @@ -145,4 +175,45 @@ private lateinit var bluetoothHelper : BluetoothHelper override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { channel.setMethodCallHandler(null) } + + + + override fun onScanResult(devices: List>) { + // 不处理扫描结果 + } + + override fun onConnectionStateChanged(state: Int) { + // // 可选:处理连接状态变化 + // if (state == BleConst.STATE_CONNECTED) { + // Log.d(TAG, "Ble设备已连接,准备好语音交互") + // } else if (state == BleConst.STATE_DISCONNECTED) { + Log.d("TAG", "Ble设备已断开,onConnectionStateChanged") + // // 设备断开连接,停止语音服务 + // // stopListening() + // } + if (state == BleConst.STATE_DISCONNECTED) { + Handler(Looper.getMainLooper()).postDelayed({ + otaManager.bluetoothHelper.startScan(device1) + }, 2000) // 2000 毫秒 = 2 秒 + } + } + + override fun onAudioDataReceived(data: ByteArray) { + + // 可选:处理音频数据 + } + + /** + * 处理唤醒信号 + * 在收到唤醒信号时启动语音识别 + */ + override fun onWakeupSignalReceived() { + + } + + override fun onDeviceInfoReceived(infoType: Int, infoData: Map) { + // 不处理设备信息 + } + + } diff --git a/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/OTAManager.kt b/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/OTAManager.kt index 10d86a089..fdfa317bc 100644 --- a/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/OTAManager.kt +++ b/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/OTAManager.kt @@ -23,12 +23,30 @@ class OTAManager(context: Context) : BluetoothOTAManager(context) { val configHelper = ConfigHelper.getInstance() val bluetoothHelper = BluetoothHelper.getInstance(context) - + /** + * 转换成OTA库的连接状态 + * + * @param status 连接状态 + * @return 库的连接状态 + */ + + fun changeConnectStatus(status: Int): Int { + var changeStatus = StateCode.CONNECTION_DISCONNECT + when (status) { + 2 -> { + changeStatus = StateCode.CONNECTION_OK + } + 1 -> { + changeStatus = StateCode.CONNECTION_CONNECTING + } + } + return changeStatus + } val btEventCallback = object : OnBTEventCallback() { override fun onDeviceConnection(device: BluetoothDevice?, way: Int, status: Int) { super.onDeviceConnection(device, way, status) - // val connectionState = AppUtil.changeConnectStatus(status) + val connectionState = changeConnectStatus(status) // Log.i( // TAG, "onConnection >>> device : ${printDeviceInfo(device)}, way = $way" + // " status :$status, change status : $connectionState" @@ -37,9 +55,9 @@ class OTAManager(context: Context) : BluetoothOTAManager(context) { //注释掉 Log.i( TAG, "onConnection >>> device : ${device}, way = $way" + - " status :$status, change status : 3" + " status :$status, change status : $connectionState" ) - onBtDeviceConnection(device, 1) + onBtDeviceConnection(device, connectionState) } override fun onReceiveData( diff --git a/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/ble/BleManager.java b/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/ble/BleManager.java index feec079de..04f53d8e9 100644 --- a/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/ble/BleManager.java +++ b/local_plugins/ota/android/src/main/kotlin/com/example/ota/tool/ota/ble/BleManager.java @@ -88,7 +88,7 @@ public class BleManager { private volatile boolean isBleScanning; private NotifyCharacteristicRunnable mNotifyCharacteristicRunnable; - private BluetoothDevice deviced;//需要连接手机设备信息 + private BluetoothDevice deviced1;//需要连接手机设备信息 private long timeout = 15000;//扫描设备时间 private int mRetryConnectCount = 0;//连接失败后尝试连接次数 private long startConnectTime = 0; //开始连接时间戳 @@ -311,7 +311,8 @@ public class BleManager { } @SuppressLint("MissingPermission") public boolean startLeScan(BluetoothDevice deviced) { - Log.i(TAG, ", timeout = " + timeout); + deviced1=deviced; + Log.i(TAG, ", deviced = " + deviced); if (!isBluetoothEnable() || !isLocationPermissionGranted(mContext)) { checkPermissionsAndRequest(mContext); return false; @@ -632,7 +633,8 @@ public class BleManager { && !mDiscoveredBleDevices.contains(device)) { Log.d(TAG, "notify device : " + printDeviceInfo(device)); Log.d(TAG, "device.getAddress : " + device.getAddress()); - if ("4D:9B:09:38:22:F5".equals(device.getAddress())) { + Log.d(TAG, "deviced1.getAddress() : " + deviced1.getAddress()); + if (deviced1 != null && deviced1.getAddress().equals(device.getAddress())) { // 匹配成功 Log.i(TAG, "filterDevice找到了DeapSound设备");