1

I'm trying to display all the available Eddystone beacons in my app.I have two eddystone for testing this app.when i open app,It's scann beacons and display duplicated values like attached image..i want to show both beacons sametime when i open the app (-57 beacon and -69 beacon ).I'm using below code.

i have initialize these Arraylist on top

    txpowerArray= new ArrayList<String>();
    urlArray=new ArrayList<String>();

    private BluetoothAdapter.LeScanCallback mLeScanCallback =new BluetoothAdapter.LeScanCallback() {
               @Override
               public void onLeScan(final BluetoothDevice device,final int rssi,final byte[] scanRecord)
               {
                     new Thread()
                       {
                           public void run()
                           {
                               RangingActivity.this.runOnUiThread(new Runnable()
                               {
                                   public void run()
                                   {
                                    connect(rssi, scanRecord,device);

                                   }
                               });
                           }
                       }.start();

               }

           };


     public void connect(int rssi, byte[] scanRecord,BluetoothDevice device){

            List<ADStructure> structures =
                    ADPayloadParser.getInstance().parse(scanRecord);
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
           for (ADStructure structure : structures)
            {

                if (structure instanceof EddystoneURL)
                {

                    EddystoneURL es = (EddystoneURL)structure;

                    Log.d("Eddy", "Tx Power = " + es.getTxPower());
                    Log.d("Eddy", "URL = " + es.getURL() );

                        clickUrl=es.getURL().toString();
                        txpower=String.valueOf(es.getTxPower());


                    txpowerArray.add(txpower);
                    urlArray.add("" + clickUrl);

                    Log.d("devicelist", " "+url+" "+txpower);

                        mBluetoothAdapter.stopLeScan(mLeScanCallback);

                }else {

                 }

            }
        }
    }, 4000);

Duplicate beacons

vindy
  • 23
  • 7

1 Answers1

0

Basically your code stops the scanning as soon as the first beacon is found. Finding the first beacon triggers onLeScan() which calls connect() which calls stopLeScan().

There's some other code before stopLeScan() so there's the chance (in a small time window) that the other beacon is also sometimes found and onLeScan() gets called again, but it's only a slim chance.

You should keep scanning for a while and only stop the scanning based on a timer. Then both (all) beacons have a chance of being found during the scan.

So find some other place for this:

mBluetoothAdapter.stopLeScan(mLeScanCallback);

The code could be something like:

// Stops scanning after SCAN_DURATION milliseconds.
mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        mBluetoothAdapter.stopLeScan(mScanCallback);
    }
}, SCAN_DURATION);

// Starts the scanning
mBluetoothAdapter.startLeScan(mScanCallback);

(Also starting a new thread only to call runOnUiThread() seems counterintuitive.)

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • In your way scan only one beacon.then I have add handler to scanning for some time.then it display duplicate beacons.how to prevent that?i have edit my question – vindy Oct 14 '16 at 05:01
  • Yes, the beacons may appear several times during a scan cycle if their advertising cycle is frequent enough compared to the scan cycle. So you should identify each beacon (maybe with the Eddystone-UID/Eddystone-URL or with `device.getAddress()`) and add them to your list only once. You could keep a list of recently seen beacons and avoid adding duplicates. Just update any data if the beacon already exists in the list. I don't know if the [answer about scanning iBeacons](http://stackoverflow.com/questions/32952919/android-how-do-i-set-uuid-beacon-value-to-proper-textview/32986093) is helpful. – Markus Kauppinen Oct 15 '16 at 09:26