首页 > 代码库 > RxJava中的Observable,在smartConfig中的应用
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 one rror(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中的应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。