将ELM 327连接到Android(蓝牙)-获取UUID
我从网上得到了以下代码将ELM 327连接到Android(蓝牙)-获取UUID,android,bluetooth,elm327,Android,Bluetooth,Elm327,我从网上得到了以下代码 try { BluetoothDevice device = btAdapter.getRemoteDevice(bdDevice.toString()); UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); socket = device.createInsecureRfcommSocketToServiceRecord(uuid); socket.
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开发。在这种情况下,uuid不是“随机代码”,而是蓝牙串行端口服务配置文件的识别密钥。我刚刚检查了我在以前的项目中使用的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.