vindy
vindy

Reputation: 23

How to prevent duplicate eddystone scan result

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.previously i have post this and didn't get a proper answer.

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 beacon result

Upvotes: 1

Views: 239

Answers (1)

davidgyoung
davidgyoung

Reputation: 64951

You need to keep a unique list of detected devices. In Java, this is often done by using a HashMap instead of an ArrayList. The key of the HashMap is the field by which you want to recognize each item as being distinct. In your case, you might use the Eddystone URL or perhaps the bluetooth device mac address as the key, so you know that it is a unique beacon.

Here are the changes you would make to do what is described above, keeping entries unique by beacon bluetooth MAC address:

txpowerMap= new HashMap<String,String>();
urlMap=new HashMap<String,String>();
...

txpowerMap.put(device.getAddress(), txpower);
urlMap.put(device.getAddress(), "" + clickUrl);

You will also have to update your view logic so it works with the HashMap containers instead of the ArrayList. You can convert the values of the HashMap into an ArrayList like this:

ArrayList<String> txPowerArray = new ArrayList<String>(txpowerMap.values());

Upvotes: 0

Related Questions