android - to implement code which can receive continuous data from bluetooth health device and I'm getting runtime error on eclipse. -


*this problem related method "ondestroy()" i've used in activity class.please *

bluetoothhdpactivity.java

package com.example.bluetoothhdp;

   public class bluetoothhdpactivity extends activity   {  private static final string tag = "bluetoothhealthactivity"; private static final int health_profile_source_data_type = 0x1007; private static final int request_enable_bt = 1; private textview mconnectindicator; private imageview mdataindicator; private textview mstatusmessage; private bluetoothadapter mbluetoothadapter; private bluetoothdevice[] mallbondeddevices; private bluetoothdevice mdevice; private int mdeviceindex = 0; private resources mres; private messenger mhealthservice; private boolean mhealthservicebound;   private handler mincominghandler = new handler()  {     @override     public void handlemessage(message msg)      {         switch (msg.what)          {             case bluetoothhdpservice.status_health_app_reg:mstatusmessage.settext(                     string.format(mres.getstring(r.string.cancel),                             msg.arg1));                 break;               case bluetoothhdpservice.status_health_app_unreg:                 mstatusmessage.settext(                         string.format(mres.getstring(r.string.copy),                         msg.arg1));                 break;               case bluetoothhdpservice.status_read_data:                 mstatusmessage.settext(mres.getstring(r.string.copyurl));                 mdataindicator.setimagelevel(1);                 break;               case bluetoothhdpservice.status_read_data_done:                 mstatusmessage.settext(mres.getstring(r.string.cut));                 mdataindicator.setimagelevel(0);                 break;                 case bluetoothhdpservice.status_create_channel:                 mstatusmessage.settext(                         string.format(mres.getstring(r.string.defaultmsisdnalphatag),                         msg.arg1));                 mconnectindicator.settext("connected");                 break;                 case bluetoothhdpservice.status_destroy_channel:                 mstatusmessage.settext(                               string.format(mres.getstring(r.string.defaultvoicemailalphatag),                         msg.arg1));                 mconnectindicator.settext("disconnected");                 break;             default:                 super.handlemessage(msg);         }     } };  private final messenger mmessenger = new messenger(mincominghandler);  @override public void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);       mbluetoothadapter = bluetoothadapter.getdefaultadapter();     if (mbluetoothadapter == null)      {        toast toast= toast.maketext(this,"bluetooth not available", toast.length_long);         toast.show();         finish();         return;     }      setcontentview(r.layout.activity_list_item);     mconnectindicator = (textview) findviewbyid(r.id.addtodictionary);     mstatusmessage = (textview) findviewbyid(r.id.background);      mres = getresources();     mhealthservicebound = false;       button registerappbutton = (button) findviewbyid(r.id.button1);     registerappbutton.setonclicklistener(new view.onclicklistener()      {         public void onclick(view v)          {             sendmessage(bluetoothhdpservice.msg_reg_health_app,                     health_profile_source_data_type);         }     });       button unregisterappbutton = (button) findviewbyid(r.id.button2);     unregisterappbutton.setonclicklistener(new view.onclicklistener()      {         public void onclick(view v)          {             sendmessage(bluetoothhdpservice.msg_unreg_health_app, 0);         }     });      button connectbutton = (button) findviewbyid(r.id.button3);     connectbutton.setonclicklistener(new view.onclicklistener()      {         public void onclick(view v)          {             mallbondeddevices =                     (bluetoothdevice[]) mbluetoothadapter.getbondeddevices().toarray(                             new bluetoothdevice[0]);              if (mallbondeddevices.length > 0)              {                 int devicecount = mallbondeddevices.length;                 if (mdeviceindex < devicecount) mdevice = mallbondeddevices[mdeviceindex];                 else                 {                     mdeviceindex = 0;                     mdevice = mallbondeddevices[0];                 }                 string[] devicenames = new string[devicecount];                 int = 0;                 (bluetoothdevice device : mallbondeddevices)                  {                     devicenames[i++] = device.getname();                 }                 selectdevicedialogfragment devicedialog =                         selectdevicedialogfragment.newinstance(devicenames, mdeviceindex);                 devicedialog.show(getfragmentmanager(), "devicedialog");             }         }     });       button disconnectbutton = (button) findviewbyid(r.id.closebutton);     disconnectbutton.setonclicklistener(new view.onclicklistener()      {         public void onclick(view v)          {             disconnectchannel();         }     });     registerreceiver(mreceiver, initintentfilter()); }   private serviceconnection mconnection = new serviceconnection()  {     public void onserviceconnected(componentname name, ibinder service)      {         mhealthservicebound = true;         message msg = message.obtain(null, bluetoothhdpservice.msg_reg_client);         msg.replyto = mmessenger;         mhealthservice = new messenger(service);         try {             mhealthservice.send(msg);         } catch (remoteexception e)          {             log.w(tag, "unable register client service.");             e.printstacktrace();         }     }      public void onservicedisconnected(componentname name)      {         mhealthservice = null;         mhealthservicebound = false;     } }; @override protected void ondestroy()  {     super.ondestroy();     if (mhealthservicebound) unbindservice(mconnection);     unregisterreceiver(mreceiver);   }   @override protected void onstart()  {     super.onstart();      if (!mbluetoothadapter.isenabled()) {         intent enableintent = new intent(bluetoothadapter.action_request_enable);         startactivityforresult(enableintent, request_enable_bt);     } else      {         initialize();     } }  @override protected void onactivityresult(int requestcode, int resultcode, intent data)  {     switch (requestcode)      {     case request_enable_bt:         if (resultcode == activity.result_ok)          {             initialize();         } else {             finish();             return;         }     } }      public void setdevice(int position)  {     mdevice = this.mallbondeddevices[position];     mdeviceindex = position; }  private void connectchannel()  {     sendmessagewithdevice(bluetoothhdpservice.msg_connect_channel); }  private void disconnectchannel()  {     sendmessagewithdevice(bluetoothhdpservice.msg_disconnect_channel); }  private void initialize()  {      intent intent = new intent(this, bluetoothhdpservice.class);     startservice(intent);     bindservice(intent, mconnection, context.bind_auto_create); }   private intentfilter initintentfilter()  {     intentfilter filter = new intentfilter();     filter.addaction(bluetoothadapter.action_state_changed);     return filter; }  private final broadcastreceiver mreceiver = new broadcastreceiver()  {     @override     public void onreceive(context context, intent intent)     {         final string action = intent.getaction();         if (bluetoothadapter.action_state_changed.equals(action))          {             if (intent.getintextra(bluetoothadapter.extra_state, bluetoothadapter.error) ==                 bluetoothadapter.state_on)              {                 initialize();             }         }     } };   private void sendmessage(int what, int value)  {     if (mhealthservice == null) {         log.d(tag, "health service not connected.");         return;     }      try {         mhealthservice.send(message.obtain(null, what, value, 0));     } catch (remoteexception e)      {         log.w(tag, "unable reach service.");         e.printstacktrace();     } }  private void sendmessagewithdevice(int what)  {     if (mhealthservice == null)      {         log.d(tag, "health service not connected.");         return;     }      try {         mhealthservice.send(message.obtain(null, what, mdevice));     } catch (remoteexception e)      {         log.w(tag, "unable reach service.");         e.printstacktrace();     } }  public static class selectdevicedialogfragment extends dialogfragment  {      public static selectdevicedialogfragment newinstance(string[] names, int position)      {         selectdevicedialogfragment frag = new selectdevicedialogfragment();         bundle args = new bundle();         args.putstringarray("names", names);         args.putint("position", position);         frag.setarguments(args);         return frag;     }      @override     public dialog oncreatedialog(bundle savedinstancestate)      {         string[] devicenames = getarguments().getstringarray("names");         int position = getarguments().getint("position", -1);         if (position == -1) position = 0;         return new alertdialog.builder(getactivity())                 .settitle("select device")                 .setpositivebutton(r.string.ok,                     new dialoginterface.onclicklistener()                  {                         public void onclick(dialoginterface dialog, int which)                          {                             ((bluetoothhdpactivity) getactivity()).connectchannel();                         }                     })                 .setsinglechoiceitems(devicenames, position,                     new dialoginterface.onclicklistener()                  {                         public void onclick(dialoginterface dialog, int which)                          {                             ((bluetoothhdpactivity) getactivity()).setdevice(which);                         }                     }                 )                 .create();     } }} 

bluetoothhdpservice.java

package com.example.bluetoothhdp;

   public class bluetoothhdpservice extends service    {  private static final string tag = "bluetoothhdpservice";  public static final int result_ok = 0; public static final int result_fail = -1;  public static final int status_health_app_reg = 100; public static final int status_health_app_unreg = 101; public static final int status_create_channel = 102; public static final int status_destroy_channel = 103; public static final int status_read_data = 104; public static final int status_read_data_done = 105; public static final int msg_reg_client = 200; public static final int msg_unreg_client = 201; public static final int msg_reg_health_app = 300; public static final int msg_unreg_health_app = 301; public static final int msg_connect_channel = 400; public static final int msg_disconnect_channel = 401;  private bluetoothhealthappconfiguration mhealthappconfig; private bluetoothadapter mbluetoothadapter; private bluetoothhealth mbluetoothhealth; private bluetoothdevice mdevice; private int mchannelid; private messenger mclient; private class incominghandler extends handler  {     @override     public void handlemessage(message msg)      {         switch (msg.what)          {              case msg_reg_client:                 log.d(tag, "activity client registered");                 mclient = msg.replyto;                 break;              case msg_unreg_client:                 mclient = null;                 break;              case msg_reg_health_app:                 registerapp(msg.arg1);                 break;              case msg_unreg_health_app:                 unregisterapp();                 break;              case msg_connect_channel:                 mdevice = (bluetoothdevice) msg.obj;                 connectchannel();                 break;              case msg_disconnect_channel:                 mdevice = (bluetoothdevice) msg.obj;                 disconnectchannel();                 break;             default:                 super.handlemessage(msg);         }     } }  final messenger mmessenger = new messenger(new incominghandler());  @override public void oncreate()  {     super.oncreate();     mbluetoothadapter = bluetoothadapter.getdefaultadapter();     if (mbluetoothadapter == null || !mbluetoothadapter.isenabled())      {         stopself();         return;     }     if (!mbluetoothadapter.getprofileproxy(this, mbluetoothservicelistener,             bluetoothprofile.health)) {        toast toast= toast.maketext(this, "hdp not available",toast.length_long);        toast.show();         stopself();         return;     } }  @override public int onstartcommand(intent intent, int flags, int startid)  {     log.d(tag, "bluetoothhdpservice running.");     return start_sticky; }  @override public ibinder onbind(intent intent)  {     return mmessenger.getbinder(); };   private void registerapp(int datatype)  {     mbluetoothhealth.registersinkappconfiguration(tag, datatype, mhealthcallback); }   private void unregisterapp()  {     mbluetoothhealth.unregisterappconfiguration(mhealthappconfig); }   private void connectchannel()  {     log.i(tag, "connectchannel()");     mbluetoothhealth.connectchanneltosource(mdevice, mhealthappconfig); }   private void disconnectchannel()  {     log.i(tag, "disconnectchannel()");     mbluetoothhealth.disconnectchannel(mdevice, mhealthappconfig, mchannelid); }   private final bluetoothprofile.servicelistener mbluetoothservicelistener =         new bluetoothprofile.servicelistener()  {     public void onserviceconnected(int profile, bluetoothprofile proxy)      {         if (profile == bluetoothprofile.health)         {             mbluetoothhealth = (bluetoothhealth) proxy;             if (log.isloggable(tag, log.debug))                 log.d(tag, "onserviceconnected profile: " + profile);         }     }      public void onservicedisconnected(int profile)      {         if (profile == bluetoothprofile.health)          {             mbluetoothhealth = null;         }     } };    private final bluetoothhealthcallback mhealthcallback = new bluetoothhealthcallback()       {       public void onhealthappconfigurationstatuschange(bluetoothhealthappconfiguration config,int status)      {         if (status == bluetoothhealth.app_config_registration_failure)          {             mhealthappconfig = null;             sendmessage(status_health_app_reg, result_fail);         } else if (status == bluetoothhealth.app_config_registration_success)          {             mhealthappconfig = config;             sendmessage(status_health_app_reg, result_ok);         } else if (status == bluetoothhealth.app_config_unregistration_failure ||                 status == bluetoothhealth.app_config_unregistration_success)          {             sendmessage(status_health_app_unreg,                     status == bluetoothhealth.app_config_unregistration_success ?                     result_ok : result_fail);         }     }      public void onhealthchannelstatechange(bluetoothhealthappconfiguration config,             bluetoothdevice device, int prevstate, int newstate, parcelfiledescriptor fd,             int channelid)      {         if (log.isloggable(tag, log.debug))             log.d(tag, string.format("prevstate\t%d ----------> newstate\t%d",                     prevstate, newstate));         if (prevstate == bluetoothhealth.state_channel_disconnected &&                 newstate == bluetoothhealth.state_channel_connected)          {             if (config.equals(mhealthappconfig))              {                 mchannelid = channelid;                 sendmessage(status_create_channel, result_ok);                 (new readthread(fd)).start();             } else              {                 sendmessage(status_create_channel, result_fail);             }         } else if (prevstate == bluetoothhealth.state_channel_connecting &&                    newstate == bluetoothhealth.state_channel_disconnected)          {             sendmessage(status_create_channel, result_fail);         } else if (newstate == bluetoothhealth.state_channel_disconnected)          {             if (config.equals(mhealthappconfig))              {                 sendmessage(status_destroy_channel, result_ok);             } else              {                 sendmessage(status_destroy_channel, result_fail);             }         }     } };   private void sendmessage(int what, int value)  {     if (mclient == null)      {         log.d(tag, "no clients registered.");         return;     }      try      {         mclient.send(message.obtain(null, what, value, 0));     } catch (remoteexception e)      {          e.printstacktrace();     } }  private class readthread extends thread  {     private parcelfiledescriptor mfd;      public readthread(parcelfiledescriptor fd)      {         super();         mfd = fd;     }      @override     public void run()      {         fileinputstream fis = new fileinputstream(mfd.getfiledescriptor());         final byte data[] = new byte[8192];         try {             while(fis.read(data) > -1)              {                 sendmessage(status_read_data, 0);             }         } catch(ioexception ioe) {}         if (mfd != null)          {             try {                 mfd.close();             } catch (ioexception e) { /* nothing. */ }         }         sendmessage(status_read_data_done, 0);     } }} 

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.example.bluetooth.health"   android:versioncode="1"   android:versionname="1.0"> <uses-sdk      android:minsdkversion="14"     android:targetsdkversion="14" /> <uses-permission      android:name="android.permission.bluetooth" /> <uses-permission  android:name="android.permission.bluetooth_admin"/> <application      android:icon="@drawable/icon"      android:label="@string/app_name">     <activity          android:name="com.example.bluetoothhdp.bluetoothhdpactivity"               android:label="@string/app_name">         <intent-filter>             <action                  android:name="android.intent.action.main" />             <category                  android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <service          android:name=".bluetoothhdpservice" />   </application>   </manifest> 

logcat

         08-26 16:37:44.773: e/androidruntime(802): fatal exception: main          08-26 16:37:44.773: e/androidruntime(802): java.lang.runtimeexception: unable destroy activity {com.example.bluetoothhdp/com.example.bluetoothhdp.bluetoothhdpactivity}: java.lang.illegalargumentexception: receiver not registered: com.example.bluetoothhdp.bluetoothhdpactivity$3@40ce3da8          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread.performdestroyactivity(activitythread.java:3451)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread.handledestroyactivity(activitythread.java:3469)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread.access$1200(activitythread.java:141)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread$h.handlemessage(activitythread.java:1287)          08-26 16:37:44.773: e/androidruntime(802):     @ android.os.handler.dispatchmessage(handler.java:99)          08-26 16:37:44.773: e/androidruntime(802):     @ android.os.looper.loop(looper.java:137)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread.main(activitythread.java:5041)          08-26 16:37:44.773: e/androidruntime(802):     @ java.lang.reflect.method.invokenative(native method)          08-26 16:37:44.773: e/androidruntime(802):     @ java.lang.reflect.method.invoke(method.java:511)          08-26 16:37:44.773: e/androidruntime(802):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793)          08-26 16:37:44.773: e/androidruntime(802):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560)          08-26 16:37:44.773: e/androidruntime(802):     @ dalvik.system.nativestart.main(native method)          08-26 16:37:44.773: e/androidruntime(802): caused by: java.lang.illegalargumentexception: receiver not registered: com.example.bluetoothhdp.bluetoothhdpactivity$3@40ce3da8          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.loadedapk.forgetreceiverdispatcher(loadedapk.java:657)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.contextimpl.unregisterreceiver(contextimpl.java:1339)          08-26 16:37:44.773: e/androidruntime(802):     @ android.content.contextwrapper.unregisterreceiver(contextwrapper.java:445)          08-26 16:37:44.773: e/androidruntime(802):     @ com.example.bluetoothhdp.bluetoothhdpactivity.ondestroy(bluetoothhdpactivity.java:254)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activity.performdestroy(activity.java:5273)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.instrumentation.callactivityondestroy(instrumentation.java:1110)          08-26 16:37:44.773: e/androidruntime(802):     @ android.app.activitythread.performdestroyactivity(activitythread.java:3438)          08-26 16:37:44.773: e/androidruntime(802):     ... 11 more 

looks manifest bit borked, especialy regarding packages. can try modify below, mention using emulator. emulator doesn't support bluetooth. unless have physical phone test with, you'll need run android virtual machine. there's few other questions deal virtual machine topic, e.g - how test bluetooth based application on androidx86 on top of virtualbox inbuilt laptop bluetooth

<?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   package="com.example.bluetoothhdp"   android:versioncode="1"   android:versionname="1.0"> <uses-sdk      android:minsdkversion="14"     android:targetsdkversion="14" /> <uses-permission      android:name="android.permission.bluetooth" /> <uses-permission  android:name="android.permission.bluetooth_admin"/> <application      android:icon="@drawable/icon"      android:label="@string/app_name">     <activity          android:name=".bluetoothhdpactivity"               android:label="@string/app_name">         <intent-filter>             <action                  android:name="android.intent.action.main" />             <category                  android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <service          android:name=".bluetoothhdpservice" />   </application>   </manifest> 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -