multithreading - Android Shared Array with dynamic OBD commands -
in app established communication car via obd port , bluetooth. have sharedpreferences
contains preferences of commands shown set preference activity
.
i use shared array of commands can change content dynamically , send new arraylist
thread manage communication.
there way or use sharedpreferences
, when change content of array restart thread manage comunications devices.
i need can changed (add or remove commands arraylist
) , in same time notify thread send commands arraylist
has changed items.
method manage connections
private synchronized void manage() { log.d(tag, "connected, socket type:"); // cancel thread completed connection if (mconnectthread != null) { mconnectthread.cancel(); mconnectthread = null; } // cancel thread running connection if (mconnectedthread != null) { mconnectedthread.cancel(); mconnectedthread = null; } // cancel thread managing connections if (mmanagethread != null) { mmanagethread.cancel(); mmanagethread = null; } if (mmsocket != null && mmdevice != null) { // start thread manage connection , perform transmissions mmanagethread = new managedatathread(mmsocket); mmanagethread.start(); // send name of connected device ui activity message msg = mhandler.obtainmessage(constants.message_device_name); bundle bundle = new bundle(); bundle.putstring(constants.device_name, mmdevice.getname()); msg.setdata(bundle); mhandler.sendmessage(msg); // update ui title updateuserinterfacetitle(); } }
and thread mmanagethread manage connetions , message exchange
public class managedatathread extends thread { private final bluetoothsocket mmsocket; private final inputstream mminstream; private final outputstream mmoutstream; private boolean wait_response = false; private string typecommand; public managedatathread(bluetoothsocket socket) { log.d(tag, "create managedatathread: "); mmsocket = socket; inputstream tmpin = null; outputstream tmpout = null; // bluetoothsocket input , output streams try { tmpin = socket.getinputstream(); tmpout = socket.getoutputstream(); } catch (ioexception e) { log.e(tag, "temp sockets not created", e); } mminstream = tmpin; mmoutstream = tmpout; mstate = state_connected; } public void run() { obdcommand obc = new obdcommand(); while(cango) { (final string command : commandarray) { byte[] send = command.getbytes(); write(send); //setta la wait_response come true //mstate = state_wait_response; byte[] buffer = new byte[1024]; int bytes; // keep listening inputstream while connected while (wait_response) { try { // read inputstream bytes = mminstream.read(buffer); obc.readresult(mminstream); //formattedmessage = obc.getresult(); formattedmessage = obc.getcalculatedresult(); //ritorno la ripologia di comando typecommand = obc.getcommandtype(); //buffer = (byte) obc.getbuffer(); // send obtained bytes ui activity /*mhandler.obtainmessage(constants.message_read, bytes, -1, formattedmessage) .sendtotarget();*/ mhandler.obtainmessage(constants.message_read, bytes, -1, typecommand + "" + formattedmessage) .sendtotarget(); wait_response = false; } catch (ioexception e) { log.e(tag, "disconnected", e); connectionlost(); break; } } } try { managedatathread.sleep(400); } catch (interruptedexception e) { e.printstacktrace(); } } } /** * write connected outstream. * * @param buffer bytes write */ public void write(byte[] buffer) { try { mmoutstream.write(buffer); // share sent message ui activity mhandler.obtainmessage(constants.message_write, -1, -1, buffer) .sendtotarget(); wait_response = true; } catch (ioexception e) { log.e(tag, "exception during write", e); cango = false; } } public void cancel() { try { cango = false; mmsocket.close(); } catch (ioexception e) { log.e(tag, "close() of connect socket failed", e); } } }
here can't use method check array changed , change command list in thread? way stop , restart method?
and here call in mainactivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar mytoolbar = (toolbar) findviewbyid(r.id.my_toolbar); setsupportactionbar(mytoolbar); rpmtxt = (textview) findviewbyid(r.id.rpmtext); speedtxt = (textview) findviewbyid(r.id.speedtext); coolanttxt = (textview) findviewbyid(r.id.colanttemptext); carbtxt = (textview) findviewbyid(r.id.carbtypetext); nodata = (textview) findviewbyid(r.id.nodatatxt); canerror = (textview) findviewbyid(r.id.canerrortxt); /** * sharedpreferences per gestire quali cose mostrare nell'activity */ showusersettings(); /*check bluetooth sensor*/ mbluetoothadapter = bluetoothadapter.getdefaultadapter(); if (mbluetoothadapter == null) { // device not support bluetooth toast.maketext(getapplicationcontext(), "il telefono non supporta il bluetooth", toast.length_long).show(); } // if bt not on, request enabled. // setupchat() called during onactivityresult if (!mbluetoothadapter.isenabled()) { intent enableintent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(enableintent, request_enable_bt); // otherwise, setup chat session } else if (mbluetoothservice == null) { setupchat(); } }
and setupchat() method call threads in mybluetoothservice.java
/** * set ui , background operations chat. */ private void setupchat() { log.d(tag, "setupchat()"); // initialize bluetoothchatservice perform bluetooth connections mbluetoothservice = new mybluetoothservice(mainactivity.this, mhandler, commandarray); // initialize buffer outgoing messages //moutstringbuffer = new stringbuffer(""); }
that send commandarray read sharedpreferences class start threads
Comments
Post a Comment