android - 与汽车的蓝牙连接

标签 android bluetooth

我正在尝试检查我的设备何时与汽车连接。我假设汽车就像一个蓝牙耳机,因此我在我的 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/

相关文章:

java - 无法在 Android 中扫描或发现 BT 和 BLE 设备

java - 将日期转换为文本格式

java - 暂停计时器,然后继续

Android连续语音识别

android - 蓝牙 RFCOMM 连接 : read serial data from com port in Windows 7

c# - Android 到 PC/PC 到 android 蓝牙/wifi 实时音频流

java - 谷歌广告没有显示在我的手机上

android - 无法从中加载模块元数据

android - 如何解决错误 onClientConnectionState() - status=22 clientIf=7 in BLE

ios - 如何从 iOS 网络应用程序与蓝牙设备配对?