|
|
@ -25,6 +25,13 @@ import android.util.Log |
|
|
import androidx.annotation.NonNull |
|
|
import androidx.annotation.NonNull |
|
|
import com.jieli.otasdk.tool.config.ConfigHelper |
|
|
import com.jieli.otasdk.tool.config.ConfigHelper |
|
|
import com.yunqiinnovation.ble_service.BleService |
|
|
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( |
|
|
class CustomUpdateCallback( |
|
|
|
|
|
|
|
|
val device: BluetoothDevice? |
|
|
val device: BluetoothDevice? |
|
|
@ -71,14 +78,14 @@ import com.yunqiinnovation.ble_service.BleService |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** OtaPlugin */ |
|
|
/** OtaPlugin */ |
|
|
class OtaPlugin: FlutterPlugin, MethodCallHandler { |
|
|
class OtaPlugin : BleService.Callback, FlutterPlugin, MethodCallHandler { |
|
|
/// The MethodChannel that will the communication between Flutter and native Android |
|
|
/// 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 |
|
|
/// This local reference serves to register the plugin with the Flutter Engine and unregister it |
|
|
/// when the Flutter Engine is detached from the Activity |
|
|
/// when the Flutter Engine is detached from the Activity |
|
|
private lateinit var channel : MethodChannel |
|
|
private lateinit var channel : MethodChannel |
|
|
private lateinit var context: Context |
|
|
private lateinit var context: Context |
|
|
|
|
|
private lateinit var device1: BluetoothDevice |
|
|
private lateinit var bluetoothHelper : BluetoothHelper |
|
|
private lateinit var bluetoothHelper : BluetoothHelper |
|
|
val otaConnectionMLD = MutableLiveData<DeviceConnection>() |
|
|
val otaConnectionMLD = MutableLiveData<DeviceConnection>() |
|
|
val mandatoryUpgradeMLD = MutableLiveData<BluetoothDevice>() |
|
|
val mandatoryUpgradeMLD = MutableLiveData<BluetoothDevice>() |
|
|
@ -90,11 +97,29 @@ private lateinit var bluetoothHelper : BluetoothHelper |
|
|
if ((status == StateCode.CONNECTION_FAILED || status == StateCode.CONNECTION_DISCONNECT) && bluetoothHelper.getConnectedDevice() != null) { |
|
|
if ((status == StateCode.CONNECTION_FAILED || status == StateCode.CONNECTION_DISCONNECT) && bluetoothHelper.getConnectedDevice() != null) { |
|
|
bluetoothHelper.disconnectDevice(bluetoothHelper.getConnectedDevice()) |
|
|
bluetoothHelper.disconnectDevice(bluetoothHelper.getConnectedDevice()) |
|
|
} |
|
|
} |
|
|
|
|
|
// 更新连接状态的 LiveData,用于通知 UI 或其他逻辑 |
|
|
otaConnectionMLD.value = DeviceConnection(device, status) |
|
|
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?) { |
|
|
override fun onMandatoryUpgrade(device: BluetoothDevice?) { |
|
|
mandatoryUpgradeMLD.postValue(device!!) |
|
|
mandatoryUpgradeMLD.postValue(device!!) |
|
|
|
|
|
Log.d("bluetoothCallback", "onMandatoryUpgrade") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
|
|
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
|
|
@ -115,21 +140,26 @@ private lateinit var bluetoothHelper : BluetoothHelper |
|
|
bluetoothHelper = BluetoothHelper(context) |
|
|
bluetoothHelper = BluetoothHelper(context) |
|
|
// 如果需要额外的初始化逻辑,请在这里添加 |
|
|
// 如果需要额外的初始化逻辑,请在这里添加 |
|
|
otaManager.registerBluetoothCallback(bluetoothCallback) |
|
|
otaManager.registerBluetoothCallback(bluetoothCallback) |
|
|
|
|
|
BleService.addCallback(this) |
|
|
// otaManager.registerBluetoothCallback(bluetoothCallback) |
|
|
// otaManager.registerBluetoothCallback(bluetoothCallback) |
|
|
result.success("Android ${android.os.Build.VERSION.RELEASE}") |
|
|
result.success("Android ${android.os.Build.VERSION.RELEASE}") |
|
|
}else if (call.method == "searchDevices") { |
|
|
}else if (call.method == "searchDevices") { |
|
|
Log.d("TAG", "searchDevices") |
|
|
Log.d("TAG", "searchDevices") |
|
|
// otaManager.configHelper |
|
|
BleService.isota=true |
|
|
BleService.disconnect() |
|
|
device1 = BleService.bluetoothGatt?.device!! |
|
|
// 确保先初始化 ConfigHelper |
|
|
BleService.disconnect() |
|
|
otaManager.bluetoothHelper.startScan(BleService.bluetoothGatt?.device) |
|
|
|
|
|
|
|
|
|
|
|
// otaManager.registerBluetoothCallback(bluetoothCallback) |
|
|
|
|
|
result.success("Android ${android.os.Build.VERSION.RELEASE}") |
|
|
|
|
|
} else if (call.method == "startUpgrade") { |
|
|
} else if (call.method == "startUpgrade") { |
|
|
Log.d("TAG", "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.bluetoothOption.firmwareFilePath = filePath |
|
|
otaManager.startOTA( |
|
|
otaManager.startOTA( |
|
|
CustomUpdateCallback( |
|
|
CustomUpdateCallback( |
|
|
@ -145,4 +175,45 @@ private lateinit var bluetoothHelper : BluetoothHelper |
|
|
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
|
|
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
|
|
channel.setMethodCallHandler(null) |
|
|
channel.setMethodCallHandler(null) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onScanResult(devices: List<Map<String, Any>>) { |
|
|
|
|
|
// 不处理扫描结果 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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<String, Any>) { |
|
|
|
|
|
// 不处理设备信息 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|