|
|
|
@ -26,6 +26,7 @@ import com.yunqiinnovation.ble_service.BleCompanionManager |
|
|
|
import android.bluetooth.BluetoothAdapter |
|
|
|
import android.bluetooth.BluetoothDevice |
|
|
|
import android.bluetooth.BluetoothManager |
|
|
|
import android.bluetooth.BluetoothProfile |
|
|
|
import android.content.BroadcastReceiver |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.content.pm.PackageManager |
|
|
|
@ -677,21 +678,130 @@ private var companionManager: BleCompanionManager? = null |
|
|
|
super.onDestroy() |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 蓝牙耳机设备信息数据类 |
|
|
|
*/ |
|
|
|
data class BluetoothHeadsetInfo( |
|
|
|
val name: String, |
|
|
|
val address: String |
|
|
|
) |
|
|
|
|
|
|
|
private fun checkBleConnection() { |
|
|
|
Log.i(TAG, "checkBleConnection收到连接请求") |
|
|
|
|
|
|
|
try { |
|
|
|
// 首先检查经典蓝牙状态 |
|
|
|
if (bluetoothAdapter == null) { |
|
|
|
Log.e(TAG, "蓝牙适配器不可用,无法进行BLE连接") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
if (!bluetoothAdapter!!.isEnabled) { |
|
|
|
Log.w(TAG, "经典蓝牙未开启,无法进行BLE连接") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 检查蓝牙耳机连接状态 |
|
|
|
checkBluetoothHeadsetConnection { hasConnectedHeadset, headsetInfoList -> |
|
|
|
if (hasConnectedHeadset && headsetInfoList.isNotEmpty()) { |
|
|
|
Log.i(TAG, "检测到 ${headsetInfoList.size} 个蓝牙耳机已连接") |
|
|
|
|
|
|
|
// 查找匹配的配对设备 |
|
|
|
val pairedMacAddress = BleService.getPairedMacAddress() |
|
|
|
val targetHeadset = headsetInfoList.find { it.address == pairedMacAddress } |
|
|
|
|
|
|
|
if (targetHeadset != null) { |
|
|
|
Log.i(TAG, "找到匹配的蓝牙耳机: ${targetHeadset.name} (${targetHeadset.address})") |
|
|
|
|
|
|
|
// 使用协程异步处理连接 |
|
|
|
launch { |
|
|
|
if(!BleService.isConnected()) { |
|
|
|
if (!BleService.isConnected()) { |
|
|
|
BleService.connect() |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
Log.w(TAG, "未找到匹配的配对设备,已连接的设备:") |
|
|
|
headsetInfoList.forEach { headset -> |
|
|
|
Log.w(TAG, " - ${headset.name} (${headset.address})") |
|
|
|
} |
|
|
|
Log.w(TAG, "配对的MAC地址: $pairedMacAddress") |
|
|
|
} |
|
|
|
} else { |
|
|
|
Log.w(TAG, "未检测到蓝牙耳机连接,跳过BLE连接") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "连接设备失败: ${e.message}", e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 检查蓝牙耳机连接状态 |
|
|
|
* @param callback 回调函数,返回是否有蓝牙耳机连接以及耳机信息列表 |
|
|
|
*/ |
|
|
|
private fun checkBluetoothHeadsetConnection(callback: (Boolean, List<BluetoothHeadsetInfo>) -> Unit) { |
|
|
|
try { |
|
|
|
bluetoothAdapter?.let { adapter -> |
|
|
|
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) |
|
|
|
!= PackageManager.PERMISSION_GRANTED) { |
|
|
|
Log.w(TAG, "缺少蓝牙连接权限,无法检查蓝牙耳机连接状态") |
|
|
|
callback(false, emptyList()) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 检查HEADSET设备连接状态 |
|
|
|
adapter.getProfileProxy(this, object : BluetoothProfile.ServiceListener { |
|
|
|
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) { |
|
|
|
try { |
|
|
|
val connectedDevices = proxy.connectedDevices |
|
|
|
val hasConnectedHeadset = connectedDevices.isNotEmpty() |
|
|
|
|
|
|
|
Log.i(TAG, "蓝牙耳机连接设备数量: ${connectedDevices.size}") |
|
|
|
|
|
|
|
if (hasConnectedHeadset) { |
|
|
|
// 返回所有连接的蓝牙耳机设备信息 |
|
|
|
val headsetInfoList = connectedDevices.map { device -> |
|
|
|
BluetoothHeadsetInfo( |
|
|
|
name = device.name ?: "Unknown Headset", |
|
|
|
address = device.address |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
Log.d(TAG, "蓝牙耳机连接设备列表:") |
|
|
|
headsetInfoList.forEach { info -> |
|
|
|
Log.d(TAG, " - ${info.name} (${info.address})") |
|
|
|
} |
|
|
|
|
|
|
|
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) |
|
|
|
callback(true, headsetInfoList) |
|
|
|
} else { |
|
|
|
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) |
|
|
|
callback(false, emptyList()) |
|
|
|
} |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "检查蓝牙耳机连接状态时发生异常", e) |
|
|
|
adapter.closeProfileProxy(BluetoothProfile.HEADSET, proxy) |
|
|
|
callback(false, emptyList()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override fun onServiceDisconnected(profile: Int) { |
|
|
|
Log.d(TAG, "蓝牙耳机服务断开连接") |
|
|
|
} |
|
|
|
}, BluetoothProfile.HEADSET) |
|
|
|
|
|
|
|
} ?: run { |
|
|
|
Log.e(TAG, "蓝牙适配器为空") |
|
|
|
callback(false, emptyList()) |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
Log.e(TAG, "检查蓝牙耳机连接状态时发生异常", e) |
|
|
|
callback(false, emptyList()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建前台服务通知 |
|
|
|
*/ |
|
|
|
|