Action to know if there is any bluetooth paired device

4k views Asked by At

When starting an activity, I need to know if there is any bluetooth paired device, and then do something depending on the result. For this, I do the next:

In the onResume:

protected void onResume() {
    super.onResume();

    /**Filters para comprobar el BroadcastReceiver*/
    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    this.registerReceiver(mReceiver, filter1);
    this.registerReceiver(mReceiver, filter2);

Then, I use the BroadcastReceiver:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        /**Do something if connected*/
        if(action.equals("android.bluetooth.device.action.ACL_CONNECTED")) {
            //Action
        }
        /**Do something if disconnected*/
        else if (action.equals("android.bluetooth.device.action.ACL_DISCONNECTED")) {               
            Toast.makeText(getApplicationContext(), "No device paired", Toast.LENGTH_SHORT).show();
        }
    }
};

In the Manifest:

<activity 
        android:name=".Configuration"
        android:label="@string/config_title" >
        <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED"/>
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED"/>
        </intent-filter>
    </activity>

But Still isn't doing what it suposses that must do, so something must be wrong or I forgot to do something.

1

There are 1 answers

0
masmic On

If someone is interested, the way that I get solved this, is this:

To check if there is any device conected to perform an action, I've used methods based on the BluetoothChat example app.

public class Transmission {
    ....

    /**
     * Return the current connection state.
     */
    public synchronized int getState() {
        return GlobalVar.mState;
    }

In this class, I only declared some variables:

public class GlobalVar {
    ....

    public static int mState;

    // Constants that indicate the current connection state
    public static final int STATE_NONE = 0;       // we're doing nothing
    public static final int STATE_LISTEN = 1;     // now listening for incoming connections
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
    public static final int STATE_CONNECTED = 3;  // now connected to a remote device

And finally, this is the class where I perform the actions and where I want to check in it's initialization if there is any conected device:

/**Check that we're actually connected before trying anything*/
    if (GlobalVar.mTransmission.getState() == GlobalVar.STATE_CONNECTED) {
        //If there is a conection, do an action
    }
    /**If there is no connection, then shows toast*/
    else {
        Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();