在 Android 应用程序中使用蓝牙向 Arduino 发送命令会出现异常(消息:无效的 UUID)

Sending commands to Arduino with Bluetooth in Android app gives exception (message: invalid UUID)

我将使用蓝牙向 Arduino 发送命令。我制作的应用程序是一个 Android 应用程序。下面代码的问题是最后一行给出了这个异常:

java.lang.IllegalArgumentException: Invalid UUID: 86663102937730

代码如下:

private void sendViaBluetooth() {

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "Bluetooth is not supported.", Toast.LENGTH_LONG);
    }
    else {
        Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();

        if (devices.size() != 0) {

            for (BluetoothDevice device : devices){

                if (device.getName().equals("HC-06")){
                    sendDataToPairedDevice(edtText.getText().toString(), device);
                    break;
                }
            }
        }
    }
}

private void sendDataToPairedDevice(String message ,BluetoothDevice device) {

    byte[] toSend = message.getBytes();
    try {
        TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        String uuid = tManager.getDeviceId();

        UUID applicationUUID = UUID.fromString(uuid); // <-- Gives IllegalArgumentException
        BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(applicationUUID);
        OutputStream mmOutStream = socket.getOutputStream();
        mmOutStream.write(toSend); // <-- gives NullPointerException (see update below)
    } 
    catch (IOException e) {
        Log.e("", "Exception during write", e);
    }
    catch (Exception ex){
        Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
        Log.e("", ex.toString());
    }
}

我也添加了这个权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

更新:

根据@neferpitou 的回答,我更新了这段代码:

UUID applicationUUID = UUID.fromString(uuid);

对此:

UUID applicationUUID = UUID.randomUUID();

它可以工作,但现在我的代码现在在带有注释的行上给出了 NullPointerException。变量 toSend 不为空,因此您可以在下图中看到。

有效的 UUID 如下所示 aae51ae6-c8c5-4805-9637-2722b683ca93

尝试将您的代码替换为:

UUID applicationUUID = UUID.randomUUID();

更新:

.write()只能处理1个字节的数据,但OutputStream中写Strings更简单。使用 BufferedWriter:

替换

mmOutStream.write(toSend);

BufferedWriter buffWriter = new BufferedWriter(new OutputStreamWriter(mmOutStream,"UTF-8"));
                buffWriter.write(message);
                buffWriter.flush();

最后在您的 try-catch 中创建一个 finally 块并关闭您的 buffWriter 例如buffWriter.close();

找到了! :) 这是我的代码

private void sendDataToPairedDevice(String message ,BluetoothDevice device) {

    try {
        myBluetooth = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(device.getAddress());
        btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(applicationUUID);//create a RFCOMM (SPP) connection
        BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
        btSocket.connect();

        btSocket.getOutputStream().write(message.getBytes());

    } catch (IOException e) {
        Log.e("", "Exception during write", e);
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}