2

我从互联网上得到了以下代码

try {
    BluetoothDevice device = btAdapter.getRemoteDevice(bdDevice.toString());

    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    socket = device.createInsecureRfcommSocketToServiceRecord(uuid);
    socket.connect();
    Toast.makeText(MainActivity.this,"socket bonded", Toast.LENGTH_LONG).show();
} catch(Exception e) {
    tv.setText(e.toString());
}

当然,uuid 只是一个随机代码,那么我如何获得所需的 uuid 呢?它应该是我的 ELM327 设备的 uuid 还是另一个 uuid?对不起,我是 uuid 和 android 开发的新手。

4

2 回答 2

1

我刚刚检查了我在以前的项目中使用的 UUID,它是相同的。我是这样用的

try {
        mSocket = device.createRfcommSocketToServiceRecord(UUID
                .fromString("00001101-0000-1000-8000-00805F9B34FB"));
        mSocket.connect();
    } catch (IOException e) {

    }

当我开发应用程序时,它运行良好。

但市场上可用的 ELM327 设备存在问题。有时配对时。

也许你应该使用 getAddress() 函数而不是 bdDevice.toString()

getAddress将给出设备唯一的 mac。

 public String getAddress ()
   Added in API level 5

   Returns the hardware address of this BluetoothDevice.

   For example, "00:11:22:AA:BB:CC".

getRemoteDevice需要一个设备地址

  getRemoteDevice(byte[] address)
   Get a BluetoothDevice object for the given Bluetooth hardware address. 
于 2016-01-24T16:41:08.157 回答
1

这种情况下的 UUID 不是“随机码”,而是蓝牙串口服务配置文件的识别密钥。

于 2016-01-24T14:45:57.380 回答