我正在尝试检查我的设备何时与汽车相连。我认为这款车就像一个蓝牙耳机,因此我在我的活动中使用了以下代码:
// 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
我需要做些什么来检测我的设备是否通过蓝牙与汽车主动连接?
提前感谢您,如果您需要其他任何内容,请告知我们。
编辑澄清
以防万一我的问题被误解了。当设备进入通过蓝牙连接到汽车的状态时,我想得到通知(只是一个日志)。这样的事情可能吗?
答案 0 :(得分:1)
我现在无法尝试,但也许这可行:
int[] validStates = {BluetoothHeadset.STATE_AUDIO_CONNECTED};
List<BluetoothDevice> mConnectedDevices =
mBluetoothHeadset.getDevicesMatchingConnectionStates(validStates);
if (!mConnectedDevices.isEmpty()) {
// You've got something connected here
}
来源:
http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html http://developer.android.com/reference/android/bluetooth/BluetoothProfile.html#getConnectedDevices()
答案 1 :(得分:0)
一个快速谷歌找到了这个答案,它适用于很多人,所以我很确定它应该适合你。资料来源:How to get Bluetooth Headset connection state on application start-up using Android2.2?
array.push_back(std::move(day));