Wednesday, 15 June 2011

java - Android - Error while reading data from Bluetooth -



java - Android - Error while reading data from Bluetooth -

i have android application sends , retreives info to/from arduino device.

package com.arduino.arduinoled1; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.uuid; import android.app.activity; import android.bluetooth.bluetoothadapter; import android.bluetooth.bluetoothdevice; import android.bluetooth.bluetoothsocket; import android.content.intent; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private static final string tag = "abdel-domotic"; button ledon, ledoff; button gateon, gateoff; button curton, curtoff; button gettemp; textview temp; outputstream mmoutputstream; inputstream mminputstream; thread workerthread; byte[] readbuffer; int readbufferposition; int counter; volatile boolean stopworker; private static final int request_enable_bt = 1; private bluetoothadapter btadapter = null; private bluetoothsocket btsocket = null; private outputstream outstream = null; // known spp uuid private static final uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb"); // insert server's mac address private static string address = "20:13:12:05:10:24"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d(tag, "in oncreate()"); setcontentview(r.layout.activity_main); ledon = (button) findviewbyid(r.id.ledon); ledoff = (button) findviewbyid(r.id.ledoff); gateon = (button) findviewbyid(r.id.gateon); gateoff = (button) findviewbyid(r.id.gateoff); curton = (button) findviewbyid(r.id.curton); curtoff = (button) findviewbyid(r.id.curtoff); gettemp = (button) findviewbyid(r.id.gettemp); btadapter = bluetoothadapter.getdefaultadapter(); checkbtstate(); ledon.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("1"); toast msg = toast.maketext(getbasecontext(), "you have clicked led on", toast.length_short); msg.show(); } }); ledoff.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("0"); toast msg = toast.maketext(getbasecontext(), "you have clicked led off", toast.length_short); msg.show(); } }); gateon.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("3"); toast msg = toast.maketext(getbasecontext(), "you have clicked gate on", toast.length_short); msg.show(); } }); gateoff.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("2"); toast msg = toast.maketext(getbasecontext(), "you have clicked gate off", toast.length_short); msg.show(); } }); curton.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("5"); toast msg = toast.maketext(getbasecontext(), "you have clicked curton on", toast.length_short); msg.show(); } }); curtoff.setonclicklistener(new onclicklistener() { public void onclick(view v) { senddata("4"); toast msg = toast.maketext(getbasecontext(), "you have clicked curton off", toast.length_short); msg.show(); } }); gettemp.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { senddata("6"); toast msg = toast.maketext(getbasecontext(), "you have clicked temperature", toast.length_short); msg.show(); beginlistenfordata(); } }); } @override public void onresume() { super.onresume(); log.d(tag, "...in onresume - attempting client connect..."); // set pointer remote node using it's address. bluetoothdevice device = btadapter.getremotedevice(address); // 2 things needed create connection: // mac address, got above. // service id or uuid. in case using // uuid spp. seek { btsocket = device.createrfcommsockettoservicerecord(my_uuid); } grab (ioexception e) { errorexit("fatal error", "in onresume() , socket create failed: " + e.getmessage() + "."); } // discovery resource intensive. create sure isn't going on // when effort connect , pass message. btadapter.canceldiscovery(); // found connection. block until connects. log.d(tag, "...connecting remote..."); seek { btsocket.connect(); log.d(tag, "...connection established , info link opened..."); } grab (ioexception e) { seek { btsocket.close(); } grab (ioexception e2) { errorexit("fatal error", "in onresume() , unable close socket during connection failure" + e2.getmessage() + "."); } } // create info stream can talk server. log.d(tag, "...creating socket..."); seek { outstream = btsocket.getoutputstream(); } grab (ioexception e) { errorexit("fatal error", "in onresume() , output stream creation failed:" + e.getmessage() + "."); } } @override public void onpause() { super.onpause(); log.d(tag, "...in onpause()..."); if (outstream != null) { seek { outstream.flush(); } grab (ioexception e) { errorexit("fatal error", "in onpause() , failed flush output stream: " + e.getmessage() + "."); } } seek { btsocket.close(); } grab (ioexception e2) { errorexit("fatal error", "in onpause() , failed close socket." + e2.getmessage() + "."); } } private void checkbtstate() { // check bluetooth back upwards , check create sure turned on // emulator doesn't back upwards bluetooth , homecoming null if(btadapter==null) { errorexit("fatal error", "bluetooth not supported. aborting."); } else { if (btadapter.isenabled()) { log.d(tag, "...bluetooth enabled..."); } else { //prompt user turn on bluetooth intent enablebtintent = new intent(btadapter.action_request_enable); startactivityforresult(enablebtintent, request_enable_bt); } } } private void errorexit(string title, string message){ toast msg = toast.maketext(getbasecontext(), title + " - " + message, toast.length_short); msg.show(); finish(); } private void senddata(string message) { byte[] msgbuffer = message.getbytes(); log.d(tag, "...sending data: " + message + "..."); seek { outstream.write(msgbuffer); } grab (ioexception e) { string msg = "in onresume() , exception occurred during write: " + e.getmessage(); if (address.equals("00:00:00:00:00:00")) msg = msg + ".\n\nupdate server address 00:00:00:00:00:00 right address on line 37 in java code"; msg = msg + ".\n\ncheck spp uuid: " + my_uuid.tostring() + " exists on server.\n\n"; errorexit("fatal error", msg); } } void beginlistenfordata() { final handler handler = new handler(); final byte delimiter = 10; // ascii code newline // character stopworker = false; readbufferposition = 0; readbuffer = new byte[1024]; workerthread = new thread(new runnable() { public void run() { while (!thread.currentthread().isinterrupted() && !stopworker) { seek { int bytesavailable = mminputstream.available(); if (bytesavailable > 0) { byte[] packetbytes = new byte[bytesavailable]; mminputstream.read(packetbytes); (int = 0; < bytesavailable; i++) { byte b = packetbytes[i]; if (b == delimiter) { byte[] encodedbytes = new byte[readbufferposition]; system.arraycopy(readbuffer, 0, encodedbytes, 0, encodedbytes.length); final string info = new string( encodedbytes, "us-ascii"); readbufferposition = 0; handler.post(new runnable() { public void run() { temp.settext(data); } }); } else { readbuffer[readbufferposition++] = b; } } } } grab (ioexception ex) { stopworker = true; } } } }); workerthread.start(); } }

