Comment créer un serveur bluetooth support d'android?

Je suis en train de créer un programme serveur qui commence à s'bluetooth, crée un socket serveur, vous attend pour un appareil pour se connecter et accepte la connexion.

Le onClick() la méthode commence le bluetooth, puis-je appeler l'AcceptThread() la méthode pour créer un serveur de socket et de commencer à écouter. Puis run() est appelé, ce qui accepte une connexion.

Mais ça ne fonctionne pas. Mon application s'arrête juste. Aucune idée pourquoi?

Le code est donné ci-dessous:

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;
}
}
}   
}

La mise en page comme l'a demandé:

<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>
est-il vraiment entrer à la méthode onClick? post connexes de mise en page
ajout de la mise en page.
est il entre à la méthode onClick ou le cas que vous avez écrit dans l'instruction switch??

OriginalL'auteur Shubham Saini | 2013-05-21