2015-12-08 54 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.