我只能找到如何顯示配對的藍牙設備,而不是當前連接的藍牙設備。這是顯示配對的代碼:
val blueToothManager=applicationContext.getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter=blueToothManager.adapter
var pairedDevices = bluetoothAdapter.bondedDevices
var data:StringBuffer = StringBuffer()
for(device: BluetoothDevice in pairedDevices)
{
data.append(device.name " ")
}
if(data.isEmpty())
{
bluetoothText.text = "0 Devices Connected"
val leftDrawable: Drawable? = getDrawable(R.drawable.ic_bluetooth_disabled)
val drawables: Array<Drawable> = bluetoothText.getCompoundDrawables()
bluetoothText.setCompoundDrawablesWithIntrinsicBounds(
leftDrawable, drawables[1],
drawables[2], drawables[3]
)
}
else
{
bluetoothText.text = data
val leftDrawable: Drawable? = getDrawable(R.drawable.ic_bluetooth_connected)
val drawables: Array<Drawable> = bluetoothText.getCompoundDrawables()
bluetoothText.setCompoundDrawablesWithIntrinsicBounds(
leftDrawable, drawables[1],
drawables[2], drawables[3]
)
}
有人知道如何顯示當前連接的藍牙設備而不是配對設備嗎?謝謝
uj5u.com熱心網友回復:
我認為沒有直接的方法來檢查配對的藍牙設備是否已連接,因為它可以隨時更改.. 我可以建議的最佳解決方案是列出所有配對的設備并使用回傳的廣播接收器藍牙的狀態更改串列中的指定設備,如以下鏈接中所述 Android 藍牙獲取連接的設備
uj5u.com熱心網友回復:
@Mohamed Saleh,謝謝。我添加了這個:
private fun isConnected(device: BluetoothDevice): Boolean {
return try {
val m: Method = device.javaClass.getMethod("isConnected")
m.invoke(device) as Boolean
} catch (e: Exception) {
throw IllegalStateException(e)
}
}
并改變了這一點:
for(device in pairedDevices)
{
if(isConnected((device)))
data.append(" " device.name " ")
}
它現在顯示連接的藍牙設備,而不是配對的設備!還要感謝另一篇文章中的@Abdallah AbuSalah:Android 藍牙連接設備
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/435786.html
