显示连接的蓝牙设备 Kotlin
Show connected bluetooth devices Kotlin
我只能找到如何显示配对的蓝牙设备,而不是当前连接的蓝牙设备。这是显示配对的代码:
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]
)
}
有人知道如何显示当前连接的蓝牙设备而不是配对的设备吗?
谢谢
我认为没有直接的方法来检查配对的蓝牙设备是否已连接,因为它可以随时更改。我建议的最佳解决方案是列出所有配对设备并使用return 蓝牙状态改变列表中指定设备的广播接收器,如下所述 link
Android bluetooth get connected devices
@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 + " ")
}
现在显示的是已连接的蓝牙设备,而不是已配对的设备!
还要感谢来自其他 post 的@Abdallah AbuSalah:Android bluetooth get connected devices
我只能找到如何显示配对的蓝牙设备,而不是当前连接的蓝牙设备。这是显示配对的代码:
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]
)
}
有人知道如何显示当前连接的蓝牙设备而不是配对的设备吗? 谢谢
我认为没有直接的方法来检查配对的蓝牙设备是否已连接,因为它可以随时更改。我建议的最佳解决方案是列出所有配对设备并使用return 蓝牙状态改变列表中指定设备的广播接收器,如下所述 link Android bluetooth get connected devices
@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 + " ")
}
现在显示的是已连接的蓝牙设备,而不是已配对的设备! 还要感谢来自其他 post 的@Abdallah AbuSalah:Android bluetooth get connected devices