I am writing Android Application on finding and detecting IBeacons(These are BLE devices) and ranging them(depending on RSSI value) I use Sample code from https://developer.android.com/guide/topics/connectivity/bluetooth-le.html
But this Code works different on my android devices (Samsung Galaxy S3 and LG G3).
On my S3 the "onLeScan" callback rises many times in loop (about 5 per second) and give me different RSSI values every time depending on the range.
But on my LG G3 the "onLeScan" callback rises only once, when i start Scanning. so if i want to obtain new RSSI values, i need to restart scanning. And i think it is not very good.
I don't know whether it is something wrong with LG G3 driver, or i must check some android settings. Can any one tell me something about it?
Here is my Code:
public class Main2Activity extends Activity implements BluetoothAdapter.LeScanCallback {
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
/**/
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
enableBtIntent.addFlags(enableBtIntent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(enableBtIntent);
}
scanLeDevice(true);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(Main2Activity.this);
}
}, 30000);
mScanning = true;
mBluetoothAdapter.startLeScan(Main2Activity.this);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(Main2Activity.this);
}
}
ArrayList<String> datas = new ArrayList<String>();
@Override
public void onLeScan(BluetoothDevice arg0, int arg1, byte[] arg2) {
// TODO Auto-generated method stub
datas.add( arg2.toString() );
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}