android - Connecting to a bluetooth device programatically -


i writing program new vehicle security app. app allows user control lock/unlock operations via phone app. lets user's phone bluetooth switched off @ first. if that's case, when opens app, phone bluetooth adapter should automatically switched on , should connect bluetooth module fixed in vehicle. according code have done, programatic enabling of bt adapter of phone works fine. connection vehicle bt module not happen.

but if user opens app while phone bt adapter switched on, connection establishing between vehicle , phone happens automatically.

i need know why connection not happen when bt adapter turned on programatically.

note - phone , vehicle bt module paired. bluetooth modules mac address hard coded in coding. coding follows. pasted necessary parts. hope every needed understand , solve problem here. way posted code pretty messed up. sorry that. hope it's clear. i'm new this.

    private static final uuid my_uuid =       uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");    // insert bluetooth devices mac address   private static string address = "00:19:5d:ef:03:79";     /** called when activity first created. */   @override   public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);    setcontentview(r.layout.main);    btadapter = bluetoothadapter.getdefaultadapter();     btadapter.enable();     @override   public void onresume() {     super.onresume();      btadapter.enable();       // set pointer remote node using it's address.     bluetoothdevice device = btadapter.getremotedevice(address);      // 2 things needed make connection:     //   mac address, got above.     //   service id or uuid.  in case using     //     uuid spp.       try {       btsocket = device.createrfcommsockettoservicerecord(my_uuid);     } catch (ioexception e) {       errorexit("fatal error", "in onresume() , socket create failed: " + e.getmessage() + ".");     }      // make sure discovery isn't going on when attempt connect , pass message.      btadapter.canceldiscovery();      // establish connection.  block until connects.      try {       btsocket.connect();      } catch (ioexception e) {       try {         btsocket.close();       } catch (ioexception e2) {         errorexit("fatal error", "in onresume() , unable close socket during connection failure" + e2.getmessage() + ".");       }     }      // create data stream can talk server.      try {       outstream = btsocket.getoutputstream();     } catch (ioexception e) {       errorexit("fatal error", "in onresume() , output stream creation failed:" + e.getmessage() + ".");     }   } 

there might timing problem, oncreate , onresume called in short order. in case bt not enabled code in onresume might called before bt service online.

my advice: try delay initiation few seconds putting code in runnable.

private handler mhandler = new handler();  public void oncreate() {       [...]       mhandler.postdelayed(new runnable() {          @override          public void run() {              btadapter.enable();               // set pointer remote node using it's address.              bluetoothdevice device = btadapter.getremotedevice(address);              // 2 things needed make connection:              //   mac address, got above.              //   service id or uuid.  in case using              //     uuid spp.              try {                  btsocket = device.createrfcommsockettoservicerecord(my_uuid);              } catch (ioexception e) {                  errorexit("fatal error", "in onresume() , socket create failed: " +                  e.getmessage() + ".");                              }               // make sure discovery isn't going on when attempt connect , pass message.               btadapter.canceldiscovery();               // establish connection.  block until connects.               try {                btsocket.connect();               } catch (ioexception e) {                try {                  btsocket.close();                } catch (ioexception e2) {                  errorexit("fatal error", "in onresume() , unable close socket during connection failure" + e2.getmessage() + ".");                }              }               // create data stream can talk server.               try {                outstream = btsocket.getoutputstream();              } catch (ioexception e) {                errorexit("fatal error", "in onresume() , output stream creation failed:" + e.getmessage() + ".");              }      }, 5000); // 5 second delay       [...] 

caveats: works bad if exit app promptly after startup. put runnable in member variable , call mhandler.removecallback(runnable) in ondestroy().


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -