如何创建android的蓝牙服务器套接字?蓝牙、服务器、android

2023-09-06 23:12:15 作者:15.想看海

我想创建一个刚刚起步的蓝牙,创建一个服务器套接字,等待一些设备连接,并接受连接一个服务器程序。

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

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

在code为如下:

 公共类MainActivity延伸活动{

    公共BluetoothAdapter mBluetoothAdapter;
    私人BluetoothServerSocket mmServerSocket;
    私有静态最后UUID MY_UUID_SECURE = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);

    TextView的文字;
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        文=(TextView的)findViewById(R.id.textView1);
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    公共无效的onClick(视图查看){
        开关(view.getId()){
        案例R.id.button1:

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            如果(mBluetoothAdapter == NULL){
                text.setText(不支持蓝牙);
                返回;
            }

            意图discoverableIntent =新
            意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
            startActivity(discoverableIntent);
            text.setText(可见!!);

            AcceptThread();
            跑();

        }
    }

    公共无效changeT(字符串str)
    {
        text.setText(STR);
    }

    公共无效AcceptThread(){
        BluetoothServerSocket TMP = NULL;
        尝试 {
            TMP = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(MYYAPP,MY_UUID_SECURE)​​;

        }赶上(IOException异常E){}
        mmServerSocket = TMP;
    }

    公共无效的run(){
        的BluetoothSocket插座= NULL;
        而(真){
            尝试 {
                插座= mmServerSocket.accept();
                changeT(听);
            }赶上(IOException异常E){
                打破;
            }
            如果(插座!= NULL){
                changeT(doneeeee);
                尝试 {
                    mmServerSocket.close();
                }赶上(IOException异常E){
                    e.printStackTrace();
                }
                打破;
            }
        }
    }
}
 

作为请求的布局:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=MainActivity。>

    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =60dp
        机器人:layout_marginTop =31dp
        机器人:文本=@字符串/而是
        机器人:的onClick =的onClick/>

    <的TextView
        机器人:ID =@ + ID / textView1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignRight =@ + ID /按钮1
        机器人:layout_marginRight =43dp
        机器人:layout_marginTop =15dp
        机器人:文本=@字符串/输出/>

< / RelativeLayout的>
 

解决方案

问题是AcceptThread()和run()函数中运行适配器可以开启之前。 在AcceptThread前单线()解决了这个。

 而(mmServerSocket == NULL);
 
使用NanoHTTPD在Android上建立本地服务器

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

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.

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?

The code is 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 as 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 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);

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

 
精彩推荐
图片推荐