sockets - Android bluetooth connection cannot be established on the first try -


i'm trying connect bluetooth device in android application have problem. seems i'm never able connect bluetooth device on first try.

i have following code in bluetoothconnectthread:

public class bluetoothconnectthread extends thread {     private bluetoothsocket mmsocket;     private bluetoothdevice mmdevice;     private context context;     private bluetoothmanager manager;      public bluetoothconnectthread(bluetoothdevice mmdevice, uuid uuid, context context, bluetoothmanager manager) {         this.context = context;         this.manager = manager;         this.mmdevice = mmdevice;         this.uuid = uuid;     }      public void run() {         try {             system.out.println("try connect");             mmsocket = (bluetoothsocket) mmdevice.getclass().getmethod("createrfcommsocket", new class[]{int.class}).invoke(mmdevice, integer.valueof(1));             mmsocket.connect();         } catch (exception connectexception) {             connectexception.printstacktrace();             try {                 mmsocket.close();                 system.out.println("couldn't establish bluetooth connection! (1)");             } catch (ioexception closeexception) {                 closeexception.printstacktrace();                 system.out.println("couldn't establish bluetooth connection! (2)");             }              try {                 system.out.println("try connect again");                 mmsocket = (bluetoothsocket) mmdevice.getclass().getmethod("createrfcommsocket", new class[]{int.class}).invoke(mmdevice, integer.valueof(1));                 mmsocket.connect();             } catch (exception connectexception2) {                 connectexception.printstacktrace();                 try {                     mmsocket.close();                     system.out.println("couldn't establish bluetooth connection! (3)");                 } catch (ioexception closeexception) {                     closeexception.printstacktrace();                     system.out.println("couldn't establish bluetooth connection! (4)");                 }             }         }          if(mmsocket.isconnected()) {             if(mmsocket.isconnected()) {                 system.out.println("connected");     //do connected socket 

i receive following log when calling run method:

07-12 14:17:10.906 9941-10518/com.example.niekdewit.test i/system.out: try connect 07-12 14:17:10.910 9941-10518/com.example.niekdewit.test w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/system.err: java.io.ioexception: read failed, socket might closed or timeout, read ret: -1 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.readall(bluetoothsocket.java:573) 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.waitsocketsignal(bluetoothsocket.java:550) 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.connect(bluetoothsocket.java:325) 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/system.err:     @ com.example.niekdewit.test.bluetoothconnectthread.run(bluetoothconnectthread.java:33) 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test i/system.out: couldn't establish bluetooth connection! (1) 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test i/system.out: try connect again 07-12 14:17:13.025 9941-10518/com.example.niekdewit.test w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback 07-12 14:17:13.147 9941-9941/com.example.niekdewit.test i/timeline: timeline: activity_idle id: android.os.binderproxy@2e01eda7 time:762005544 07-12 14:17:16.503 9941-9941/com.example.niekdewit.test i/timeline: timeline: activity_idle id: android.os.binderproxy@2e01eda7 time:762008901 07-12 14:17:17.279 9941-10518/com.example.niekdewit.test i/system.out: connected 

i tried replacing 2 lines

mmsocket = (bluetoothsocket) mmdevice.getclass().getmethod("createrfcommsocket", new class[]{int.class}).invoke(mmdevice, integer.valueof(1)); 

with

 mmsocket = mmdevice.createrfcommsockettoservicerecord(uuid); 

but doesn't work either, generates different log though. using method i'm not able connect on second try.

07-12 14:27:15.968 15135-15622/com.example.niekdewit.test i/system.out: try connect 07-12 14:27:15.969 15135-15622/com.example.niekdewit.test w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test w/system.err: java.io.ioexception: read failed, socket might closed or timeout, read ret: -1 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.readall(bluetoothsocket.java:573) 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.waitsocketsignal(bluetoothsocket.java:550) 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.connect(bluetoothsocket.java:325) 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test w/system.err:     @ com.example.niekdewit.test.bluetoothconnectthread.run(bluetoothconnectthread.java:36) 07-12 14:27:18.205 15135-15622/com.example.niekdewit.test i/system.out: couldn't establish bluetooth connection! (1) 07-12 14:27:18.206 15135-15622/com.example.niekdewit.test i/system.out: try connect again 07-12 14:27:18.206 15135-15622/com.example.niekdewit.test w/bluetoothadapter: getbluetoothservice() called no bluetoothmanagercallback 07-12 14:27:18.319 15135-15135/com.example.niekdewit.test i/timeline: timeline: activity_idle id: android.os.binderproxy@2e01eda7 time:762610717 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test w/system.err: java.io.ioexception: read failed, socket might closed or timeout, read ret: -1 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.readall(bluetoothsocket.java:573) 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.waitsocketsignal(bluetoothsocket.java:550) 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test w/system.err:     @ android.bluetooth.bluetoothsocket.connect(bluetoothsocket.java:325) 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test w/system.err:     @ com.example.niekdewit.test.bluetoothconnectthread.run(bluetoothconnectthread.java:36) 07-12 14:27:20.669 15135-15622/com.example.niekdewit.test i/system.out: couldn't establish bluetooth connection! (3) 07-12 14:27:20.854 15135-15135/com.example.niekdewit.test i/timeline: timeline: activity_idle id: android.os.binderproxy@2e01eda7 time:762613252 

when first try connect line

 mmsocket = mmdevice.createrfcommsockettoservicerecord(uuid); 

and try on second try ("try connect again") line

mmsocket = (bluetoothsocket) mmdevice.getclass().getmethod("createrfcommsocket", new class[]{int.class}).invoke(mmdevice, integer.valueof(1)); 

then generates same log first log attached

i have no idea going on? hope 1 of can see wrong code, or point me in right direction.

edit: when device paired possible connect on first try using both methods.

edit2:

mmsocket = mmdevice.createinsecurerfcommsockettoservicerecord(uuid); 

also not work

i try change

mmsocket = (bluetoothsocket) mmdevice.getclass().getmethod("createrfcommsocket", new class[]{int.class}).invoke(mmdevice, integer.valueof(1)); 

to

    // bluetoothdevice device= bluetoothadapter.getdefaultadapter().getremotedevice("00:1c:4d:02:a6:55");     // have device capture method invoke socket     method m = device.getclass().getmethod("createrfcommsocket", new class[] { int.class });     socket = (bluetoothsocket)m.invoke(device, integer.valueof(1));     socket.connect();     // cool stuff 

this how connect devices


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -