Reputation: 1148
I am developing an Android app that scans for BLE devices periodically, and use the beacon information for further processes. I need to catch the Beacon which is closest.
// Scan for bluetooth devices and parse results
private void scanLeDevice() {
// Stops scanning after a pre-defined scan period.
if(!mScanning){
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "stop scanning");
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
Log.v(TAG, "closing scan. beacon found:" + beaconFound);
broadcastLocalUpdate();
beaconFound = false;
stopSelf();
}
}, SCAN_PERIOD);
proximity=null;
totalDevices=0;
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
Log.d(TAG, "start scanning");
}
}
The beacons I use:
On one of my test devices, the Sony Xperia z2, this works. I get a long list of devices with Beacon information and RSSI values. each Beacon is discovered multiple times each scan.
On my other test device, the Motorolla Moto G(2nd generation), this scanning behaves WILDLY UNPREDICTABLE. On most scans only 2 or 3 devices are discovered. Other occasions it discovers about 10 devices (includes duplicates). It does not matter whether these devices are close or far.
Upvotes: 2
Views: 419
Reputation: 40357
Your beacons are not advertising frequently enough, creating a very real chance that no transmission occurs while the receiver is monitoring on its frequency. Try for at least 10 transmissions a second, but preferably more if you have an external power source.
Of course it is not all in the transmitter - Android phones vary drastically in how much they receive to detect BLE advertisements, and may report drastically different signal levels under the same conditions.
You may want to collect results over several scans in a short period, and look at the aggregate data, not just that of a single scan.
Upvotes: 1
Reputation: 1312
The scan result is for each received adv. No matter what rssi. The antenna is a shared resource on most phones. Wifi, bt classic, ble. So the phone only listens for adv in windows. Only if the adv falls into this window will get callback. The phone must listen on 3 channels. So try playing with your adv interval and adv package size. If you have many in the same room you should not use too fast adv interval as the adv channels will be filled and collisions will occur. Try turning off wifi and retest. Avoid having classic bt connection. Put name in scanresponse if you can to keep adv package short.
Upvotes: 0