java - How to increase the buffer size in Handler? -
i'm working on plotting graph on android app. want use buffer size equal 512 store incoming data arduino. after buffer full graph plotted , keep going. how can change code achieve this?
handler mhandler = new handler(){ @override public void handlemessage(message msg) { // todo auto-generated method stub super.handlemessage(msg); switch(msg.what){ case bluetooth.success_connect: bluetooth.connectedthread = new bluetooth.connectedthread((bluetoothsocket)msg.obj); toast.maketext(getapplicationcontext(), "connected!", 0).show(); string s = "successfully connected"; bluetooth.connectedthread.start(); break; case bluetooth.message_read: byte[] readbuf = (byte[]) msg.obj; string strincom = new string(readbuf, 0, 5); // create string bytes array log.d("strincom", strincom); if (strincom.indexof('.')==2 && strincom.indexof('s')==0){ strincom = strincom.replace("s", ""); if (isfloatnumber(strincom)){ series.appenddata(new graphviewdata(graph2lastxvalue,double.parsedouble(strincom)),autoscrollx); //x-axis control if (graph2lastxvalue >= xview && lock == true){ series.resetdata(new graphviewdata[] {}); graph2lastxvalue = 0; }else graph2lastxvalue += 0.1; if(lock == true) graphview.setviewport(0, xview); else graphview.setviewport(graph2lastxvalue-xview, xview); //refresh graphview.removeview(graphview); graphview.addview(graphview); } } break; } } public boolean isfloatnumber(string num){ //log.d("checkfloatnum", num); try{ double.parsedouble(num); } catch(numberformatexception nfe) { return false; } return true; } };
this bluetooth code:
public class bluetooth extends activity implements onitemclicklistener{ public static void disconnect(){ if (connectedthread != null) { connectedthread.cancel(); connectedthread = null; } } public static void gethandler(handler handler){//bluetooth handler mhandler = handler; } static handler mhandler = new handler(); static connectedthread connectedthread; public static final uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); protected static final int success_connect = 0; protected static final int message_read = 1; arrayadapter<string> listadapter; listview listview; static bluetoothadapter btadapter; set<bluetoothdevice> devicesarray; arraylist<string> paireddevices; arraylist<bluetoothdevice> devices; intentfilter filter; broadcastreceiver receiver; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_bluetooth); init(); if (btadapter==null){ toast.maketext(getapplicationcontext(), "no bluetooth detected", 0).show(); finish(); }else{ if (!btadapter.isenabled()){ turnonbt(); } getpaireddevices(); startdiscovery(); } } private void startdiscovery() { // todo auto-generated method stub btadapter.canceldiscovery(); btadapter.startdiscovery(); } private void turnonbt() { intent intent = new intent(bluetoothadapter.action_request_enable); startactivityforresult(intent, 1); } private void getpaireddevices() { devicesarray = btadapter.getbondeddevices(); if (devicesarray.size()>0){ for(bluetoothdevice device:devicesarray){ paireddevices.add(device.getname()); } } } private void init(){ listview = (listview)findviewbyid(r.id.listview); listview.setonitemclicklistener(this); listadapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1,0); listview.setadapter(listadapter); btadapter = bluetoothadapter.getdefaultadapter(); paireddevices = new arraylist<string>(); filter = new intentfilter(bluetoothdevice.action_found); devices = new arraylist<bluetoothdevice>(); receiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if (bluetoothdevice.action_found.equals(action)){ bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device); devices.add(device); string s = ""; for(int a=0;a<paireddevices.size();a++){ if (device.getname().equals(paireddevices.get(a))){ //append s = "(paired)"; break; } } listadapter.add(device.getname()+" "+s+" "+"\n"+device.getaddress()); }else if (bluetoothadapter.action_discovery_started.equals(action)){ }else if (bluetoothadapter.action_discovery_finished.equals(action)){ }else if (bluetoothadapter.action_state_changed.equals(action)){ if (btadapter.getstate() == btadapter.state_off){ turnonbt(); } } } }; registerreceiver(receiver, filter); intentfilter filter = new intentfilter(bluetoothadapter.action_discovery_started); registerreceiver(receiver, filter); filter = new intentfilter(bluetoothadapter.action_discovery_finished); registerreceiver(receiver, filter); filter = new intentfilter(bluetoothadapter.action_state_changed); } @override protected void onpause() { // todo auto-generated method stub super.onpause(); unregisterreceiver(receiver); } protected void onactivityresult(int requestcode, int resultcode, intent data){ super.onactivityresult(requestcode, resultcode, data); if (resultcode == result_canceled){ toast.maketext(getapplicationcontext(), "bluetooth must enabled continue", toast.length_short).show(); finish(); } } @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub if (btadapter.isdiscovering()){ btadapter.canceldiscovery(); } if (listadapter.getitem(arg2).contains("(paired)")){ bluetoothdevice selecteddevice = devices.get(arg2); connectthread connect = new connectthread(selecteddevice); connect.start(); }else { toast.maketext(getapplicationcontext(), "device not paired", 0).show(); } } private class connectthread extends thread { private final bluetoothsocket mmsocket; private final bluetoothdevice mmdevice; public connectthread(bluetoothdevice device) { // use temporary object later assigned mmsocket, // because mmsocket final bluetoothsocket tmp = null; mmdevice = device; // bluetoothsocket connect given bluetoothdevice try { // my_uuid app's uuid string, used server code tmp = device.createrfcommsockettoservicerecord(my_uuid); } catch (ioexception e) { } mmsocket = tmp; } public void run() { // cancel discovery because slow down connection btadapter.canceldiscovery(); try { // connect device through socket. block // until succeeds or throws exception mmsocket.connect(); //connectedthread = new connectedthread(mmsocket); } catch (ioexception connectexception) { // unable connect; close socket , out try { mmsocket.close(); } catch (ioexception closeexception) { } return; } // work manage connection (in separate thread) mhandler.obtainmessage(success_connect, mmsocket).sendtotarget(); } /** cancel in-progress connection, , close socket */ public void cancel() { try { mmsocket.close(); } catch (ioexception e) { } } } static class connectedthread extends thread { private final bluetoothsocket mmsocket; private final inputstream mminstream; private final outputstream mmoutstream; public connectedthread(bluetoothsocket socket) { mmsocket = socket; inputstream tmpin = null; outputstream tmpout = null; // input , output streams, using temp objects because // member streams final try { tmpin = socket.getinputstream(); tmpout = socket.getoutputstream(); } catch (ioexception e) { } mminstream = tmpin; mmoutstream = tmpout; } stringbuffer sbb = new stringbuffer(); public void run() { byte[] buffer; // buffer store stream int bytes; // bytes returned read() // keep listening inputstream until exception occurs while (true) { try { try { sleep(30); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } buffer = new byte[1024]; // read inputstream bytes = mminstream.read(buffer); // send obtained bytes ui activity mhandler.obtainmessage(message_read, bytes, -1, buffer).sendtotarget(); } catch (ioexception e) { break; } } } /* call main activity send data remote device */ public void write(string income) { try { mmoutstream.write(income.getbytes()); for(int i=0;i<income.getbytes().length;i++) log.v("outstream"+integer.tostring(i),character.tostring((char)(integer.parseint(byte.tostring(income.getbytes()[i]))))); try { thread.sleep(20); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (ioexception e) { } } /* call main activity shutdown connection */ public void cancel() { try { mmsocket.close(); } catch (ioexception e) { } } }
}
Comments
Post a Comment