1

我正在开发一个 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
            }
        }
    };

请帮助我提供实现此目的的代码。

提前致谢。

4

0 回答 0