简体   繁体   中英

Bluetooth Device Connectivity issue

I am developing an android app where in i am checking if two devices are connected via bluetooth

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.

    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?

<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". 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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