java - Android & Bluetooth & Arduino -
i trying display sensor data on android phone (target 4.3) received transmitting arduino type device. transmission takes place via bluetooth. able connect arduino type device , share data, reason having synchronization issues.
the way arduino setup right now, after successful connection waits byte received phone (unsigned byte value 255), when receives byte responds sending packet (3 bytes) containing information 3 different sensors i.e.
packet: byte 1: temperature data byte 2: cadence data byte 3: speed data
all have display data(which updating live) repeatedly until user terminates connection on android phone.
here code feel making minor error somewhere in logic.
messagehandler
handler mhandler = new handler(){ public void handlemessage(message msg){ super.handlemessage(msg); switch(msg.what){ case success_connect: // something; connectedthread connectedthread = new connectedthread((bluetoothsocket)msg.obj); toast.maketext(getactivity(),"connected",toast.length_short).show(); /* * send test string here */ /* * string connect_string = "test"; * connectedthread.write(connect_string.getbytes()); */ connectedthread.start(); break; case message_read: byte[] readbuf = (byte[])msg.obj; int tempint = bytetoint(readbuf[0]); int cadenceint = bytetoint(readbuf[1]); int speedint = bytetoint(readbuf[2]); edittext temperaturedata = (edittext)getactivity().findviewbyid(r.id.temperaturedata); temperaturedata.settext(integer.tostring(tempint)); edittext cadencedata = (edittext)getactivity().findviewbyid(r.id.cadence); cadencedata.settext(integer.tostring(cadenceint)); edittext speeddata = (edittext)getactivity().findviewbyid(r.id.speed_data); speeddata.settext(integer.tostring(speedint)); } } };
connectthread
public 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 mbluetoothadapter.canceldiscovery(); try { // connect device through socket. block // until succeeds or throws exception mmsocket.connect(); } catch (ioexception connectexception) { // unable connect; close socket , out try { mmsocket.close(); } catch (ioexception closeexception) { toast.maketext(getactivity(), "connecting device failed!", toast.length_long).show(); } 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) { } } }
connectedthread
private 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; } public void run() { byte[] buffer; // buffer store stream int bytes; // bytes returned read() // keep listening inputstream until exception occurs while (true) { try { // read inputstream buffer = new byte[3]; byte maxbyte = (byte) 1; mmoutstream.write(255); bytes = mminstream.read(buffer,0,buffer.length); // send obtained bytes message handler mhandler.obtainmessage(message_read,buffer).sendtotarget(); } catch (ioexception e) { break; } } } /* call main activity send data remote device */ public void write(byte[] bytes) { try { mmoutstream.write(bytes); } catch (ioexception e) { } } }
byte int method
public static int bytetoint(byte b){ int value; value = b & 0xff; return value; }
the data receive being displayed ends wrong because byte array sequence off causes wrong values displayed. have been trying figure out while , input helpful.
check if can send in message handler cloned array. should "buffer.clone" or "buffer.clone()" instead of "buffer". if so, means not-cloned buffer copied reference handler. while handler doing stuff, connected thread may redefine array , reassign different values. test can define buffer ad byte[] instead of byte[]. in way fixed similar problem in application.
Comments
Post a Comment