每次以不同的间隔重试

时间:2018-11-01 11:04:28

标签: java android algorithm retry-logic

我正在开发ble-android通信应用程序。当我尝试从ble设备读取值时,有时会出现TYPE_GATT_ERROR。因此,我希望每次看到TYPE_GATT_ERROR时重试扫描并读取值。第一次发生扫描时,我想在30秒后再次开始扫描,如果这段时间如果代码也到达TYPE_GATT_ERROR段,则应在1分钟后再次开始扫描,如果到达错误段则应第三次再次扫描,并应随时间重新扫描间隔2分钟。我该如何使用下面的代码段呢?我想实现重试算法的地方是TYPE_GATT_ERROR条件的Elseif情况。

private final BroadcastReceiver bleServiceReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent){
            String type = intent.getExtras().getString(
                    BleReceivedService.EXTRA_TYPE);
            if (BleReceivedService.TYPE_GATT_CONNECTED.equals(type)){
                Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_CONNECTED");
                if (BleReceivedService.getGatt() != null){
                    BleReceivedService.getGatt().discoverServices();
                    Log.d(TAG, " Connect Device");
                    stopScan();
                }
            } else if (BleReceivedService.TYPE_GATT_DISCONNECTED.equals(type)){
                Log.d(TAG, " MYTAG Disconnect Device");
                Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_DISCONNECTED");
                BleReceivedService.getInstance().disconnectDevice();
          if(isScanning) {
             Log.d("ScanCheck","Scanning after disconnection");
          stopScan();
          }
            } else if (BleReceivedService.TYPE_GATT_ERROR.equals(type)) {
                Log.d(TAG, " MYTAG DashBoard onReceive TYPE_GATT_ERROR");
//here i want to call startscan() method in the above described fashion
            } else {
                //do someting
            }
        }
    };

请帮助我完成代码。

谢谢。

0 个答案:

没有答案