与汽车的蓝牙连接

6

我想检查我的设备何时连接到了汽车。我假设汽车像蓝牙耳机一样,因此我在我的活动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

我需要做什么才能检测到我的设备正在通过蓝牙与汽车进行连接?

提前感谢您的帮助,如果还需要其他信息,请告知。

编辑澄清

以防我的问题被误解。我想在设备进入通过蓝牙连接到汽车的状态时得到通知(仅记录)。这种情况是否可能?


你尝试过连接普通的蓝牙耳机吗? - bunbun
抱歉,刚才没有完全阅读你的问题,但如果仍然存在问题,请先尝试使用普通的蓝牙耳机。 - bunbun
不,游戏的目的是连接到“n个蓝牙启用的汽车”。如果上述代码不适用于此目的,请指出正确的方向:)@bernlim - Lunchbox
明白了。我的建议是,在尝试连接更复杂的汽车系统之前,先确保您的代码可以与更简单的蓝牙耳机连接。这样调试起来应该会更容易(我想象你正在车里或者在狭小的车库里试着打字)。 - bunbun
@bernlim,哈哈,说起来容易做起来难,我能理解这个观点,这可能看起来很奇怪。我附近有一辆蓝牙汽车,但是没有蓝牙耳机:),这就是所谓的慢慢来呗? - Lunchbox
哈哈,我明白了。嗯,我们都有一些奇怪的请求。请查看我的建议,看看是否有帮助。 - bunbun
2个回答

1

请查看我的编辑。我认为这样可以获取已连接的蓝牙音频设备。请查看我的编辑,我已经进一步解释了我的问题。请看一下,我今天早上意识到可能有点不清楚,无论如何感谢! - Lunchbox
@Lunchbox 我在想你可以把那段代码放到你的 onServiceConnected 方法里面。 - jlhonora
我已经实现了类似的修复,代码只有轻微的不同。我会尽快更新我的答案。你指引了我正确的方向,所以加一。谢谢! - Lunchbox

0
一个快速的谷歌搜索找到了这个答案,它对很多人都有效,所以我相信它应该也适用于你。来源:如何在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:

        }
}
};

看起来连接到汽车时事件没有触发。我会在找到解决方案后发布它。 - Lunchbox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接