如何识别我的Android设备通过蓝牙连接到汽车系统。

huangapple go评论82阅读模式
英文:

How to identify when my Android device is connected to the system of a car via Bluetooth

问题

我在想是否有任何特定的“回调”或类似的内容,当我检查我的设备是否通过蓝牙连接到汽车系统时,我可以得到。我知道有一些公司在连接到这些系统时会更改用户界面。比如说,没有蓝牙连接或通过蓝牙连接耳机时的界面,与连接到汽车时完全不同的界面(例如,非常大的按钮,以便在驾驶时轻松点击)。

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

是否有其他方法可以实现?如果有,请提供一个简单的实现方法或链接到良好的文档。

英文:

I was wondering if there is any particular callback or anything similar that I could get when checking if my device is connected via bluetooth to a system of a car.

I am aware that there are some companies which change the UI when connected to these systems. Let's say, a UI for no Bluetooth connection or Bluetooth connection wearing headphones vs a completely different UI when connected to the car (for example really big buttons to be easily clicked while driving)

I had a look at BluetoothAdapter from Android. Particularly I was interested in ACTION_CONNECTION_STATE_CHANGED which could let me know when a new connection has been made, but I did not really see any way to identify when has been connected to the system of a car or any other Bluetooth device.

Is there any other way to do it? If so, please provide a simple implementation or link to good documentation.

答案1

得分: 0

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 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) {
}

}

英文:

I have followed the question proposed in the commentaries to filter the Bluetooth connections by state (In this case I am interested in the ones which are connected).

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) {
    }

}

huangapple
  • 本文由 发表于 2020年9月16日 13:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63913586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定