multithreading - Android -(Thread) app will hang when I run a new thread -
i have 3 activities in android application. in first activity, on click of bluetooth device list of paired devices, i'm starting service keep bluetooth connection visible actives. in service class i'm reading data continuously bluetooth device , i'm binding second activity service class read data received.
i'm not able instance of binder outside onserviceconnected() method of service connection method. i'm calling user-defined thread onserviceconnected() method. in way i'm getting values continuously service class. app not respond after few seconds of successful execution.
it blocking main thread think. i'm not getting need modify code. code below second activity(mainactivity). "bluetoothmanager" service class. need similar task in third activity also.
i'm not getting whether problem binding or thread. need call thread outside of service connection class. if so, i'll null pointer exception. i'm calling thread onserviceconnected() function binder object not null. have use boolean misbound while loop. true. please me. i'm new android.
bluetoothmanager.class public class bluetoothmanager extends service{ final int handlerstate = 0; // used identify handler message private bluetoothadapter btadapter = null; private bluetoothsocket btsocket = null; private stringbuilder recdatastring = new stringbuilder(); public connectedthread mconnectedthread; static handler bluetoothin; int bp; string sensor0,sensor1; static handler mhandler; // spp uuid service - should work devices private static final uuid btmoduleuuid = uuid .fromstring("00001101-0000-1000-8000-00805f9b34fb"); ibinder mbinder = new localbinder(); @override public ibinder onbind(intent intent) { return mbinder; } public class localbinder extends binder { bluetoothmanager getservice() { return bluetoothmanager.this; } } @override public void oncreate() { /// toast.maketext(this, " myservice created ", toast.length_long).show(); // flag="created"; } private bluetoothsocket createbluetoothsocket(bluetoothdevice device) throws ioexception { return device.createrfcommsockettoservicerecord(btmoduleuuid); // creates secure outgoing connecetion bt device using uuid } public string getbpm(){ return sensor1; } @override public int onstartcommand(intent intent, int flags, int startid) { toast.maketext(this, " myservice started", toast.length_long).show(); final string address=intent.getstringextra("address"); final int currentid = startid; if(address!=null) { btadapter = bluetoothadapter.getdefaultadapter(); bluetoothdevice device = btadapter.getremotedevice(address); try { btsocket = createbluetoothsocket(device); } catch (ioexception e) { toast.maketext(getbasecontext(), "socket creation failed", toast.length_long).show(); } // establish bluetooth socket connection. try { btsocket.connect(); } catch (ioexception e) { try { btsocket.close(); } catch (ioexception e2) { // insert code deal } } mconnectedthread = new connectedthread(btsocket); mconnectedthread.start(); // send character when resuming.beginning transmission check // device connected // if not exception thrown in write method , // finish() called mconnectedthread.write("x"); } bluetoothin = new handler() { public void handlemessage(android.os.message msg) { if (msg.what == handlerstate) { // if message want string readmessage = (string) msg.obj; // msg.arg1 = bytes // connect // thread recdatastring.append(readmessage); // keep appending // string until ~ int endoflineindex = recdatastring.indexof("~"); // determine // // end-of-line if (endoflineindex > 0) { // make sure there data before ~ string datainprint = recdatastring.substring(0, endoflineindex); // extract string //txtstring.settext("data received = " + datainprint); /*int datalength = */datainprint.length(); // length of // data received /*txtstringlength.settext("string length = " + string.valueof(datalength));*/ if (recdatastring.charat(0) == '#') // if starts // # know // // looking { sensor0 = recdatastring.substring(1,3); // sensor1=sensor0; log.d("bpm", sensor0); } recdatastring.delete(0, recdatastring.length()); // clear // // string // data // strincom =" "; datainprint = " "; } } } }; // bluetooth // adapter return currentid; } private class connectedthread extends thread { private final inputstream mminstream; private final outputstream mmoutstream; // creation of connect thread public connectedthread(bluetoothsocket socket) { inputstream tmpin = null; outputstream tmpout = null; try { // create i/o streams connection tmpin = socket.getinputstream(); tmpout = socket.getoutputstream(); } catch (ioexception e) { } mminstream = tmpin; mmoutstream = tmpout; } public void run() { byte[] buffer = new byte[256]; int bytes; // keep looping listen received messages while (true) { try { bytes = mminstream.read(buffer); // read bytes input // buffer string readmessage = new string(buffer, 0, bytes); // send obtained bytes ui activity via handler bluetoothin.obtainmessage(handlerstate, bytes, -1, readmessage).sendtotarget(); } catch (ioexception e) { break; } } } // write method public void write(string input) { byte[] msgbuffer = input.getbytes(); // converts entered string // bytes try { mmoutstream.write(msgbuffer); // write bytes on bt connection // via outstream } catch (ioexception e) { // if cannot write, close application toast.maketext(getbasecontext(), "connection failure", toast.length_long).show(); } } } @override public void onrebind(intent intent) { log.v("myservice", "in onrebind"); super.onrebind(intent); } @override public boolean onunbind(intent intent) { log.v("myapp", "in onunbind"); return true; } @override public void ondestroy() { super.ondestroy(); log.v("myservice", "in ondestroy"); } } mainactivity.java
public class mainactivity extends activity { private serviceconnection mconnection; textview sensorview0; boolean misbound; bluetoothmanager bm; private handler bpmhandler; private serviceconnection mconnection; final int handlerstate = 0; // used identify handler message @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sensorview0 = (textview) findviewbyid(r.id.bpm); bpmhandler=new handler(){ public void handlemessage(android.os.message msg) { if (msg.what == handlerstate) { string s=(string)msg.obj; sensorview0.settext("bpm="+s); } } }; } @override public void onresume() { super.onresume(); mconnection= new serviceconnection() { @override public void onservicedisconnected(componentname name) { misbound = false; bm=null; } @override public void onserviceconnected(componentname name, ibinder service) { localbinder mybinder = (localbinder)service; misbound = true; bm=mybinder.getservice(); mconnectedservice=new connectedservice(misbound); mconnectedservice.start(); } }; intent intent = new intent(this, bluetoothmanager.class); bindservice(intent, mconnection, context.bind_auto_create); } private class connectedservice extends thread { final boolean bound; public connectedservice(boolean misbound){ bound =misbound; } public void run() { string s; while (bound) { s= bm.getbpm(); message msg = new message(); msg.what =handlerstate ; msg.obj=s; mainactivity.this.bpmhandler.sendmessage(msg); } } }; @override public void onpause() { super.onpause(); unbindservice(mconnection); misbound = false; } }
i feel connectedservice thread code causing issue. instead of continuously racing grtbpm method, why don't post message when there change. can use local broadcast manager broadcast message service , catch in activity , update ui accordingly. connectedservice thread runs continuously , keep posting message handler causing load on main thread.
Comments
Post a Comment