From 17e9577b8571a2bacb16e953cd75219e3249492c Mon Sep 17 00:00:00 2001 From: fdp <1286779656@qq.com> Date: Thu, 7 Aug 2025 12:06:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=AE=BE=E7=BD=AEphy?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yunqiinnovation/ble_service/BleService.kt | 79 ++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) 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 649d22f75..299005b5f 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 @@ -127,7 +127,7 @@ object BleService { // 音频数据分块发送的常量 private val AUDIO_CHUNK_SIZE = 40 // 每次发送40字节 - private val AUDIO_SEND_INTERVAL = 30L // 发送间隔10ms + private val AUDIO_SEND_INTERVAL = 20L // 发送间隔10ms // 初始化状态 private var isInitialized = false @@ -607,8 +607,28 @@ object BleService { when { status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothProfile.STATE_CONNECTED -> { updateConnectionState(BleConst.STATE_CONNECTED) + + // 设置PHY值为2M以提高传输速度 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + try { + // 首先尝试读取当前PHY状态 + g.readPhy() + + // 然后请求设置2M PHY + g.setPreferredPhy( + BluetoothDevice.PHY_LE_2M_MASK, // TX PHY: 2M + BluetoothDevice.PHY_LE_2M_MASK, // RX PHY: 2M + BluetoothDevice.PHY_OPTION_NO_PREFERRED + ) + Log.d(TAG, "已请求设置PHY为2M") + } catch (e: Exception) { + Log.w(TAG, "设置PHY失败: ${e.message},将使用默认1M PHY") + } + } else { + Log.w(TAG, "当前Android版本不支持PHY设置(需要API 26+),使用默认1M PHY") + } + g.discoverServices() - } newState == BluetoothProfile.STATE_DISCONNECTED -> { @@ -622,6 +642,45 @@ object BleService { } } } + + /** + * PHY读取回调 - 获取当前PHY状态 + */ + override fun onPhyRead(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (status == BluetoothGatt.GATT_SUCCESS) { + val txPhyStr = getPhyString(txPhy) + val rxPhyStr = getPhyString(rxPhy) + Log.i(TAG, "当前PHY状态: TX=$txPhyStr, RX=$rxPhyStr") + } else { + Log.w(TAG, "读取PHY状态失败: status=$status") + } + } + } + + /** + * PHY更新回调 - 监控PHY设置结果 + */ + override fun onPhyUpdate(gatt: BluetoothGatt, txPhy: Int, rxPhy: Int, status: Int) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + when (status) { + BluetoothGatt.GATT_SUCCESS -> { + val txPhyStr = getPhyString(txPhy) + val rxPhyStr = getPhyString(rxPhy) + Log.i(TAG, "PHY更新成功: TX=$txPhyStr, RX=$rxPhyStr") + } + 6 -> { // GATT_REQUEST_NOT_SUPPORTED + Log.w(TAG, "设备不支持PHY更新,将继续使用1M PHY进行通信") + } + 133 -> { // GATT_ERROR + Log.w(TAG, "PHY更新出现一般性错误,可能是设备兼容性问题") + } + else -> { + Log.w(TAG, "PHY更新失败: status=$status") + } + } + } + } override fun onServicesDiscovered(g: BluetoothGatt, status: Int) { if (status != BluetoothGatt.GATT_SUCCESS) { @@ -1835,6 +1894,22 @@ startBytesStatistics() } } + /** + * 获取PHY类型的字符串描述 + */ + private fun getPhyString(phy: Int): String { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + when (phy) { + BluetoothDevice.PHY_LE_1M -> "1M" + BluetoothDevice.PHY_LE_2M -> "2M" + BluetoothDevice.PHY_LE_CODED -> "Coded" + else -> "Unknown($phy)" + } + } else { + "Unknown($phy)" + } + } + /** * 启动音频队列处理 */