|
|
@ -10,8 +10,12 @@ import android.util.Log |
|
|
import androidx.lifecycle.LifecycleService |
|
|
import androidx.lifecycle.LifecycleService |
|
|
import androidx.lifecycle.MutableLiveData |
|
|
import androidx.lifecycle.MutableLiveData |
|
|
import kotlinx.coroutines.* |
|
|
import kotlinx.coroutines.* |
|
|
|
|
|
import java.io.File |
|
|
|
|
|
import java.io.FileOutputStream |
|
|
|
|
|
import java.text.SimpleDateFormat |
|
|
import java.util.* |
|
|
import java.util.* |
|
|
|
|
|
import com.yunqiinnovation.azure_speech.AzureAsrHelper |
|
|
|
|
|
import com.yunqiinnovation.azure_speech.AzureTtsHelper |
|
|
/** |
|
|
/** |
|
|
* BLE服务类,提供核心蓝牙功能 |
|
|
* BLE服务类,提供核心蓝牙功能 |
|
|
*/ |
|
|
*/ |
|
|
@ -21,10 +25,14 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 服务和特征UUID - 根据协议规范重新定义 |
|
|
// 服务和特征UUID - 根据协议规范重新定义 |
|
|
// private const val AUDIO_SERVICE_UUID = "0000faa0-0000-1000-8000-00805f9b34fb" // 主服务UUID |
|
|
// private const val AUDIO_SERVICE_UUID = "0000faa0-0000-1000-8000-00805f9b34fb" // 主服务UUID |
|
|
private const val AUDIO_SERVICE_UUID = "0000fd7b-0000-1000-8000-00805f9b34fb" // 主服务UUID |
|
|
private const val AUDIO_SERVICE_UUID = "0000abc0-0000-1111-2222-123456789abc" // 主服务UUID |
|
|
private const val NOTIFY_CHAR_UUID = "0000faa2-0000-1000-8000-00805f9b34fb" // 用于接收数据的特征(notify) |
|
|
private const val NOTIFY_CHAR_UUID = |
|
|
private const val WRITE_CHAR_UUID = "0000faa1-0000-1000-8000-00805f9b34fb" // 用于发送命令的特征(write without response) |
|
|
"0000abc2-0000-1111-2222-123456789abc" // 用于接收数据的特征(notify) |
|
|
private const val CLIENT_CHAR_CONFIG_UUID = "00002902-0000-1000-8000-00805f9b34fb" // 标准的客户端特征配置描述符 |
|
|
private const val WRITE_CHAR_UUID = |
|
|
|
|
|
"0000abc1-0000-1111-2222-123456789abc" // 用于发送命令的特征(write without response) |
|
|
|
|
|
private const val CLIENT_CHAR_CONFIG_UUID = |
|
|
|
|
|
"00002902-0000-1000-8000-00805f9b34fb" // 标准的客户端特征配置描述符 |
|
|
|
|
|
|
|
|
// 连接状态常量 |
|
|
// 连接状态常量 |
|
|
const val STATE_DISCONNECTED = 0 |
|
|
const val STATE_DISCONNECTED = 0 |
|
|
const val STATE_CONNECTING = 1 |
|
|
const val STATE_CONNECTING = 1 |
|
|
@ -50,8 +58,16 @@ class BleService(private val context: Context) { |
|
|
fun onCommandResult(commandType: Int, success: Boolean) |
|
|
fun onCommandResult(commandType: Int, success: Boolean) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// region 文件相关 |
|
|
|
|
|
private var outputFile: File? = null |
|
|
|
|
|
private var fos: FileOutputStream? = null |
|
|
|
|
|
|
|
|
|
|
|
// Azure服务 |
|
|
|
|
|
private var azureAsrHelper: AzureAsrHelper? = null |
|
|
|
|
|
private var azureTtsHelper: AzureTtsHelper? = null |
|
|
// 蓝牙管理器和适配器 |
|
|
// 蓝牙管理器和适配器 |
|
|
private val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager |
|
|
private val bluetoothManager = |
|
|
|
|
|
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager |
|
|
private val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter |
|
|
private val bluetoothAdapter: BluetoothAdapter? = bluetoothManager.adapter |
|
|
private val bluetoothLeScanner: BluetoothLeScanner? = bluetoothAdapter?.bluetoothLeScanner |
|
|
private val bluetoothLeScanner: BluetoothLeScanner? = bluetoothAdapter?.bluetoothLeScanner |
|
|
|
|
|
|
|
|
@ -102,7 +118,10 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 设置超时停止扫描 |
|
|
// 设置超时停止扫描 |
|
|
scanHandler.postDelayed({ |
|
|
scanHandler.postDelayed({ |
|
|
Log.d(TAG, "定时器触发,准备停止扫描 [主线程:${Looper.myLooper() == Looper.getMainLooper()}]") |
|
|
Log.d( |
|
|
|
|
|
TAG, |
|
|
|
|
|
"定时器触发,准备停止扫描 [主线程:${Looper.myLooper() == Looper.getMainLooper()}]" |
|
|
|
|
|
) |
|
|
stopScan() |
|
|
stopScan() |
|
|
}, scanTimeout) |
|
|
}, scanTimeout) |
|
|
|
|
|
|
|
|
@ -142,7 +161,7 @@ class BleService(private val context: Context) { |
|
|
// if (!deviceName.contains("sanag") && !deviceName.contains("Ola Friend")) { |
|
|
// if (!deviceName.contains("sanag") && !deviceName.contains("Ola Friend")) { |
|
|
// return |
|
|
// return |
|
|
// } |
|
|
// } |
|
|
Log.d(TAG, "============发现设备: $deviceName [$deviceAddress], RSSI: $rssi") |
|
|
//Log.d(TAG, "============发现设备: $deviceName [$deviceAddress], RSSI: $rssi") |
|
|
|
|
|
|
|
|
// 打印所有发现的UUID以便调试 |
|
|
// 打印所有发现的UUID以便调试 |
|
|
val serviceUuids = scanRecord?.serviceUuids |
|
|
val serviceUuids = scanRecord?.serviceUuids |
|
|
@ -156,16 +175,20 @@ class BleService(private val context: Context) { |
|
|
for (i in 0 until 65535) { |
|
|
for (i in 0 until 65535) { |
|
|
val manufacturerData = scanRecord?.getManufacturerSpecificData(i) |
|
|
val manufacturerData = scanRecord?.getManufacturerSpecificData(i) |
|
|
if (manufacturerData != null) { |
|
|
if (manufacturerData != null) { |
|
|
Log.d(TAG, "发现厂商ID: $i, 数据长度: ${manufacturerData.size}") |
|
|
// Log.d(TAG, "发现厂商ID: $i, 数据长度: ${manufacturerData.size}") |
|
|
// 这里可以根据已知的厂商ID和格式解析数据 |
|
|
// 这里可以根据已知的厂商ID和格式解析数据 |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 避免重复添加 |
|
|
// 避免重复添加 |
|
|
|
|
|
|
|
|
val existingIndex = scanResults.indexOfFirst { it.device.address == deviceAddress } |
|
|
val existingIndex = scanResults.indexOfFirst { it.device.address == deviceAddress } |
|
|
|
|
|
|
|
|
if (existingIndex >= 0) { |
|
|
if (existingIndex >= 0) { |
|
|
|
|
|
|
|
|
scanResults[existingIndex] = result |
|
|
scanResults[existingIndex] = result |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
Log.d(TAG, "扫描回调A, scanResults[existingIndex]ADD") |
|
|
scanResults.add(result) |
|
|
scanResults.add(result) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -217,7 +240,8 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 连接GATT服务器 |
|
|
// 连接GATT服务器 |
|
|
Log.i(TAG, "正在连接设备: ${device.name ?: "未知设备"} [$deviceAddress]") |
|
|
Log.i(TAG, "正在连接设备: ${device.name ?: "未知设备"} [$deviceAddress]") |
|
|
bluetoothGatt = device.connectGatt(context, false, gattCallback) |
|
|
bluetoothGatt = |
|
|
|
|
|
device.connectGatt(context, false, gattCallback, BluetoothDevice.TRANSPORT_LE) |
|
|
} catch (e: Exception) { |
|
|
} catch (e: Exception) { |
|
|
Log.e(TAG, "连接设备时发生错误: ${e.message}") |
|
|
Log.e(TAG, "连接设备时发生错误: ${e.message}") |
|
|
updateConnectionState(STATE_ERROR) |
|
|
updateConnectionState(STATE_ERROR) |
|
|
@ -244,6 +268,8 @@ class BleService(private val context: Context) { |
|
|
* GATT回调 |
|
|
* GATT回调 |
|
|
*/ |
|
|
*/ |
|
|
private val gattCallback = object : BluetoothGattCallback() { |
|
|
private val gattCallback = object : BluetoothGattCallback() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { |
|
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { |
|
|
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { |
|
|
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) { |
|
|
Log.i(TAG, "设备已连接,开始发现服务...") |
|
|
Log.i(TAG, "设备已连接,开始发现服务...") |
|
|
@ -276,12 +302,15 @@ class BleService(private val context: Context) { |
|
|
// 查找目标服务 |
|
|
// 查找目标服务 |
|
|
val audioService = gatt.getService(UUID.fromString(AUDIO_SERVICE_UUID)) |
|
|
val audioService = gatt.getService(UUID.fromString(AUDIO_SERVICE_UUID)) |
|
|
if (audioService != null) { |
|
|
if (audioService != null) { |
|
|
Log.i(TAG, "找到音频服务") |
|
|
Log.i(TAG, "找到音频服务,开始遍历特征...") |
|
|
|
|
|
audioService.characteristics.forEach { char -> |
|
|
|
|
|
Log.i(TAG, "特征UUID: ${char.uuid}") |
|
|
|
|
|
} |
|
|
|
|
|
Log.i(TAG, "找到音频服务$") |
|
|
|
|
|
|
|
|
// 获取特征 |
|
|
// 获取特征 |
|
|
notifyChar = audioService.getCharacteristic(UUID.fromString(NOTIFY_CHAR_UUID)) |
|
|
notifyChar = audioService.getCharacteristic(UUID.fromString(NOTIFY_CHAR_UUID)) |
|
|
writeChar = audioService.getCharacteristic(UUID.fromString(WRITE_CHAR_UUID)) |
|
|
writeChar = audioService.getCharacteristic(UUID.fromString(WRITE_CHAR_UUID)) |
|
|
|
|
|
|
|
|
if (notifyChar != null && writeChar != null) { |
|
|
if (notifyChar != null && writeChar != null) { |
|
|
Log.i(TAG, "找到所需特征") |
|
|
Log.i(TAG, "找到所需特征") |
|
|
|
|
|
|
|
|
@ -290,6 +319,7 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 注册特征通知 |
|
|
// 注册特征通知 |
|
|
setupNotifications(gatt) |
|
|
setupNotifications(gatt) |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
Log.e(TAG, "未找到所需特征") |
|
|
Log.e(TAG, "未找到所需特征") |
|
|
updateConnectionState(STATE_ERROR) |
|
|
updateConnectionState(STATE_ERROR) |
|
|
@ -303,8 +333,15 @@ class BleService(private val context: Context) { |
|
|
updateConnectionState(STATE_ERROR) |
|
|
updateConnectionState(STATE_ERROR) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
// 添加扩展函数(在文件顶部声明) |
|
|
|
|
|
fun List<Byte>.toByteArray(): ByteArray { |
|
|
|
|
|
return ByteArray(this.size) { this[it] } |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { |
|
|
override fun onCharacteristicChanged( |
|
|
|
|
|
gatt: BluetoothGatt, |
|
|
|
|
|
characteristic: BluetoothGattCharacteristic |
|
|
|
|
|
) { |
|
|
val characteristicUuid = characteristic.uuid.toString() |
|
|
val characteristicUuid = characteristic.uuid.toString() |
|
|
Log.d(TAG, "收到特征变化通知: $characteristicUuid") |
|
|
Log.d(TAG, "收到特征变化通知: $characteristicUuid") |
|
|
|
|
|
|
|
|
@ -321,6 +358,16 @@ class BleService(private val context: Context) { |
|
|
val value = characteristic.value |
|
|
val value = characteristic.value |
|
|
Log.i(TAG, "收到控制通知: ${value.contentToString()}") |
|
|
Log.i(TAG, "收到控制通知: ${value.contentToString()}") |
|
|
|
|
|
|
|
|
|
|
|
// 判断是否收到音频数据 |
|
|
|
|
|
if (value.isNotEmpty() && (value[0].toInt() and 0xFF == 0x4b || value[0].toInt() and 0xFF == 0x48)) { |
|
|
|
|
|
// 处理设备返回的协议数据 |
|
|
|
|
|
fos?.write(value) |
|
|
|
|
|
// 方法1:手动转换 |
|
|
|
|
|
val byteArray = ByteArray(value.size) { index -> value[index] } |
|
|
|
|
|
|
|
|
|
|
|
// 或者直接使用现有 |
|
|
|
|
|
azureAsrHelper?.pushAudioData(byteArray) |
|
|
|
|
|
} |
|
|
// 判断是否是协议格式响应 |
|
|
// 判断是否是协议格式响应 |
|
|
if (value.isNotEmpty() && value[0].toInt() and 0xFF == 0xA5) { |
|
|
if (value.isNotEmpty() && value[0].toInt() and 0xFF == 0xA5) { |
|
|
// 处理设备返回的协议数据 |
|
|
// 处理设备返回的协议数据 |
|
|
@ -334,12 +381,18 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 收到唤醒后自动发送开始录音命令 |
|
|
// 收到唤醒后自动发送开始录音命令 |
|
|
sendStartRecordingCommand() |
|
|
sendStartRecordingCommand() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { |
|
|
override fun onCharacteristicWrite( |
|
|
|
|
|
gatt: BluetoothGatt, |
|
|
|
|
|
characteristic: BluetoothGattCharacteristic, |
|
|
|
|
|
status: Int |
|
|
|
|
|
) { |
|
|
if (characteristic.uuid.toString() == WRITE_CHAR_UUID) { |
|
|
if (characteristic.uuid.toString() == WRITE_CHAR_UUID) { |
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
Log.i(TAG, "命令发送成功: ${characteristic.value.contentToString()}") |
|
|
Log.i(TAG, "命令发送成功: ${characteristic.value.contentToString()}") |
|
|
@ -349,7 +402,11 @@ class BleService(private val context: Context) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) { |
|
|
override fun onDescriptorWrite( |
|
|
|
|
|
gatt: BluetoothGatt, |
|
|
|
|
|
descriptor: BluetoothGattDescriptor, |
|
|
|
|
|
status: Int |
|
|
|
|
|
) { |
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
if (status == BluetoothGatt.GATT_SUCCESS) { |
|
|
Log.i(TAG, "描述符写入成功: ${descriptor.uuid}") |
|
|
Log.i(TAG, "描述符写入成功: ${descriptor.uuid}") |
|
|
Log.i(TAG, "特征通知设置成功,设备就绪") |
|
|
Log.i(TAG, "特征通知设置成功,设备就绪") |
|
|
@ -380,6 +437,7 @@ class BleService(private val context: Context) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 处理设备返回的数据 |
|
|
* 处理设备返回的数据 |
|
|
* 协议格式: |
|
|
* 协议格式: |
|
|
@ -448,6 +506,23 @@ class BleService(private val context: Context) { |
|
|
try { |
|
|
try { |
|
|
val commandData = byteArrayOf(0x01) |
|
|
val commandData = byteArrayOf(0x01) |
|
|
writeChar?.value = commandData |
|
|
writeChar?.value = commandData |
|
|
|
|
|
// 获取外部存储目录路径 |
|
|
|
|
|
// 生成带时间戳的文件名(格式:yyyyMMdd_HHmmss_audio.opus) |
|
|
|
|
|
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(Date()) |
|
|
|
|
|
val fileName = "${timeStamp}_audio.opus" |
|
|
|
|
|
val externalDir = context.getExternalFilesDir(null) |
|
|
|
|
|
outputFile = File(externalDir, fileName) |
|
|
|
|
|
fos = FileOutputStream(outputFile).also { |
|
|
|
|
|
Log.i(TAG, "音频文件创建成功:${outputFile?.absolutePath}") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// val subscriptionKey = call.argument<String>("subscriptionKey") ?: "" |
|
|
|
|
|
// val region = call.argument<String>("region") ?: "" |
|
|
|
|
|
// val supportedLanguages = call.argument<List<String>>("supportedLanguages") ?: listOf("zh-CN") |
|
|
|
|
|
|
|
|
|
|
|
// val success = azureAsrHelper.initialize(subscriptionKey, region, supportedLanguages.toTypedArray()) |
|
|
|
|
|
// result.success(success) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val success = bluetoothGatt?.writeCharacteristic(writeChar) ?: false |
|
|
val success = bluetoothGatt?.writeCharacteristic(writeChar) ?: false |
|
|
|
|
|
|
|
|
@ -478,7 +553,7 @@ class BleService(private val context: Context) { |
|
|
writeChar?.value = commandData |
|
|
writeChar?.value = commandData |
|
|
|
|
|
|
|
|
val success = bluetoothGatt?.writeCharacteristic(writeChar) ?: false |
|
|
val success = bluetoothGatt?.writeCharacteristic(writeChar) ?: false |
|
|
|
|
|
fos?.close() |
|
|
if (success) { |
|
|
if (success) { |
|
|
Log.i(TAG, "停止录音命令已发送") |
|
|
Log.i(TAG, "停止录音命令已发送") |
|
|
} else { |
|
|
} else { |
|
|
@ -498,6 +573,10 @@ class BleService(private val context: Context) { |
|
|
* 协议头(0xAA) + 字段类型(0x01) + 数据长度(0x01) + 音量值(0~16) + 校验和 |
|
|
* 协议头(0xAA) + 字段类型(0x01) + 数据长度(0x01) + 音量值(0~16) + 校验和 |
|
|
*/ |
|
|
*/ |
|
|
fun adjustVolume(volume: Int): Boolean { |
|
|
fun adjustVolume(volume: Int): Boolean { |
|
|
|
|
|
Log.e( |
|
|
|
|
|
TAG, |
|
|
|
|
|
"adjustVolume,bluetoothGatt=${bluetoothGatt},writeChar=${writeChar},connectionState=${connectionState.value}" |
|
|
|
|
|
) |
|
|
if (bluetoothGatt == null || writeChar == null || connectionState.value != STATE_CONNECTED) { |
|
|
if (bluetoothGatt == null || writeChar == null || connectionState.value != STATE_CONNECTED) { |
|
|
Log.e(TAG, "无法发送音量调整命令,设备未连接") |
|
|
Log.e(TAG, "无法发送音量调整命令,设备未连接") |
|
|
return false |
|
|
return false |
|
|
@ -515,9 +594,9 @@ class BleService(private val context: Context) { |
|
|
|
|
|
|
|
|
// 计算校验和 |
|
|
// 计算校验和 |
|
|
val checksum = ((header.toInt() and 0xFF) + |
|
|
val checksum = ((header.toInt() and 0xFF) + |
|
|
(fieldType.toInt() and 0xFF) + |
|
|
(fieldType.toInt() and 0xFF) + |
|
|
(dataLen.toInt() and 0xFF) + |
|
|
(dataLen.toInt() and 0xFF) + |
|
|
(volumeData.toInt() and 0xFF)) and 0xFF |
|
|
(volumeData.toInt() and 0xFF)) and 0xFF |
|
|
|
|
|
|
|
|
// 创建完整命令 |
|
|
// 创建完整命令 |
|
|
val commandData = byteArrayOf(header, fieldType, dataLen, volumeData, checksum.toByte()) |
|
|
val commandData = byteArrayOf(header, fieldType, dataLen, volumeData, checksum.toByte()) |
|
|
|