I wrote code about android bluetooth programming.I'm connecting to HC06 modules.I used socket and I used Runnable method.If connection has ended I close socket
When I disconnect the bluetooth cable I can see the message of connection failure.But I exit from bluetooth connection range I cannot see the message of connection failure.
I cannot exit from bluetooth device connection range.Even if I move away.
What must I do?
Use intent filters to listen to the ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECT_REQUESTED, and ACTION_ACL_DISCONNECTED broadcasts:
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
}
}
};
Related
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
}
}
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
I used ACTION_MEDIA_MOUNTED and ACTION_MEDIA_UNMOUNTED to detect USB connection on Nexus 4, but I cannot receive any broadcast signal.
Here is my broadcast receiver code:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
debugReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
debugOn = true;
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
debugOn = false;
}
}
};
registerReceiver(debugReceiver, filter);
Any ideas? I also searched others questions; they said if I add
"filter.addDataScheme("file");"
I will get the signal, but I have tried and nothing was received.
Use UsbManager.ACTION_USB_DEVICE_ATTACHED and UsbManager.ACTION_USB_DEVICE_DETACHED for your intent filter.
To check for connection to PC use Intent.ACTION_BATTERY_CHANGED and onReceive intent.getInt(BatteryManager.EXTRA_PLUGGED) and see if the value is BatteryManager.BATTERY_PLUGGED_USB.
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();
}
}
};
If the phone has bluetooth toggled on, can an app read the list of the ids of nearby discoverable devices? If so, which function returns such a list?
Thanks!
Have a look here
// Create a BroadcastReceiver for ACTION_FOUND
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_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy