Android 与汽车的蓝牙连接
我正在尝试检查我的设备何时与汽车连接。我假设汽车的作用类似于蓝牙耳机,因此我在“创建”活动中使用了以下代码:Android 与汽车的蓝牙连接,android,bluetooth,Android,Bluetooth,我正在尝试检查我的设备何时与汽车连接。我假设汽车的作用类似于蓝牙耳机,因此我在“创建”活动中使用了以下代码: // Get the default adapter BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceL
// Get the default adapter
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothProfile.ServiceListener mProfileListener = new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = (BluetoothHeadset) proxy;
LogginUtil.logString("BluetoothApp", "Headset event called at " + today.format("%k:%M:%S") + " - " + profile);
} else {
LogginUtil.logString("BluetoothApp", "Other event called at " + today.format("%k:%M:%S") + " - " + profile);
}
}
public void onServiceDisconnected(int profile) {
if (profile == BluetoothProfile.HEADSET) {
mBluetoothHeadset = null;
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
LogginUtil.logString("BluetoothApp", "Headset event disconnected at " + today.format("%k:%M:%S"));
}
}
};
// Establish connection to the proxy.
mBluetoothAdapter.getProfileProxy(getApplicationContext(), mProfileListener, BluetoothProfile.HEADSET);
启动应用程序时,在蓝牙打开和关闭的情况下,我得到以下输出:
Headset event called at "current time" - 1
当我将设备与汽车配对时,得到的输出完全相同:
Headset event called at "current time" - 1
我需要做什么才能检测到我的设备通过蓝牙主动连接到汽车
提前谢谢你,如果你还需要什么,请告诉mw
编辑澄清
以防我的问题被误解。当设备进入通过蓝牙连接到汽车的状态时,我想得到通知(只是一个日志)。这样做可能吗?我现在无法尝试,但这可能有效:
int[] validStates = {BluetoothHeadset.STATE_AUDIO_CONNECTED};
List<BluetoothDevice> mConnectedDevices =
mBluetoothHeadset.getDevicesMatchingConnectionStates(validStates);
if (!mConnectedDevices.isEmpty()) {
// You've got something connected here
}
int[]validStates={BluetoothHeadset.STATE_AUDIO_CONNECTED};
列出mConnectedDevices=
mBluetoothHeadset.getDevicesMatchingConnectionStates(有效状态);
如果(!mConnectedDevices.isEmpty()){
//你这里有点关联
}
资料来源:
谷歌很快找到了这个答案,它适用于很多人,所以我很确定它应该适用于你。资料来源:
请看我的编辑。我认为这将有助于连接蓝牙音频设备。请看我的编辑,我已经详细解释了我的问题。请看一看,我今天早上意识到它可能有点不清楚,不管怎样,谢谢@午餐盒我想你可以把这个片段放在你的
onServiceConnected
方法中。我实现了类似的修复,代码只是略有不同。我一有时间就会更新我的答案。你给我指出了正确的方向,所以+1。谢谢您是否尝试过只连接普通蓝牙耳机?抱歉,请仔细阅读您的问题,但如果仍然存在问题,请务必先尝试使用普通蓝牙耳机。否,游戏的目的是连接到启用蓝牙功能的汽车。如果上述代码错误,请将e指向正确的方向:),@Bernlim。我建议您在尝试更复杂的汽车系统之前,首先确保您的代码能够与更简单的蓝牙耳机连接。调试应该更容易(我想象你坐在车里试着把它打印出来,或者在拥挤的车库里)@bernlim,哈哈,说起来容易做起来难,我明白它的意思,这可能看起来很奇怪。我附近有一辆支持蓝牙功能的汽车,但没有支持蓝牙功能的耳机:),踏脚石就这么多了,对吧?连接到汽车上时,活动似乎不会启动。我会在找到解决方案后发布。
private static final String ACTION_BT_HEADSET_STATE_CHANGED = "android.bluetooth.headset.action.STATE_CHANGED";
private static final int STATE_CONNECTED = 0x00000002;
private static final int STATE_DISCONNECTED = 0x00000000;
private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";
private BroadcastReceiver mBlueToothHeadSetEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if(action == null)
return;
if(action.equals(ACTION_BT_HEADSET_STATE_CHANGED)){
int extraData = intent.getIntExtra(EXTRA_STATE , STATE_DISCONNECTED);
if(extraData == STATE_CONNECTED ){
//TODO :
}else if(extraData == STATE_DISCONNECTED){
//TODO:
}
}
} catch (Exception e) {
//TODO:
}
}
};