英文:
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
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) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论