如何在Kotlin API 33中以编程方式更改蓝牙编解码器?



我想把蓝牙编解码器类型从AAC改为SBC。这是一个非常新的特性,文档还不完整。BluetoothAdapter

我尝试了setcodecconfigpreference;但现在不支持....

// Manifest
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" tools:ignore="ProtectedPermissions" />
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// API 33+ ----------------------------------------------------//
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val permissions = arrayOf(
android.Manifest.permission.BLUETOOTH,
android.Manifest.permission.BLUETOOTH_PRIVILEGED,
android.Manifest.permission.BLUETOOTH_ADMIN,
android.Manifest.permission.BLUETOOTH_CONNECT,
android.Manifest.permission.BLUETOOTH_SCAN
)
if (applicationContext.checkSelfPermission(android.Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, permissions,0)
}

val device = BluetoothDevice.DEVICE_TYPE_CLASSIC
val codec = BluetoothCodecConfig.Builder()
.setCodecType(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC)
.build()

//   HOW DO I APPLY THIS ???
}
}

你需要首先设置你的SDK来使用这里描述的Android 14预览SDK(详细信息请参阅文档):

android {
compileSdkPreview "UpsideDownCake"
...
defaultConfig {
targetSdkPreview "UpsideDownCake"
}
}

这应该会让你访问Android 14的新api,它可以通过获取代理和设置基于连接设备的配置来使用:

context.getSystemService(BluetoothManager::class.java)
.adapter
.getProfileProxy(
context,
object : BluetoothProfile.ServiceListener {
override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
(proxy as BluetoothA2dp).setCodecConfigPreference(
bluetoothDevice,
BluetoothCodecConfig.Builder()
.setCodecType(BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC)
.build()
)
}
override fun onServiceDisconnected(profile: Int) {}
},
BluetoothProfile.A2DP
)

相关内容

  • 没有找到相关文章

最新更新