我想制作一个蓝牙广播接收器,只对连接蓝牙免提车载套件做出反应。但我不知道如何过滤掉它。
有没有办法如何从意图中获取 BluetoothClass.Device ,还是以其他方式获得它?
我从下面的东西开始,但我没有得到正确的方法。
另外,当您断开与设备的连接时,会调用以下if-block(仅在没有final int state = intent.getIntExtra(???);
if (action.equals("android.bluetooth.device.action.ACL_CONNECTED")) {
final int state = intent.getIntExtra(???);
}
希望你能帮助我,坦克给你们所有人。
SOLUTION:
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int btClass = device.getBluetoothClass().getDeviceClass();
if (btClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE|| btClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO){...}
答案 0 :(得分:0)
您需要使用侦听BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED的过滤器注册BroadcastReceiver(BluetoothHeadset类适用于耳机和免提配置文件)。您还需要为清单添加BLUETOOTH权限。
这只会让您更改连接,因此您必须使用蓝牙API来确定它是否已连接或断开连接。
要获取设备,您可以使用BluetoothHeadset.getDevicesMatchingConnectionStates(int []状态)获取所有已连接设备的列表。