繁体   English   中英

蓝牙设备连接问题

[英]Bluetooth Device Connectivity issue

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

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

你的清单中有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);

你尝试过bluetooth-admin权限吗?

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM