2015-12-08 60 views
2

我从网上得到连接ELM 327到Android(蓝牙) - 获取UUID

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开发的新手。

回答

1

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

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.