How to fix Error receiving broadcast Intent in android -
i writing android application usb host communication. sending sample data android when ever button pressed. when click button showing error called error receiving broadcast intent please tell me how solve this. usb driver activity:
/* * usbcontroller.java * file part of usbcontroller * * copyright (c) 2012 - manuel di cerbo * * usbcontroller free software; can redistribute and/or modify * under terms of gnu general public license published * free software foundation; either version 2 of license, or * (at option) later version. * * usbcontroller distributed in hope useful, * without warranty; without implied warranty of * merchantability or fitness particular purpose. see * gnu general public license more details. * * should have received copy of gnu general public license * along usbcontroller. if not, see <http://www.gnu.org/licenses/>. */ package com.example.democomm; import java.util.hashmap; import java.util.iterator; import android.app.activity; import android.app.pendingintent; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.hardware.usb.usbconstants; import android.hardware.usb.usbdevice; import android.hardware.usb.usbdeviceconnection; import android.hardware.usb.usbendpoint; import android.hardware.usb.usbinterface; import android.hardware.usb.usbmanager; import android.util.log; public class usbdriver { private final context mapplicationcontext; private final usbmanager musbmanager; @suppresswarnings("unused") private final usbconnectionhandler mconnectionhandler; private final int vid; private final int pid; protected static final string action_usb_permission = "ch.serverbox.android.usb"; public static int device_exception; public static usbdevice device_details; public static usbendpoint data_in_end_point = null; public static usbendpoint data_out_end_point = null; public static usbdeviceconnection usb_device_connection; public usbdriver(activity parentactivity,usbconnectionhandler connectionhandler, int vid, int pid) { mapplicationcontext = parentactivity.getapplicationcontext(); mconnectionhandler = connectionhandler; musbmanager = (usbmanager) mapplicationcontext.getsystemservice(context.usb_service); vid = 6790; pid =29987; device_exception = 0; // init(); check_devices(); } private void check_devices() { @suppresswarnings("unused") int j=0; hashmap<string, usbdevice> devlist = musbmanager.getdevicelist(); iterator<usbdevice> deviter = devlist.values().iterator(); device_details = null; if (devlist.size() != 0) { while (deviter.hasnext()) { device_details = deviter.next(); if (device_details.getvendorid() == vid && device_details.getproductid() == pid) { if (!musbmanager.haspermission(device_details)) { onpermissiondenied(device_details); } else { usbdeviceconnection conn = musbmanager.opendevice(device_details); if (!conn.claiminterface(device_details.getinterface(1), true)) { return; } conn.controltransfer(0x21, 34, 0, 0, null, 0, 0); conn.controltransfer(0x21, 32, 0, 0,new byte[] { (byte) 0x80, 0x25 , 0x00, 0x00,0x00, 0x00, 0x08 }, 7, 0); usb_device_connection=conn; data_in_end_point = null; data_out_end_point = null; usbinterface usbif = device_details.getinterface(1); (int = 0; < usbif.getendpointcount(); i++) { if (usbif.getendpoint(i).gettype() == usbconstants.usb_endpoint_xfer_bulk) { if (usbif.getendpoint(i).getdirection() == usbconstants.usb_dir_in) data_in_end_point = usbif.getendpoint(i); else data_out_end_point = usbif.getendpoint(i); } } if (data_in_end_point == null || data_out_end_point == null) device_exception = 2; } break; }j++; } if (device_details == null) { device_exception = 3; return; } } else { device_exception = 1; return; } } public void onpermissiondenied(usbdevice d) { usbmanager usbman = (usbmanager) mapplicationcontext.getsystemservice(context.usb_service); pendingintent pi = pendingintent.getbroadcast(mapplicationcontext, 0, new intent(action_usb_permission), 0); mapplicationcontext.registerreceiver(mpermissionreceiver,new intentfilter(action_usb_permission)); usbman.requestpermission(d, pi); } private class permissionreceiver extends broadcastreceiver { private final ipermissionlistener mpermissionlistener; public permissionreceiver(ipermissionlistener permissionlistener) { mpermissionlistener = permissionlistener; } @override public void onreceive(context context, intent intent) { mapplicationcontext.unregisterreceiver(this); if (intent.getaction().equals(action_usb_permission)) { if (!intent.getbooleanextra(usbmanager.extra_permission_granted, false)) { mpermissionlistener.onpermissiondenied((usbdevice) intent.getparcelableextra(usbmanager.extra_device)); } else { l("permission granted"); usbdevice dev = (usbdevice) intent.getparcelableextra(usbmanager.extra_device); if (dev != null) { if (dev.getvendorid() == vid && dev.getproductid() == pid) { check_devices() ; } } else { e("device not present!"); } } } } } // main loop // end main loop private broadcastreceiver mpermissionreceiver = new permissionreceiver(new ipermissionlistener() { @override public void onpermissiondenied(usbdevice d) { l("permission denied on " + d.getdeviceid()); } }); private static interface ipermissionlistener { void onpermissiondenied(usbdevice d); } public final static string tag = "usbcontroller"; private void l(object msg) { log.d(tag, ">==<" + msg.tostring() + " >==<"); } private void e(object msg) { log.e(tag, ">==< " + msg.tostring() + " >==<"); } }
this usb handler:
/* * iusbconnectionhandler.java * file part of usbcontroller * * copyright (c) 2012 - manuel di cerbo * * usbcontroller free software; can redistribute and/or modify * under terms of gnu general public license published * free software foundation; either version 2 of license, or * (at option) later version. * * usbcontroller distributed in hope useful, * without warranty; without implied warranty of * merchantability or fitness particular purpose. see * gnu general public license more details. * * should have received copy of gnu general public license * along usbcontroller. if not, see <http://www.gnu.org/licenses/>. */ package com.example.democomm; /** * (c) neuxs-computing gmbh switzerland * @author manuel di cerbo, 02.02.2012 * */ public interface usbconnectionhandler { void onusbstopped(); void onerrorlooperrunningalready(); void ondevicenotfound(); }
this main activity:
package com.example.democomm; import java.nio.bytebuffer; import java.util.hashmap; import android.app.actionbar; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.graphics.typeface; import android.hardware.usb.usbdevice; import android.hardware.usb.usbdeviceconnection; import android.hardware.usb.usbendpoint; import android.hardware.usb.usbinterface; import android.hardware.usb.usbmanager; import android.hardware.usb.usbrequest; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.checkbox; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { public static final int targetvendorid = 6790; public static final int targetproductid = 29987; public usbmanager manager; public usbdeviceconnection usbdeviceconnection; public usbinterface usbinterfacefound = null; public usbendpoint endpointout = null; public usbendpoint endpointin = null; public usbdevice usbdevice,device_details; public usbendpoint listusbendpoint; public static final string prefs_name = "loginprefs"; hashmap<string, usbdevice> devicelist= null; int selectedendpoint; static int coil_no; private static final int vid = 6790; private static final int pid = 29987; @suppresswarnings("unused") private static usbdriver usb_driver_class; actionbar actionbar; usbconnectionhandler connectionhandler; public static usbdriver usb_driver_child; public static boolean communication_failed,frame_ok,total_frame_decoded; static byte[] communication_byte; public sharedpreferences loginpreferences; public sharedpreferences.editor loginpreferenceseditor; public boolean savelogin; public string password; button sample_button; public edittext username,password; checkbox remember_me; typeface custom_font; string = ""; intent i2; checkbox show_password; string procid; string user_name_string,password_string; textview title,username_txt,password_txt,company_name; bytebuffer buffer; button signin; edittext dialog_username,dialog_password,dialog_confirm; static byte[] sample; static boolean communication_ok; public static float []wave_form_data=new float[1500]; public static float []wave_form_data_1=new float[1500]; public static float respsonse_time,drive_voltage; static int sequence_no,response_time; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); signin = (button)findviewbyid(r.id.button1); signin.setonclicklistener(new view.onclicklistener() { @suppresswarnings("static-access") @override public void onclick(view v) { communication_byte=new byte[1]; if(check_devices_available()==true) { int packet_size = usb_driver_child.data_in_end_point.getmaxpacketsize(); toast.maketext(mainactivity.this,""+packet_size, toast.length_long).show(); receive.start(); communication_ok=false; for(int i=0;(i<5 && communication_ok!=true);i++) send_communication_check_command(); if(communication_ok) toast.maketext(mainactivity.this, "communication established", 1000).show(); else toast.maketext(mainactivity.this, "communication failure", 10000).show(); } } }); } public boolean check_devices_available() { usb_driver_class = new usbdriver(this, connectionhandler, vid, pid); if(usb_driver_child.device_exception==0) { if(usb_driver_child.usb_device_connection==null || usb_driver_child.data_out_end_point==null) return false; toast.maketext(mainactivity.this,"device found", 1000).show(); return true; } else if(usb_driver_child.device_exception==1) { toast.maketext(mainactivity.this,"no devices attached ", toast.length_long).show(); return false; } else if(usb_driver_child.device_exception==2) { toast.maketext(mainactivity.this,"device found,but no end points", toast.length_long).show(); return false; } else if(usb_driver_child.device_exception==3) { toast.maketext(mainactivity.this,"unable open device", toast.length_long).show(); return false; } return false; //un known exception } thread receive = new thread(new runnable() { @suppresswarnings("unused") @override public void run() { sequence_no=0; buffer = bytebuffer.allocate(64); sample = new byte[64]; int frame_size; usbrequest request = new usbrequest(); int i,j; byte [] datarx=new byte[1]; char q; while (true) { request.initialize(usbdriver.usb_device_connection, usbdriver.data_in_end_point); request.queue(buffer, 64); if (usbdriver.usb_device_connection.requestwait() == request) { sample=buffer.array(); for(i=0;i<64;i++) { if(sample[i]=='&') { communication_ok=true; break; } else if(sample[i]==0x03) { if(sample[0]==0x02) frame_ok=true; break; } } if(frame_ok==true) { frame_ok=false; if(sample[1]==1) coil_no=1; else coil_no=2; response_time= (int)(((sample[2]&0x00ff)<<8) + (sample[3]&0x00ff)); total_frame_decoded=true; sample = null; } } } } }); private static void send_communication_check_command() { long i,j; communication_byte[0]='&'; usbdriver.usb_device_connection.bulktransfer(usbdriver.data_out_end_point,communication_byte, 1, 0); for(i=0;(i<1000 && communication_ok!=true) ;i++) for(j=0;(j<1000 && communication_ok!=true);j++); } //test_name=1; drive response test //test_name=2; drive life test //test_name=3; stop test /*public static void send_command_for_drive_response_test(int test_name) { int i; byte []command_to_transfer= new byte[5]; communication_ok=false; for(i=0;i<5;i++) { send_communication_check_command(); if(communication_ok==true) break; } if(communication_ok==true) { command_to_transfer[0]='*'; if(test_name==1) { command_to_transfer[1]=command_to_transfer[2]=(byte) 0xa1; } else if(test_name==2) { command_to_transfer[1]=command_to_transfer[2]=(byte) 0xa2; } else if(test_name==3) { command_to_transfer[1]=command_to_transfer[2]=(byte) 0xa3; } command_to_transfer[3]=0; //for future reference command_to_transfer[4]='#'; usbdriver.usb_device_connection.bulktransfer(usbdriver.data_out_end_point,command_to_transfer, 5, 0); communication_failed=true; } else { communication_failed=false; } }*/ }
this logcat:
fatal exception: main java.lang.runtimeexception: error receiving broadcast intent { act=ch.serverbox.android.usb flg=0x10 (has extras) } in com.example.democomm.usbdriver$permissionreceiver@41ce8ea8 @ android.app.loadedapk$receiverdispatcher$args.run(loadedapk.java:773) @ android.os.handler.handlecallback(handler.java:730) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5103) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) @ dalvik.system.nativestart.main(native method) caused by: java.lang.arrayindexoutofboundsexception: length=1; index=1 @ android.hardware.usb.usbdevice.getinterface(usbdevice.java:155) @ com.example.democomm.usbdriver.check_devices(usbdriver.java:88) @ com.example.democomm.usbdriver.access$4(usbdriver.java:65) @ com.example.democomm.usbdriver$permissionreceiver.onreceive(usbdriver.java:171) @ android.app.loadedapk$receiverdispatcher$args.run(loadedapk.java:763) ... 9 more
never worked usb communication in android, error written right in log:
caused by: java.lang.arrayindexoutofboundsexception: length=1; index=1 @ android.hardware.usb.usbdevice.getinterface(usbdevice.java:155) @ com.example.democomm.usbdriver.check_devices(usbdriver.java:88)
the 88-th line looks this:
device_details.getinterface(1);
looks want first interface here , can obtained device_details.getinterface(0)
, not device_details.getinterface(1)
.
Comments
Post a Comment