i creating app android-arduino bluetooth serial communication. able connect arduino successfully. app can send data arduino without hassle , have verified it. while receiving data arduino, app receives part of data being send. example if "404" being send arduino, app show "4" being received.
i checked other such apps , other apps able receive "404" itself. problem code.
this code read data arduino:
public string read(byte[] bytes){ try { minput.read(bytes); strinput = new string(bytes); }catch(exception e){ e.printstacktrace(); } return strinput; } //minput input stream of bluetooth connection as can see data recived byte buffer , converted string using new string(bytes); method. how ever when toast string, 4 being toasted instead of 404 send arduino.
the byte buffer of size 256.
edit: requested full code bluetoothmanager.java this:
public class bluetoothmanager { private bluetoothadapter bluetoothadapter; private bluetoothdevice bluetoothdevice; private bluetoothsocket bluetoothsocket; private connectedthread connectedthread; private byte[] buffer; public bluetoothmanager(){ buffer=new byte[256]; bluetoothsocket=null; bluetoothadapter=null; bluetoothdevice=null; connectedthread=null; getbluetoothadapter(); if(!isbluetoothavailable()){ turnbluetoothon(); } scantoconnect(); } public void turnbluetoothoff(){ try { bluetoothsocket.close(); bluetoothsocket=null; bluetoothadapter.canceldiscovery(); bluetoothadapter.disable(); bluetoothadapter=null; bluetoothdevice=null; }catch(exception e){ e.printstacktrace(); } } private boolean isbluetoothavailable(){ return bluetoothadapter.isenabled(); } private void turnbluetoothon(){ bluetoothadapter.enable(); } public string readdata(context context){ string outputstring=null; if(isbluetoothavailable()) { outputstring = connectedthread.read(buffer); }else{ toast.maketext(context, "error: not connected", toast.length_long).show(); } return outputstring; } public void writedata(string string, context context){ if(isbluetoothavailable()) { connectedthread.write(string.getbytes()); }else{ toast.maketext(context, "error: not connected", toast.length_long).show(); } } private void getbluetoothadapter(){ try{ bluetoothadapter=bluetoothadapter.getdefaultadapter(); }catch (exception e){ e.printstacktrace(); } } private void scantoconnect(){ set<bluetoothdevice> paireddevices=bluetoothadapter.getbondeddevices(); if(paireddevices.size()>0){ try { (bluetoothdevice device : paireddevices) { if (device.getname().equals("hc-05")) { bluetoothdevice = device; new connectbt(bluetoothdevice); break; } } }catch(exception e){ e.printstacktrace(); } } } private class connectbt extends thread { public connectbt(bluetoothdevice device) { bluetoothsocket tmp = null; bluetoothdevice = device; uuid uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); try { tmp = device.createrfcommsockettoservicerecord(uuid); } catch (ioexception e) { e.printstacktrace(); } bluetoothsocket = tmp; run(); } public void run() { bluetoothadapter.canceldiscovery(); try { bluetoothsocket.connect(); connectedthread = new connectedthread(bluetoothsocket); } catch (ioexception connectexception) { closesocket(); } } private void closesocket() { try { bluetoothsocket.close(); } catch (ioexception e) { e.printstacktrace(); } } } private class connectedthread extends thread{ private inputstream minput=null; private outputstream moutput=null; private string strinput; public connectedthread(bluetoothsocket socket){ bluetoothsocket=socket; inputstream tmpin=null; outputstream tmpout=null; try{ tmpin=socket.getinputstream(); tmpout=socket.getoutputstream(); }catch(ioexception e){ e.printstacktrace(); closesocket(); } minput=tmpin; moutput=tmpout; } public void write(byte[] bytes){ try{ moutput.write(bytes); }catch(ioexception e){ e.printstacktrace(); } } public string read(byte[] bytes){ try { minput.read(bytes); strinput = new string(bytes); }catch(exception e){ e.printstacktrace(); } return strinput; } public void closesocket(){ try{ bluetoothsocket.close(); }catch(ioexception e){ e.printstacktrace(); } } } } edit-2: on further debugging found out minput.available() returns 0 while minput.read(bytes) returns 1.why behavior while in arduino code using bluetooth.println("404");
http://felhr85.net/2014/11/11/usbserial-a-serial-port-driver-library-for-android-v2-0/
try should work. please provide full code of it. may error should in part connecting function serial event handler.
Comments
Post a Comment