Android蓝牙SPP服务器

时间:2016-04-07 09:31:23

标签: android bluetooth server spp

我是Android编程的新手,现在我遇到了问题。 我想用我的Android手机作为蓝牙服务器,这意味着当我打开一个特殊的Activity时,手机应该听其他蓝牙设备(已经配对),而其他设备可以打开我的Android手机的连接。所以它应该像电话是一个外围设备,就像一个bt扬声器。 所以配对仍然完成。并且发现我必须使用SPP模式。当它连接时我想发送一个接收简单的字节流。 我找到了一个名为'bluetooth spp tools pro'的应用程序(https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=de),它可以完成我想要的任何操作。但问题是手机充当客户端并打开连接。

所以也许你可以帮助我并给我一些提示,以了解我必须做的事情。

感谢您的帮助:)

编辑:现在我尝试了Android的示例,看起来它几乎正常工作。我可以将我的电脑与智能手机连接,智能手机与它连接。但是当它连接时,应用程序崩溃,我不知道为什么...... 也许你可以告诉我为什么ma app崩溃..这里是代码:

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) { }
    }
}

}

1 个答案:

答案 0 :(得分:2)

创建RFCOMM套接字(也称为串行端口配置文件(SPP))并侦听传入数据。

Android有API用于此目的,使用BluetoothServerSocket并收听传入数据。

可以找到BluetoothServerSocket使用的示例here