I would like to make my BLE scan list to update if I turn off one of the devices.
For instance :
- devices turning on
- Apps detect 3 devices via BLE at the first time
- Turn off one device
- Apps can detect 2 devices via BLE
When it comes to the implementation, Apps cannot get the updated list , still detects 3 devices at the first time. Would you please tell me how to modify the handler so that the LeDeviceListAdapter can clear and update if procedure 3 is done and goes 4 ?
The below is my code
OnCreate:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
private boolean useDiceOne;
@Override
public void run() {
if(deviceFound){
System.out.println("2");
iv.setImageResource(R.drawable.pairing_d);
mLeDeviceListAdapter.notifyDataSetChanged();
if(screenHeight < 1800){
if(mLeDeviceListAdapter.getCount() > 2 ){
iv.setImageResource(R.drawable.pairing_d2);
}
}else{
if(mLeDeviceListAdapter.getCount() > 3 ){
iv.setImageResource(R.drawable.pairing_d2);
}
}
}else{
if (!useDiceOne) {
iv.setImageResource(R.drawable.pairing_b);
} else {
iv.setImageResource(R.drawable.pairing_bn);
}
useDiceOne = !useDiceOne;
handler.postDelayed(this, 400);
}
}
} , 50);
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private void scanLeDevice(final boolean enable) {
if (enable) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, 10000);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
runScan= new Runnable()
{
public void run()
{
reverseByte(scanRecord);
byte [] serviceBytes = new byte[16];
System.arraycopy(scanRecord, 41, serviceBytes, 0, 16);
if(checkService(asd , serviceBytes)){
iv.setImageResource(R.drawable.pairing_d);
deviceFound = true;
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
if(screenHeight < 1800){
if(mLeDeviceListAdapter.getCount() > 2 ){
System.out.println("z5");
lblCalibrate.setText(resultUiString.get(16));
iv.setImageResource(R.drawable.pairing_d2);
}
}else{
if(mLeDeviceListAdapter.getCount() > 3 ){
System.out.println("z5");
lblCalibrate.setText(resultUiString.get(16));
iv.setImageResource(R.drawable.pairing_d2);
}
}
}else{
}
}
};
handler.postDelayed(runScan, 500);
}
};