0

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);
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pprunner
  • 95
  • 7

1 Answers1

0

I did not quite understand the whole point of your application, but in any case, to connect to any particular device, you need to know either its name or the mac address or a specific service or characteristic. So you need or to add scanning screen where user will be able to choose needed devices or to hardcode one of the devices parameters. And when you will have list of needed devices you can scan for them.

 new ScanCallback() {
                @Override
                public void onScanResult(int callbackType, ScanResult result) {
                   result.getDevice().getAddress()// get device MAC
                   result.getDevice()// get device
                }
 }

Then connect to device and disconnect after specific time. All other staff you can implement according to your needed. For example you can save already connected devices to list to avoid reconnection and so forth

You can read more here: https://medium.com/@avigezerit/bluetooth-low-energy-on-android-22bc7310387a

  • I hade to make application to my project that connects to specific device 'on click' and disconnects after few seconds. I connected it by MAC addres and UUID, i unfortunately didn't managed to disconnect after 15 seconds – pprunner Feb 13 '20 at 16:16
  • What is your problem? You don't know how to disconnect? Could you please describe it more details so I'll be able to help – Ivan Ardelian Feb 14 '20 at 12:19