|
|
|
@ -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) |
|
|
|
g.discoverServices() |
|
|
|
|
|
|
|
// 设置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 -> { |
|
|
|
@ -623,6 +643,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) { |
|
|
|
updateConnectionState(BleConst.STATE_ERROR) |
|
|
|
@ -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)" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 启动音频队列处理 |
|
|
|
*/ |
|
|
|
|