how I'm building application using android studio that will try to connect (on button clicked) to specific device- bluetooth module HC-05 v2. I wrote 'trying' because HC-05 modeule will be able to connect only to 1 device.
If first device is being handled, the second one in queue will try to connect as long as it will get connected. So what i'm gonna do is- on button clicked 'CONNECT' phone will be trying to connect with hc-05 module (searching by name ?), if it finds it- connects to it. After 15 seconds will automaticly disconnect and what goes after- second device in queue will be connected for 15 seconds, and so on.
I made some steps to enable/ disable bluetooth using app, but have no idea how to set automatic connecetion (phone->BT module) ONLY for 15 seconds.
Here is my code:
BluetoothAdapter bt;
private final String TAG="MainActivity";
Button button=(Button) findViewById(R.id.button);
Button polacz=(Button) findViewById(R.id.polacz);
public void enableDisableBT() {
if (bt == null) {
Log.d(TAG, "enableDisableBT: Brak mozliwosci polaczenia.");
}
if (!bt.isEnabled()) {
Log.d(TAG, "enableDisableBT: włączanie BT'ka");
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBTIntent);
IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(receiver, BTIntent);
}
if (bt.isEnabled()) {
Log.d(TAG, "enableDisableBT: wyłączanie BT'ka");
bt.disable();
IntentFilter BTIntent = new IntentFilter((BluetoothAdapter.ACTION_STATE_CHANGED));
registerReceiver(receiver, BTIntent);
}
}