the sending part works fine, when seek retrieve info gives me next error message

06-20 17:28:12.440: d/abdel-domotic(6718): ...connection established , info link opened... 06-20 17:28:12.440: d/abdel-domotic(6718): ...creating socket... 06-20 17:28:15.470: d/abdel-domotic(6718): ...sending data: 6... 06-20 17:28:15.470: d/blz20_asockwrp(6718): asocket_write 06-20 17:28:15.470: i/blz20_wrapper(6718): blz20_wrp_poll: nfds 2, timeout -1 ms 06-20 17:28:15.470: d/blz20_wrapper(6718): blz20_wrp_poll: transp poll : (fd 43) returned r_ev [pollout ] (0x4) 06-20 17:28:15.470: d/blz20_wrapper(6718): blz20_wrp_poll: homecoming 1 06-20 17:28:15.470: d/blz20_wrapper(6718): blz20_wrp_write: wrote 1 bytes out of 1 on fd 43 06-20 17:28:15.490: w/dalvikvm(6718): threadid=9: thread exiting uncaught exception (group=0x4001e578) 06-20 17:28:15.500: e/androidruntime(6718): fatal exception: thread-10 06-20 17:28:15.500: e/androidruntime(6718): java.lang.nullpointerexception 06-20 17:28:15.500: e/androidruntime(6718): @ com.arduino.arduinoled1.mainactivity$8.run(mainactivity.java:260) 06-20 17:28:15.500: e/androidruntime(6718): @ java.lang.thread.run(thread.java:1019) 06-20 17:28:15.530: d/abdel-domotic(6718): ...in onpause()... 06-20 17:28:15.530: d/blz20_asockwrp(6718): asocket_abort [43,44,45] 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_shutdown: s 43, how 2 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_shutdown: fd (-1:43), bta 2, rc 1, wflags 0x800, cflags 0x0, port 9050 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_shutdown: shutdown socket 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_write: wrote 1 bytes out of 1 on fd 45 06-20 17:28:15.530: d/blz20_asockwrp(6718): asocket_destroy 06-20 17:28:15.530: d/blz20_asockwrp(6718): asocket_abort [43,44,45] 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_shutdown: s 43, how 2 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_shutdown: fd (-1:43), bta 2, rc 1, wflags 0x800, cflags 0x0, port 9050 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_shutdown: shutdown socket 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_write: wrote 1 bytes out of 1 on fd 45 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_close: s 45 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_close: std close (45) 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_close: s 44 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_close: std close (44) 06-20 17:28:15.530: i/blz20_wrapper(6718): blz20_wrp_close: s 43 06-20 17:28:15.530: d/blz20_wrapper(6718): blz20_wrp_close: fd (-1:43), bta 2, rc 1, wflags 0x800, cflags 0x0, port 9050 06-20 17:28:15.530: i/blz20_wrapper(6718): __close_prot_rfcomm: fd 43 06-20 17:28:15.530: i/btl_ifc(6718): send_ctrl_msg: [btl_ifc ctrl] send btlif_bts_rfc_close (bts) 8 pbytes (hdl 40) 06-20 17:28:15.530: d/btl_ifc_wrp(6718): wrp_close_s_only: wrp_close_s_only [43] (43:-1) [brcm.bt.btlif] 06-20 17:28:15.530: d/btl_ifc_wrp(6718): wrp_close_s_only: info socket closed 06-20 17:28:15.530: d/btl_ifc_wrp(6718): wsactive_del: delete wsock 43 active list [ad42cbd0] 06-20 17:28:15.530: d/btl_ifc_wrp(6718): wrp_close_s_only: wsock closed, homecoming pool 06-20 17:28:15.530: d/blz20_wrapper(6718): btsk_free: success

my first guess error in line 260 says int bytesavailable = mminputstream.available();

am correct?

what problem? , how can prepare it?

your mminputstream object null. forgot create object before using it.

java android bluetooth arduino

No comments:

Post a Comment