简体   繁体   English

蓝牙设备连接问题

[英]Bluetooth Device Connectivity issue

I am developing an android app where in i am checking if two devices are connected via bluetooth 我正在开发一个Android应用程序,我正在检查两个设备是否通过蓝牙连接

I am Registering the Broadcast Reciever using the below code. 我正在使用以下代码注册广播接收器。

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

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

The BroadcastReceiver looks like this. 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");
        }    
    }
};

How ever this is not working. 这怎么不行。 Not Sure Where i am going wrong. 不确定我哪里错了。 Please Help! 请帮忙! Thanks! 谢谢!

Do you have BLUETOOTH permission in your manifest? 你的清单中有BLUETOOTH权限吗?

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

also instead of registering a receiver twice and using two filters you could do 也可以使用两个过滤器来代替两次注册接收器

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" />

The Android documentation states that "ACL connections are managed automatically by the Android Bluetooth stack". Android文档声明“ACL连接由Android蓝牙堆栈自动管理”。 Probably, they are not expected to be managed at application level; 可能不会在应用程序级别管理它们; BluetoothDevice.ACTION_ACL_CONNECTED and BluetoothDevice.ACTION_ACL_DISCONNECTED dispatching depends on device and firmware version (for instance, I experienced that the Nexus S dispatches them correctly, while the older GT-I5800 doesn't). 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