蓝牙设备连接问题

时间:2013-10-15 12:00:26

标签: java android bluetooth android-bluetooth

我正在开发一个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");
        }    
    }
};

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

你尝试过bluetooth-admin权限吗?

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

答案 2 :(得分:0)

Android文档指出“ACL连接由Android蓝牙堆栈自动管理”。可能不会在应用程序级别管理它们; BluetoothDevice.ACTION_ACL_CONNECTEDBluetoothDevice.ACTION_ACL_DISCONNECTED调度取决于设备和固件版本(例如,我经历过Nexus S正确发送它们,而较旧的GT-I5800没有)。