-1

I would like to make my BLE scan list to update if I turn off one of the devices.

For instance :

  1. devices turning on
    1. Apps detect 3 devices via BLE at the first time
    2. Turn off one device
    3. 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);
    }
};
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141

1 Answers1

0

In order to achieve your goal, you have to restart scanning after you turn off the device. The experiments I have done show that I have to follow the procedure below to update the result of scanning:
Start scanning → stop scanning → start scanning → stop scanning …

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
cq674350529
  • 118
  • 9