【问题标题】:Bluetooth connection with car与汽车的蓝牙连接
【发布时间】:2015-06-21 14:09:32
【问题描述】:

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

我需要怎么做才能检测到我的设备通过蓝牙与汽车主动连接?

提前感谢您,如果您需要其他任何东西,请告诉 mw。

编辑说明

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

【问题讨论】:

  • 您是否尝试过仅连接普通蓝牙耳机?
  • 抱歉,请完整阅读您的问题,但如果仍有问题,请务必先尝试使用普通蓝牙耳机。
  • 不,游戏的目的是连接到支持蓝牙的汽车。如果上面的代码为此目的是错误的,请指出正确的方向:),@bernlim
  • 明白。我建议你做的是确保你的代码在尝试更复杂的汽车系统之前首先连接更简单的蓝牙耳机。它应该更容易调试(我想你正坐在你的车里试图输入这个或者在拥挤的车库里)
  • @bernlim,大声笑,说起来容易做起来难,我明白它的重点,这可能看起来很奇怪。我附近有一辆支持蓝牙的汽车,但没有支持蓝牙的耳机:),这么多垫脚石吧?

标签: android bluetooth


【解决方案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()

【讨论】:

  • 请看我的编辑。我认为这将有助于获得连接的蓝牙音频设备。请看我的编辑,我已经更详细地解释了我的问题。请看一下,今天早上我意识到可能有点不清楚,谢谢!
  • @Lunchbox 我在想你可以把 sn-p 放在你的 onServiceConnected 方法中
  • 我确实实现了类似的修复,代码只是略有不同。我会尽快更新我的答案。你给我指出了正确的方向,所以+1。谢谢!
【解决方案2】:

一个快速的谷歌找到了这个答案,它适用于很多人,所以我很确定它应该适合你。来源:How to get Bluetooth Headset connection state on application start-up using Android2.2?

private static final String ACTION_BT_HEADSET_STATE_CHANGED  = "android.bluetooth.headset.action.STATE_CHANGED";
private static final int STATE_CONNECTED = 0x00000002; 
private static final int STATE_DISCONNECTED  = 0x00000000;  
private static final String EXTRA_STATE = "android.bluetooth.headset.extra.STATE";

private BroadcastReceiver mBlueToothHeadSetEventReceiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
    try {
        String action = intent.getAction();

        if(action == null)
            return;

        if(action.equals(ACTION_BT_HEADSET_STATE_CHANGED)){
            int extraData = intent.getIntExtra(EXTRA_STATE  , STATE_DISCONNECTED);
            if(extraData == STATE_CONNECTED ){

                //TODO :

            }else if(extraData == STATE_DISCONNECTED){

                //TODO:
            }
        }
        } catch (Exception e) {

        //TODO:

        }
}
};

【讨论】:

  • 看起来事件在连接到汽车时不会触发。找到后我会发布解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多