蓝牙连接汽车

Lun*_*box 6 android bluetooth

我正在尝试检查我的设备何时与汽车相连.我认为这款车就像一个蓝牙耳机,因此我在我的活动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);
Run Code Online (Sandbox Code Playgroud)

当我启动应用程序时,打开和关闭蓝牙,我得到以下输出:

Headset event called at "current time" - 1
Run Code Online (Sandbox Code Playgroud)

当我将我的设备与汽车配对时,我得到完全相同的输出:

Headset event called at "current time" - 1
Run Code Online (Sandbox Code Playgroud)

我需要做些什么来检测我的设备是否通过蓝牙与汽车主动连接?

提前谢谢你,如果你还需要其他任何东西,请告诉我们.

编辑澄清

以防万一我的问题被误解了.当设备进入通过蓝牙连接到汽车的状态时,我想得到通知(只是一个日志).这样的事情可能吗?

jlh*_*ora 1

我现在无法尝试,但也许这可行:

int[] validStates = {BluetoothHeadset.STATE_AUDIO_CONNECTED};
List<BluetoothDevice> mConnectedDevices =
  mBluetoothHeadset.getDevicesMatchingConnectionStates(validStates);
if (!mConnectedDevices.isEmpty()) {
    // You've got something connected here
}
Run Code Online (Sandbox Code Playgroud)

资料来源:

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