【问题标题】:How to identify when my Android device is connected to the system of a car via Bluetooth如何识别我的 Android 设备何时通过蓝牙连接到汽车系统
【发布时间】:2020-09-16 05:17:46
【问题描述】:

我想知道在检查我的设备是否通过蓝牙连接到汽车系统时,是否有任何特定的 callback 或类似信息。

我知道有些公司在连接到这些系统时会更改 UI。比方说,没有蓝牙连接或蓝牙连接的用户界面与连接到汽车时完全不同的用户界面(例如在驾驶时很容易点击的非常大的按钮)

我查看了来自 Android 的 BluetoothAdapter。特别是我对ACTION_CONNECTION_STATE_CHANGED 很感兴趣,它可以让我知道何时建立了新连接,但我并没有真正看到任何方法来识别何时连接到汽车系统或任何其他蓝牙设备。

还有其他方法吗?如果是这样,请提供一个简单的实现或良好文档的链接。

【问题讨论】:

标签: java android bluetooth


【解决方案1】:

我已按照评论中提出的问题按状态过滤蓝牙连接(在这种情况下,我对已连接的连接感兴趣)。

public class BluetoothServiceListener implements BluetoothProfile.ServiceListener {
    private String TAG = "bluetoothListener";
    private static final int[] states={
            BluetoothProfile.STATE_CONNECTED};
    @Override
    public void onServiceConnected(int profile, BluetoothProfile bluetoothProfile) {
        List<BluetoothDevice> Devices=bluetoothProfile.getDevicesMatchingConnectionStates(states);
        for (BluetoothDevice device:Devices){
            if(device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO);
                Log.i(TAG, "NAME | " + device.getName() + device.getBluetoothClass().getDeviceClass());
        }
    }

    @Override
    public void onServiceDisconnected(int profile) {
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    相关资源
    最近更新 更多