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.
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.
In this class, I only declared some variables:
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: