How to create a bluetooth server socket in android? -


i trying create server program starts bluetooth, creates server socket, waits device connect , accepts connection.

the onclick() method starts bluetooth, call acceptthread() method create server socket , start listening. run() called accepts connection.

but not working. app stops. idea why?

the code given below:

public class mainactivity extends activity {      public bluetoothadapter mbluetoothadapter;     private bluetoothserversocket mmserversocket;     private static final uuid my_uuid_secure = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");      textview text;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         text=(textview)findviewbyid(r.id.textview1);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.main, menu);         return true;     }      public void onclick(view view) {         switch (view.getid()) {         case r.id.button1:              mbluetoothadapter = bluetoothadapter.getdefaultadapter();             if (mbluetoothadapter == null) {                 text.settext("does not support bluetooth");                 return;             }              intent discoverableintent = new             intent(bluetoothadapter.action_request_discoverable);             discoverableintent.putextra(bluetoothadapter.extra_discoverable_duration, 300);             startactivity(discoverableintent);             text.settext("discoverable!!");              acceptthread();             run();          }     }      public void changet(string str)     {         text.settext(str);     }      public void acceptthread() {         bluetoothserversocket tmp = null;         try {             tmp = mbluetoothadapter.listenusingrfcommwithservicerecord("myyapp", my_uuid_secure);          } catch (ioexception e) { }         mmserversocket = tmp;     }      public void run() {         bluetoothsocket socket = null;         while (true) {             try {                 socket = mmserversocket.accept();                 changet("listening");             } catch (ioexception e) {                 break;             }             if (socket != null) {                 changet("doneeeee");                 try {                     mmserversocket.close();                 } catch (ioexception e) {                     e.printstacktrace();                 }                 break;             }         }     }    } 

the layout requested:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context=".mainactivity" >      <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparentleft="true"         android:layout_alignparenttop="true"         android:layout_marginleft="60dp"         android:layout_margintop="31dp"         android:text="@string/but"          android:onclick="onclick"/>      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_alignright="@+id/button1"         android:layout_marginright="43dp"         android:layout_margintop="15dp"         android:text="@string/output" />  </relativelayout> 

the problem acceptthread() , run() functions running before adapter turned on. single line before acceptthread() solved this.

while(mmserversocket==null); 

also, run() has run in different thread.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -