安卓蓝牙SPP服务器

3
我是Android编程的新手,现在遇到了一个问题。我想将我的Android手机用作蓝牙服务器,也就是说当我打开特定的Activity时,手机应该监听其他已配对的蓝牙设备,并且其他设备可以连接到我的Android手机。因此,它应该像手机是外围设备一样,如蓝牙扬声器。
因此,配对仍然完成。我发现我必须使用SPP模式。当连接成功后,我想发送和接收简单的字节流。我找到了一个名为“bluetooth spp tools pro”的应用程序(https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=de),它几乎可以做到我想要的一切。但是这里的问题是手机作为客户端并打开连接。
因此,也许您可以帮助我并给我一些提示,以便我理解我需要做什么。
感谢您的帮助:)
编辑:现在我尝试了Android的一个示例,看起来它几乎可以工作。我可以将我的电脑与智能手机连接,并且智能手机会连接到它。但是当它连接成功后,应用程序崩溃了,我不知道为什么...也许您可以告诉我为什么我的应用程序崩溃了..这里是代码:
public class BluetoothServer extends AppCompatActivity {

public BluetoothAdapter mBluetoothAdapter;
private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private AcceptThread mSecureAcceptThread;

private TextView textViewStatus;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bluetooth_server);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    textViewStatus = (TextView) findViewById(R.id.BluetoothServerTextView);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mSecureAcceptThread.cancel();
    mSecureAcceptThread  = null;
}

public void onClickOpenBluetoothServer(View view) {

    // Setup Bluetooth Adapter
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    //Enable Discoverbility
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
    startActivity(discoverableIntent);
    textViewStatus.append("\nDiscoverable Added!");

    //Starting Accepting Thread
    mSecureAcceptThread = new AcceptThread();
    mSecureAcceptThread.start();
}


/**
 * Accepting Thread mit run();
 */
private class AcceptThread extends Thread {
    private final BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
        BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
            tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("PAKSAFE", MY_UUID_SECURE);
        } catch (IOException e) { }
        mmServerSocket = tmp;
        textViewStatus.append("\nAcceptThread Created!");
    }

    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                textViewStatus.append("\nListening");
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
                //manageConnectedSocket(socket);
                textViewStatus.setText("Acccepted");
                try {
                    mmServerSocket.close();
                } catch (IOException e) {
                    break;
                }
                break;
            }
        }
    }

    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }
}

}


原始问题与您更新的问题无关。将第二个问题移至新问题中。展示您尝试过什么,提供更多信息。在SO上以如此广泛的方式提问是违反规则的。 - deimus
1个回答

2
创建一个RFCOMM套接字(也称为串口配置文件(SPP)),并监听传入的数据。
Android有一个API用于此目的,使用BluetoothServerSocket并监听传入的数据。
可以在此处找到使用BluetoothServerSocket的示例。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接