如何在android中创建蓝牙服务器套接字? [英] How to create a bluetooth server socket in android?

查看:20
本文介绍了如何在android中创建蓝牙服务器套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a server program that just starts bluetooth, creates a server socket, waits for some device to connect and accepts the connection.

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

The onClick() method starts the bluetooth, then I call the AcceptThread() method to create a server socket and start listening. Then run() is called which accepts a connection.

但是它不起作用.我的应用程序就停止了.知道为什么吗?

But it is not working. My app just stops. Any idea why?

代码如下:

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() 之前的一行解决了这个问题.

The problem was the AcceptThread() and run() functions were running before the adapter could be turned on. A single line before the AcceptThread() solved this.

while(mmServerSocket==null);

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

Also, run() has to be run in a different thread.

这篇关于如何在android中创建蓝牙服务器套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