【发布时间】:2015-06-21 14:09:32
【问题描述】:
我正在尝试检查我的设备何时与汽车连接。我假设汽车就像一个蓝牙耳机,因此我在我的活动 onCreate 中使用了以下代码:
// 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。
编辑说明
以防我的问题被误解。当设备进入通过蓝牙连接到汽车的状态时,我想得到通知(只是一个日志)。这样的事情可能吗?
【问题讨论】:
-
您是否尝试过仅连接普通蓝牙耳机?
-
抱歉,请完整阅读您的问题,但如果仍有问题,请务必先尝试使用普通蓝牙耳机。
-
不,游戏的目的是连接到支持蓝牙的汽车。如果上面的代码为此目的是错误的,请指出正确的方向:),@bernlim
-
明白。我建议你做的是确保你的代码在尝试更复杂的汽车系统之前首先连接更简单的蓝牙耳机。它应该更容易调试(我想你正坐在你的车里试图输入这个或者在拥挤的车库里)
-
@bernlim,大声笑,说起来容易做起来难,我明白它的重点,这可能看起来很奇怪。我附近有一辆支持蓝牙的汽车,但没有支持蓝牙的耳机:),这么多垫脚石吧?