gpt4 book ai didi

android - 与汽车的蓝牙连接

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:47 24 4
gpt4 key购买 nike

我正在尝试检查我的设备何时与汽车连接。我假设汽车就像一个蓝牙耳机,因此我在我的 onCreate Activity 中使用了以下代码:

    // 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
}

来源:

http://developer.android.com/reference/android/bluetooth/BluetoothHeadset.html http://developer.android.com/reference/android/bluetooth/BluetoothProfile.html#getConnectedDevices()

关于android - 与汽车的蓝牙连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30965414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com