multithreading - Android Bluetooth connecting to Device socket.connect() wont connect -


fixed!!!

didnt work because of uuid since api15 can use function uuid

uid uuid = bluetoothdevice.getuuids()[0].getuuid();  bluetoothsocket socket = bluetoothdevice.createrfcommsockettoservicerecord(uuid); 

fixed!!!

i trying connect devie write data onto it. can search devices , if click connect thread gets started, "run()" function isnt called. programm stucked at

right trying connect bluetooth headset. dont know if pairing 2 devices? if use normal bluetooth of phone, pairing 2 devices code 1 2 3 4 in program dont have enter code. can me pls?

mmsocket.connect();

so connectedthread() never runs!

here full code:

public class mainactivity extends activity {

private static final int request_enable_bt = 1; private bluetoothadapter mbluetoothadapter = null; private arrayadapter<string> mnewdevicesarrayadapter; listview newdeviceslistview;    // return intent public static string extra_device_address = "device_address"; private connectingdevices mconnectingdevices = null;   @override protected void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mbluetoothadapter = bluetoothadapter.getdefaultadapter();      newdeviceslistview = (listview)findviewbyid(r.id.new_devices);     mnewdevicesarrayadapter=new arrayadapter<string>(mainactivity.this, android.r.layout.simple_list_item_1);     newdeviceslistview.setadapter(mnewdevicesarrayadapter);     newdeviceslistview.setonitemclicklistener(mdeviceclicklistener);      registerreceiver(mreceiver, new intentfilter(bluetoothdevice.action_found));      mconnectingdevices = new connectingdevices();  }  @override public boolean oncreateoptionsmenu(menu menu)  {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.main, menu);     return true; }  public void onclickbtnetage1(view view) { mconnectingdevices.write(new byte[2]); }   public void onclickbtnactivatebluetooth(view view) {        if (mbluetoothadapter == null)        {             toast.maketext(this, "bluetooth ist nicht verfügbar!", toast.length_long).show();             finish();             return;       }       else if (!mbluetoothadapter.isenabled())        {             intent enablebtintent = new intent(bluetoothadapter.action_request_enable);             startactivityforresult(enablebtintent, request_enable_bt);       }       toast.maketext(this, "bluetooth wurde aktiviert!", toast.length_long).show();       findviewbyid(r.id.btnsearchdevices).setenabled(true);   }  public void onclickbtnsearchdevices(view view) {     mbluetoothadapter.startdiscovery();     toast.maketext(this, "bluetooth geräte werden gesucht!", toast.length_long).show();       // find , set listview newly discovered devices  }  // create broadcastreceiver action_found  private final broadcastreceiver mreceiver = new broadcastreceiver() {         @override         public void onreceive(context context, intent intent) {             string action = intent.getaction();              // when discovery finds device             if (bluetoothdevice.action_found.equals(action)) {                 // bluetoothdevice object intent                 bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);                 // if it's paired, skip it, because it's been listed                 if (device.getbondstate() != bluetoothdevice.bond_bonded)                  {                     mnewdevicesarrayadapter.add(device.getname() + "\n" + device.getaddress());                     mnewdevicesarrayadapter.notifydatasetchanged();                  }               }         }     };      // on-click listener devices in listviews     private onitemclicklistener mdeviceclicklistener = new onitemclicklistener() {         public void onitemclick(adapterview<?> av, view v, int arg2, long arg3) {             // cancel discovery because it's costly , we're connect             mbluetoothadapter.canceldiscovery();              // device mac address, last 17 chars in view             string info = ((textview) v).gettext().tostring();             string address = info.substring(info.length() - 17);              // create result intent , include mac address             intent intent = new intent();             intent.putextra(extra_device_address, address);             // set result , finish activity         //    setresult(activity.result_ok, intent);           // finish();                    address = intent.getextras().getstring(extra_device_address);                 // bluetoothdevice object                 bluetoothdevice device = mbluetoothadapter.getremotedevice(address);                 // attempt connect device                 mconnectingdevices.connect(device);           }      };} 

and connectingdevices class

