有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java蓝牙设备连接问题

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

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

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

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

广播接收器是这样的

    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");
        }    
    }
};

无论如何,这是行不通的。我不确定我会错在哪里。请帮忙!谢谢


共 (2) 个答案

  1. # 1 楼答案

    你试过蓝牙管理员许可吗

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
  2. # 2 楼答案

    你的舱单上有蓝牙权限吗

    <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);