如何使用 matlab 'Instrument Control'-Toolbox 通过蓝牙连接 android 设备?

How to connect android device via bluetooth with matlab 'Instrument Control'-Toolbox?

我想通过 蓝牙matlab 连接 android 设备用于在 matlab 和我自己的 android 应用程序之间交换数据。但是我无法通过 'Instrument Control'-Toolbox 与我的 android 设备连接。为什么?


首先,我扫描了所有可用的设备,然后尝试(使用 "connect" 按钮)与 android.

连接

我搜索了一下,上面写着:

所以我阅读了技术规范。从我的设备上,我找不到它们支持所需的 SPP 蓝牙配置文件。

但在 android 文档中它说:


所以我认为 android 本身支持 SPP,但我使用的设备不支持?
有没有办法通过蓝牙将其中一部手机与 matlab 连接?
哪些 android 设备在工作?

解决方案

这里'activate bluetooth spp in android'说:

  • 在 Android phone 上,您可能需要 运行 一个通过 SPP 启动服务的应用程序。

您需要侦听传入的连接请求,因此您应该使用此功能:

listenUsingRfcommWithServiceRecord(String, UUID)

在这里你可以找到一些例子:


代码示例

final Thread connect = new Thread(new Runnable() {
            @Override
            public void run() {
                BluetoothServerSocket serverSocket;

                BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                UUID sppUUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");

                        BluetoothSocket bluetoothSocket = null;

                        try {

                           serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord("your app name", sppUUID);
                           bluetoothSocket = serverSocket.accept(); // blocking call, until a connection is established.
                           Log.i("TAG", "serverSocket accept");

                        } catch (IOException e) {
                            Log.e("TAG", "IOException");
                        }

                        // If a connection was accepted
                        if (bluetoothSocket != null) {
                            // Do work to manage the connection (in a separate thread)
                            manageConnectedSocket(bluetoothSocket);
                        }

            }
        });
connect.start();

我的错误是认为我可以在没有自己的应用程序的情况下连接 matlab 和 android,只需使用设置中的 android 'bluetooth' 连接部分。