        public class connectingdevices {  boolean bconnectedthread = false; boolean bconnectthread = false; boolean bconnectedthreadstart = false; boolean bconnectthreadstart = false; boolean brun=false; boolean bconnectedsynchronized=false; //00001108-0000-1000-8000-00805f9b34fb     private bluetoothadapter mbluetoothadapter = null;     private static final uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");     private connectedthread mconnectedthread;     //private final handler mhandler;     private connectthread mconnectthread;      public void write(byte[] out) {         boolean a=bconnectedthread;         boolean b=bconnectthread;         boolean c=bconnectedthreadstart;         boolean d=bconnectthreadstart;         boolean e=brun;         boolean f=bconnectedsynchronized;          // create temporary object         connectedthread r;         // synchronize copy of connectedthread         synchronized (this) {              r = mconnectedthread;         }         // perform write unsynchronized         r.write(out);      }  private class connectthread extends thread {      private final bluetoothsocket mmsocket;     private final bluetoothdevice mmdevice;       public connectthread(bluetoothdevice device) {         // use temporary object later assigned mmsocket,         // because mmsocket final         bluetoothsocket tmp = null;         mmdevice = device;         bconnectthread = true;          // bluetoothsocket connect given bluetoothdevice         try {             // my_uuid app's uuid string, used server code             tmp = device.createrfcommsockettoservicerecord(my_uuid);         } catch (ioexception e) { }         mmsocket = tmp;     }      public void run() {         // cancel discovery because slow down connection         mbluetoothadapter = bluetoothadapter.getdefaultadapter();         mbluetoothadapter.canceldiscovery();        brun=true;         try {             // connect device through socket. block             // until succeeds or throws exception             mmsocket.connect();         } catch (ioexception connectexception) {             // unable connect; close socket , out             try {                 mmsocket.close();             } catch (ioexception closeexception) { }             return;         }          // work manage connection (in separate thread)         //bconnectedthreadstart = true;         connected(mmsocket);      }      /** cancel in-progress connection, , close socket */     public void cancel() {         try {             mmsocket.close();         } catch (ioexception e) { }     } }   private class connectedthread extends thread {     private final bluetoothsocket mmsocket;     private final inputstream mminstream;     private final outputstream mmoutstream;      public connectedthread(bluetoothsocket socket) {         mmsocket = socket;         inputstream tmpin = null;         outputstream tmpout = null;         bconnectedthread=true;          // input , output streams, using temp objects because         // member streams final         try {             tmpin = socket.getinputstream();             tmpout = socket.getoutputstream();         } catch (ioexception e) { }          mminstream = tmpin;         mmoutstream = tmpout;     }      public void run() {         byte[] buffer = new byte[1024];  // buffer store stream         int bytes; // bytes returned read()          // keep listening inputstream until exception occurs         while (true) {             try {                 // read inputstream                 bytes = mminstream.read(buffer);                 // send obtained bytes ui activity                // mhandler.obtainmessage(message_read, bytes, -1, buffer)                   //      .sendtotarget();             } catch (ioexception e) {                 break;             }         }     }      /* call main activity send data remote device */     public void write(byte[] bytes) {         try {             mmoutstream.write(bytes);         } catch (ioexception e) { }     }      /* call main activity shutdown connection */     public void cancel() {         try {             mmsocket.close();         } catch (ioexception e) { }     } }   public synchronized void connect(bluetoothdevice device)   {       bconnectthreadstart=true;     mconnectthread = new connectthread(device);     mconnectthread.start();  }   public synchronized void connected(bluetoothsocket socket) {      bconnectedsynchronized=true;     mconnectedthread = new connectedthread(socket);     mconnectedthread.start();   }}    logcat     05-21 17:49:29.902: w/dalvikvm(25720): threadid=1: thread exiting uncaught exception (group=0x417da498)     05-21 17:49:29.912: e/androidruntime(25720): fatal exception: main     05-21 17:49:29.912: e/androidruntime(25720): java.lang.illegalstateexception: not execute method of activity     05-21 17:49:29.912: e/androidruntime(25720):    @ android.view.view$1.onclick(view.java:3676)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.view.view.performclick(view.java:4171)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.view.view$performclick.run(view.java:17070)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.os.handler.handlecallback(handler.java:615)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.os.handler.dispatchmessage(handler.java:92)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.os.looper.loop(looper.java:137)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.app.activitythread.main(activitythread.java:4797)     05-21 17:49:29.912: e/androidruntime(25720):    @ java.lang.reflect.method.invokenative(native method)     05-21 17:49:29.912: e/androidruntime(25720):    @ java.lang.reflect.method.invoke(method.java:511)     05-21 17:49:29.912: e/androidruntime(25720):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:804)     05-21 17:49:29.912: e/androidruntime(25720):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:571)     05-21 17:49:29.912: e/androidruntime(25720):    @ dalvik.system.nativestart.main(native method)     05-21 17:49:29.912: e/androidruntime(25720): caused by: java.lang.reflect.invocationtargetexception     05-21 17:49:29.912: e/androidruntime(25720):    @ java.lang.reflect.method.invokenative(native method)     05-21 17:49:29.912: e/androidruntime(25720):    @ java.lang.reflect.method.invoke(method.java:511)     05-21 17:49:29.912: e/androidruntime(25720):    @ android.view.view$1.onclick(view.java:3671)     05-21 17:49:29.912: e/androidruntime(25720):    ... 11 more     05-21 17:49:29.912: e/androidruntime(25720): caused by: java.lang.nullpointerexception     05-21 17:49:29.912: e/androidruntime(25720):    @ com.example.elevator.connectingdevices.write(connectingdevices.java:40)     05-21 17:49:29.912: e/androidruntime(25720):    @ com.example.elevator.mainactivity.onclickbtnetage1(mainactivity.java:63)     05-21 17:49:29.912: e/androidruntime(25720):    ... 14 more     05-21 17:49:49.062: i/system.out(27064): debugger has connected     05-21 17:49:49.062: i/system.out(27064): waiting debugger settle...     05-21 17:49:49.262: i/system.out(27064): waiting debugger settle...     05-21 17:49:49.472: i/system.out(27064): waiting debugger settle...     05-21 17:49:49.663: i/system.out(27064): waiting debugger settle...     05-21 17:49:49.863: i/system.out(27064): waiting debugger settle...     05-21 17:49:50.063: i/system.out(27064): waiting debugger settle...     05-21 17:49:50.263: i/system.out(27064): waiting debugger settle...     05-21 17:49:50.474: i/system.out(27064): debugger has settled (1455)     05-21 17:49:50.684: w/resourcetype(27064): no package identifier when getting value resource number 0x00000000     05-21 17:49:50.684: w/packagemanager(27064): failure retrieving resources forcom.example.elevator: resource id #0x0     05-21 17:49:50.944: d/iconcustomizer(27064): generate customized icon com.example.elevator.png     05-21 17:49:50.984: w/iconcustomizer(27064): can't load transform_config.xml     05-21 17:49:51.164: i/themeservice(27064): add pending job /data/data/com.example.elevator/cache/com.example.elevator.png     05-21 17:49:51.174: i/themeservice(27064): binding service     05-21 17:49:51.344: i/themeservice(27064): service connected     05-21 17:49:51.344: i/themeservice(27064): saving icon /data/data/com.example.elevator/cache/com.example.elevator.png     05-21 17:49:51.415: i/adreno200-egl(27064): <qegldrvapi_eglinitialize:294>: egl 1.4 qualcomm build:  (cl3068996)     05-21 17:49:51.415: i/adreno200-egl(27064): build date: 03/07/13 thu     05-21 17:49:51.415: i/adreno200-egl(27064): local branch: au37     05-21 17:49:51.415: i/adreno200-egl(27064): remote branch:      05-21 17:49:51.415: i/adreno200-egl(27064): local patches:      05-21 17:49:51.415: i/adreno200-egl(27064): reconstruct branch:      05-21 17:49:52.346: i/themeservice(27064): unbinding service     05-21 17:50:36.903: i/connectingdevices(27064): connectthread function started     05-21 17:50:36.903: i/connectingdevices(27064): connectthread working     05-21 17:50:36.913: i/connectingdevices(27064): connectthread running     05-21 17:50:36.953: e/connectingdevices(27064): mmsocket unable connect     05-21 17:50:36.953: e/connectingdevices(27064): java.io.ioexception: unable start service discovery     05-21 17:50:36.953: e/connectingdevices(27064):     @ android.bluetooth.bluetoothsocket$sdphelper.dosdp(bluetoothsocket.java:455)     05-21 17:50:36.953: e/connectingdevices(27064):     @ android.bluetooth.bluetoothsocket.connect(bluetoothsocket.java:230)     05-21 17:50:36.953: e/connectingdevices(27064):     @ com.example.elevator.connectingdevices$connectthread.run(connectingdevices.java:77) 


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -