android - 如何在android中创建蓝牙服务器套接字?

标签 android bluetooth serversocket

我正在尝试创建一个服务器程序,它只启动蓝牙、创建服务器套接字、等待某个设备连接并接受连接。

onClick() 方法启动蓝牙,然后调用 AcceptThread() 方法创建服务器套接字并开始监听。然后调用 run() 接受连接。

但它不起作用。我的应用程序刚刚停止。知道为什么吗?

代码如下:

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>

最佳答案

问题是 AcceptThread()run() 函数在适配器打开之前运行。 AcceptThread() 之前的一行解决了这个问题。

while(mmServerSocket==null);

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

关于android - 如何在android中创建蓝牙服务器套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16676165/

相关文章:

bluetooth - bluez 同时具有经典和低能耗设备

ios - iPad 作为使用蓝牙的 Mac 键盘

c++ - 套接字动态绑定(bind)到缩小范围

android - 选项卡中的图标不显示!!!我应该怎么办?

java - 以编程方式添加时如何为 GridLayout 子项设置 layout_columnWeight?

android - 在 RecyclerView android 中拖放期间 Position 值面临问题

android - BluetoothChat 示例,无法识别丢失的连接

Android:蓝牙连接、套接字和线程

c++ - 服务器套接字 - 仅接受来自白名单中 IP 地址的连接

java - android.widget.Toolbar 和 android.support.v7.widget.Toolbar 有什么区别?