How to check if a bluetooth device is paired - android

I'm trying to check if there is a bluetooth device paired when running my app.
In the main activity, I find bluetooth devices and pair to them. In the second activity, I must check if there is a device paired or not.
If a device is conected, it starts automatically sending data, but if there is no conexion, then it simply shows a toast.
I need to do this just when the second activity starts. I found this code, but I don't know how to make it to start when the activity is just created.
public void onCreate() {
//...
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 that listens for bluetooth broadcasts
private final BroadcastReceiver BTReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Do something if connected
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Do something if disconnected
}
}
};

Here is a complete description of the problem, with the correct answer to solve it:
Action to know if there is any bluetooth paired device

Related

How to know if my BluetoothDevice is connected?

I'm trying to check if my BluetoothDevice is connected to something.
If it is connected, obtain the data of the other device
I want to implement this function to my application since I need to monitor if the connection was lost or is still connected and add a visual indicator in case the connection changes.
Tried with this function but the toast shows nearby devices (not connected) constantly:
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
Toast.makeText(getApplicationContext(),name,Toast.LENGTH_SHORT).show();
}
AFAIK there is no way to view the state of the connection. Instead you monitor for changes in the state of the bluetooth connection. So you can register a receiver and then receive a broadcast when the device is disconnected.
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(rec, filter);
private BroadcastReceiver rec = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_DISCONNECTED.equals(action)) {
// Bluetooth is now disconnected
}
}

Bluetooth scan returns nothing

I am recently learning Android. I tried to implement the function that searches for Bluetooth devices. I am able to list all the previously paired devices, but I am not able to discover new devices.
Permissions are set in AndroidManifest.xml.
Here is my code:
public void listDiscoverableDevices(boolean status) {
// Create a BroadcastReceiver for ACTION_FOUND
mBluetoothAdapter.cancelDiscovery();
mArrayAdapter.clear();
mDisplay.setText("Search");
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//BluetoothDevice.
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mArrayAdapter.add(device.getAddress());
} else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
} else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//mDisplay.setText(action);
mArrayAdapter.add(action);
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
if(!mBluetoothAdapter.startDiscovery()) {
mDisplay.setText("Discover failed!");
}
}
It seems to me that the BluetoothDevice.ACTION_FOUND action is never received. I have been struggling with this for two days. Any idea?
It took me two days. Today I finally found out that why my program didn't work the first time. Starting from Android 6.0, you also need either android.permission.ACCESS_FINE_LOCATION or android.permission.ACCESS_FINE_LOCATION permission to scan for remote Bluetooth devices.
And you may need to go to "Settings"->"Apps"->yourApp->"Permissions" to turn on the location service permission. Or I guess you can also request the permission programmatically.

Bluetooth.ACTION_FOUND not registering

I have a bluetooth device.
If:
They have already paired and connected to the device
it becomes unplugged
then it becomes plugged back in
I would like to auto-connect to it. That's why I'm listening to ACTION_FOUND.
My code is fairly simple. The ACTION_BOND_STATE_CHANGED and ACTION_ACL_DISCONNECTED work just fine. I am completely unable to get the ACTION_FOUND to catch though. My "INTENT RECEIVED" log message never prints…
I feel like I am missing something simple. Thanks!
public void registerReceiver() {
if (BuildConfig.DEBUG) Log.e(TAG, "REGISTERING RECEIVER");
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
mainActivity.registerReceiver(receiver, filter);
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BuildConfig.DEBUG) Log.e(TAG, "INTENT RECEIVED: " + String.valueOf(action));
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// … some stuff …
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
autoConnectDevice();
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
disconnectDevice();
}
}
};
public void unregisterReceiver() {
if (BuildConfig.DEBUG) Log.e(TAG, "UNREGISTERING RECEIVER");
mainActivity.unregisterReceiver(receiver);
}
I believe the ACTION_FOUND event only fires when you are running device discovery with the intent to pair your device to another. It would not fire when an already paired device comes back into range. Unfortunately, there does not appear to be an event for the case that you want.
Your best bet will be to start an AsycTask or Thread when the connection is lost and try to reconnect until successful or until you hit an arbitrary time limit and give up.

Bluetooth status on statusbar

I have my own application home screen as default home screen for my android device. I have a status bar in my application and I am managing the Bluetooth connection status with an Imageview(onclick) in that status bar.
If I click on the Imageview in my statusbar I am turning the bluetooth on/off. I am changing the background of imageview according to the status of bluetooth.
If bluetooth is ON - Blueimage
If bluetooth is OFF - greyImage
So my question is - If I enable/disable the bluetooth in bluetoth settings page and press the back button and navigate to my home screen then Imageview(background image) in my status bar should get changed automatically.
I tried a lot updating the image of imageview on back button press(by overriding onbackpressed method) but no result.
Is there any similar API with which we can read/store the status of bluetooth in a variable as soon as we enable/disable the bluetooth in bluetooth settings page and according to that the status bar imageview backround will get changed automatically?
Any help is greatly appreciated, Thanks
you can put the code that checked for the Bluetooth status in the onRestart() method
like this:
public class yourClass {
public void onCreate(Bundle savedInstanceState) {
// your code
}
public void onRestart() {
// put the checked code for the Bluetooth status here
}
You need to add broadcast receivers in your code to receive once bluetooth is switched on or off in your activity class. for your reference :
public void onCreate() {
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
this.registerReceiver(mReceiver, filter3);
}
//The BroadcastReceiver that listens for bluetooth broadcasts
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);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
... //Device found
}
else if (BluetoothAdapter.ACTION_ACL_CONNECTED.equals(action)) {
... //Device is now connected
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
... //Done searching
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
... //Device is about to disconnect
}
else if (BluetoothAdapter.ACTION_ACL_DISCONNECTED.equals(action)) {
... //Device has disconnected
}
once you receives the status do your functionality .. done !!

16s time delay until the bluetooth disconnected request is recognized

I am working with a bluetooth device (the IOIO developer board).
I want to listen, when my device is disconnected. It is working ok with the code above, but it is not recognized instantaneously. When I power off my bluetooth developer board, I have to wait ~16s until my Android recognized that the connection was lost.
Does anybody know why? I heard it should be a internal Android limitation, that the connection is not checked so often?
Does anybody know how to write a thread which "pings" the bluetooth device if it is still there? I think it is very similar to the Android BluetoothChat example, but I couldn't fixed it on my own.
Thanks.
Felix
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);
}
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);
if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
Toast.makeText(context,"The device is about to disconnect" , Toast.LENGTH_LONG).show();
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
Toast.makeText(context,"Device has disconnected" , Toast.LENGTH_LONG).show();
}
}
};

Categories

Resources