0

我正在开发一个 android 应用程序,我正在检查两个设备是否通过蓝牙连接

我正在使用以下代码注册广播接收器。

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);

    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);

BroadcastReceiver 看起来像这样。

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        // When discovery finds a device

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
        {   
            Log.e("bluetooth connected","bluetooth connected");
        }
        else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
        {
            Log.e("bluetooth not connected","bluetooth not connected");
        }    
    }
};

这怎么行不通。不确定我哪里出错了。请帮忙!谢谢!

4

3 回答 3

2

您的清单中是否有 BLUETOOTH 权限?

<uses-permission android:name="android.permission.BLUETOOTH" />

也可以代替两次注册接收器并使用两个过滤器

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);    

this.registerReceiver(mReceiver, filter);
于 2013-10-15T12:07:20.563 回答
0

你试过蓝牙管理员权限吗?

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
于 2013-10-15T18:04:47.260 回答
0

Android 文档指出“ACL 连接由 Android 蓝牙堆栈自动管理”。可能不期望它们在应用程序级别进行管理;BluetoothDevice.ACTION_ACL_CONNECTED并且BluetoothDevice.ACTION_ACL_DISCONNECTED调度取决于设备和固件版本(例如,我体验到 Nexus S 可以正确调度它们,而较旧的 GT-I5800 则没有)。

于 2013-10-16T07:42:57.497 回答