Observable: Event Source
Subscriber: Viewer
Public voidSmartconfig (FinalString Password) {Observable Esptouch= Observable.create (NewObservable.onsubscribe<string>() {@Override Public voidCall (SUBSCRIBER<?SuperString>subscriber) {espwifiadminsimple Mwifiadmin=Newespwifiadminsimple (Getactivity ()); String Apssid=Mwifiadmin.getwificonnectedssid (); String Appassword=password; String Apbssid=Mwifiadmin.getwificonnectedbssid (); Esptouchtask Esptouchtask=NewEsptouchtask (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(inti = 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 (Newthrowable (), 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.ADDV Alueaslastcause (Newthrowable (), Getresources (). getString (R.string.smart_no_device_found)); }Catch(Exception e) {subscriber.onerror (Onerrorthrowable.addvalueaslastcause (Newthrowable (), Getresources (). getString (R.string.smart_network_error)); } }Else{subscriber.onerror (Onerrorthrowable.addvalueaslastcause (Newthrowable (), Getresources (). getString (R.string.smart_config_fail)); } subscriber.oncompleted (); } }); Esptouch.subscribeon (Schedulers.io ()). Observeon (Androidschedulers.mainthread ()). Subscribe ( NewSubscriber<string>() {@Override Public voidoncompleted () {//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 voidOnError (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 voidOnNext (String msg) {mmessage.settext (msg); } }); }
The application of observable in Rxjava in Smartconfig