2013-05-21 62 views
3

我想创建一个服务器程序,只是开始蓝牙,创建一个服务器套接字,等待一些设备连接并接受连接。如何在android中创建蓝牙服务器套接字?

的onclick()方法启动蓝牙,然后我打电话AcceptThread()方法创建一个服务器套接字,开始收听。然后运行()被调用,它接受连接。

但它不工作。我的应用程序停止。任何想法为什么?

的代码如下:

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

布局的要求:

<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> 
+0

它是否真的进入onClick方法?发布相关布局 – Elior

+0

添加了布局。 –

+0

它是否进入onClick方法或您在switch语句中写入的情况? – Elior

回答

5

问题是AcceptThread()run()职能运行适配器可以开启之前。 AcceptThread()之前的一行代码解决了这个问题。

while(mmServerSocket==null); 

此外,run()必须运行在不同的线程。