문제

I want to be able to walk around a room with an android device in my hand, click a button that says room 1 and then start discovering bluetooth devices around me and getting their RSSI values. I then want to save this information as a text file. Then repeat for different rooms.

That's the easy part.

The part I'm having trouble with is finding out how to (or if it's possible) to do this but have a timer set so that the discovery process doesn't necessarily finish itself, but so I can time it to start and stop the discovery repeatedly by itself to get new values until I pess finish. Most of the values that are close by USUALLY appear within about 5 seconds or so, which I'm happy enough with for what I'm trying to do at the moment. I've found this question but there's no answer.

To make it a bit clearer a simple example would be:

  • Press "Room1" button
  • Start Discovery
  • After 5 seconds, stop discovery and start again
  • Do this over and over until...
  • "Stop" button pressed

Does anyone know if this is possible? And if so what would be the best way? I've looked at Timer and TimerTask and they look useful.

Also I'm asking a bit before I start getting too deep into it as I've done that before n had to revert back to the start. :(

Thanks

도움이 되었습니까?

해결책

You could use something like this:

private void start() {
    mBluetoothAdapter.startDiscovery();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mBluetoothAdapter.isDiscovering())
                mBluetoothAdapter.cancelDiscovery();
            if (repeat) start();                
        } }, 5000);
}

Make a variable repeat that is set to false by the stop button. The start button sets repeat to true and calls start().

As far as I know the bluetooth discovery runs indefinitely so you don't need to stop and restart it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top