2

I need to continuously monitor the bluetooth devices which are present every 5 seconds. I have written the following piece of code which is not working for me.

private static final int DISCOVERY_REQUEST = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_connection);

    final TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText("");
    final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
            .getDefaultAdapter();
    mBluetoothAdapter.startDiscovery();
    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        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);
                tv.append(device.getName() + "-"
                        + /* device.getAddress()+ */"\n");
                tv.append("here");
                /*
                 * if (device.getName().equals("ONCEWASCUT-L7")) {
                 * tv.append("this is in the vicinity");
                 * 
                 * }
                 */
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
                    .equals(action)) {
                tv.append("\nEntered the Finished\n ");
                mBluetoothAdapter.startDiscovery();
            }
        }

    };
    String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
    startActivityForResult(new Intent(aDiscoverable), DISCOVERY_REQUEST);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mReceiver, filter);
    mBluetoothAdapter.startDiscovery();
}

What code needs to be added to this so that the aplication works fine.Note that I need to monitor the bluetooth devices every 5 seconds.

Dheeraj R
  • 701
  • 9
  • 17
  • Monitoring every 5 seconds is basically the same as always monitoring. A single scan to detect surrounding devices takes a few seconds, so you'll need to restart the scan immediately. – TJD Nov 08 '12 at 23:32
  • @TJD does this consume a lot of battery charge? – partho Jul 25 '22 at 08:55

2 Answers2

0

You can use the Timer and TimerTask for continuously checking that particular device is available or not.

For example:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask {
    @Override
    public void run () {
        //Here you can use handler or whatever you want to use.
    }
},delay, period);

delay - amount of time in milliseconds before first execution.

period - amount of time in milliseconds between subsequent executions.

http://developer.android.com/reference/java/util/Timer.html#scheduleAtFixedRate

Refer this link for more details.

Jon
  • 9,156
  • 9
  • 56
  • 73
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
0

declare Handler, Change time as per your requirement

mhandler = new Handler();
            Runnable mrunnable = new Runnable() {
                @Override
                public void run() {
                    if (mBluetoothAdapter.isDiscovering()) {
                        mBluetoothAdapter.cancelDiscovery();
                    }
                    mBluetoothAdapter.startDiscovery();
                    mhandler.postDelayed(this, 1000);
                }
            };
            mhandler.post(mrunnable);
Rex
  • 57
  • 8