我想检查我的设备何时连接到了汽车。我假设汽车像蓝牙耳机一样,因此我在我的活动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
我需要做什么才能检测到我的设备正在通过蓝牙与汽车进行连接?
提前感谢您的帮助,如果还需要其他信息,请告知。
编辑澄清
以防我的问题被误解。我想在设备进入通过蓝牙连接到汽车的状态时得到通知(仅记录)。这种情况是否可能?