RxJava中的Observable,在smartConfig中的应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RxJava中的Observable,在smartConfig中的应用相关的知识,希望对你有一定的参考价值。
Observable: 事件源
subscriber: 观察者
public void smartConfig(final String password){ Observable esptouch = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { EspWifiAdminSimple mWifiAdmin = new EspWifiAdminSimple(getActivity()); String apSsid = mWifiAdmin.getWifiConnectedSsid(); String apPassword = password; String apBssid = mWifiAdmin.getWifiConnectedBssid(); EsptouchTask esptouchTask = new EsptouchTask(apSsid, apBssid, apPassword, false, getActivity()); List<IEsptouchResult> resultList = esptouchTask.executeForResults(1); if (resultList.size() > 0 && resultList.get(0).isSuc()){ subscriber.onNext(getResources().getString(R.string.smart_config_register)); String mac = resultList.get(0).getBssid(); Log.e("felix", "mac:" + resultList.get(0).getBssid()); try { List<Device> devices = null; for (int i = 0; i < 10; i++){ Thread.sleep(1000); Response<List<Device>> r = RestApi.getRegisterDevices(mac).execute(); devices = r.body(); // mac 18fe34fdace7 //if (devices != null){ if(devices.size()>0){ break; }//18fe34d88345 mac adress 18fe34fda484 } if (devices == null){ subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_no_device_found))); return; } subscriber.onNext(getResources().getString(R.string.smart_config_register)); if(devices.size()>0){ for (Device device: devices){ //if(device.deviceMac.equals(mac)){ mDevice = device; subscriber.onCompleted(); return; //} } } subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_no_device_found))); }catch (Exception e){ subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_network_error))); } }else{ subscriber.onError(OnErrorThrowable.addValueAsLastCause(new Throwable(), getResources().getString(R.string.smart_config_fail))); } subscriber.onCompleted(); } }); esptouch.subscribeOn(Schedulers.io()) .observeOn(androidSchedulers.mainThread()) .subscribe(new Subscriber<String>() { @Override public void onCompleted() { // mProgress.setVisibility(View.GONE); // mCancel.setText(R.string.close); // mCancel.setEnabled(true); // Response<Device> d = RestApi.registerDevices(device.id, device).execute(); // subscriber.onNext(getResources().getString(R.string.smart_config_success)); // mDevice = d.body(); if (mDevice != null && mListener != null){ dismiss(); mListener.onDeviceDiscovered(mDevice); } } @Override public void onError(Throwable e) { String message = (String)((OnErrorThrowable.OnNextValue)e.getCause()).getValue(); mProgress.setVisibility(View.GONE); mCancel.setText(R.string.close); mCancel.setEnabled(true); mMessage.setText(message); } @Override public void onNext(String msg) { mMessage.setText(msg); } }); }
以上是关于RxJava中的Observable,在smartConfig中的应用的主要内容,如果未能解决你的问题,请参考以下文章
RxJava2 Observable中的异步任务状态&&取消等效项?
RxJava + Websocket - 如何将 Observable 添加到 Websocket 监听器?