i new android development.i have created 1 bluetooth application sending image device application. first load image gallery , using uri send image remote device.and using search button search possibility devices visible 1 list view. problem when click list view(remotedevice address)the remote device not paired , not sending images.please me thanks...`
my xml layout file:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <button android:id="@+id/button_search" android:text="search listener" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/text_message" /> <button android:id="@+id/btnidopengallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/button_listen" android:text="opengallery" /> <listview android:id="@+id/list_discovered" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/btnidopengallery" android:layout_alignparenttop="true" /> </relativelayout> my mainactivity:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnopenimage=(button)findviewbyid(r.id.btnidopengallery); openimage(); founddevices = new arraylist<bluetoothdevice>(); configurebluetooth(); setuplistview(); } private void configurebluetooth() { bluetooth = bluetoothadapter.getdefaultadapter(); } private void openimage() { btnopenimage.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_pick); startactivityforresult(intent.createchooser(intent, "select picture"), select_picture); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if(requestcode==2) { selectedimageuri = data.getdata(); } } private void setupsearchbutton() { button searchbutton = (button) findviewbyid(r.id.button_search); searchbutton.setonclicklistener(new onclicklistener() { public void onclick(view view) { registerreceiver(discoveryresult, new intentfilter( bluetoothdevice.action_found)); if (!bluetooth.isdiscovering()) { founddevices.clear(); bluetooth.startdiscovery(); } } }); } private void setuplistview() { list = (listview) findviewbyid(r.id.list_discovered); aa = new arrayadapter<bluetoothdevice>(this, android.r.layout.simple_list_item_1, founddevices); list.setadapter(aa); list.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> arg0, view view, int index, long arg3) { asynctask<integer, void, void> connecttask = new asynctask<integer, void, void>() { @override protected void doinbackground(integer... params) { try { bluetoothdevice device = founddevices .get(params[0]); socket = device .createrfcommsockettoservicerecord(uuid); socket.connect(); } catch (ioexception e) { log.d("bluetooth_client", e.getmessage()); } return null; } @override protected void onpostexecute(void result) { //switchui(); try { sendfile(selectedimageuri, socket); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }; connecttask.execute(index); } }); } public void sendfile(uri uri, bluetoothsocket bs) throws ioexception { bufferedinputstream bis = new bufferedinputstream(getcontentresolver().openinputstream(uri)); outputstream os = bs.getoutputstream(); try { int buffersize = 1024; byte[] buffer = new byte[buffersize]; // need know how may bytes read write them bytebuffer int len = 0; while ((len = bis.read(buffer)) != -1) { os.write(buffer, 0, len); } } { bis.close(); os.flush(); os.close(); bs.close(); } }
Comments
Post a Comment