Reputation: 23
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);
Upvotes: 1
Views: 168
Reputation: 3235
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.)
Upvotes: 0