3

i got the following code from the internet

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());
}

of course the uuid is a random code just for example, so how do i get the desired uuid ? should it be the uuid of my ELM327 device or another uuid? excuse me i am new to the uuid thing and android development.

Mike
  • 4,550
  • 4
  • 33
  • 47
walidsarkis
  • 99
  • 1
  • 1
  • 11
  • Does this answer your question? [Which GATT Profile and Services are used by OBD BLE Adapters like LELink, Automatic, Carista?](https://stackoverflow.com/questions/52075456/which-gatt-profile-and-services-are-used-by-obd-ble-adapters-like-lelink-automa) – DrMickeyLauer Oct 28 '21 at 10:17

2 Answers2

1

The UUID in this case isn't "random code" but the identifying key for the Bluetooth serial port service profile.

obdkey
  • 400
  • 3
  • 9
1

i just checked the UUID i used in a previous project and it is the same . i used it like this

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

    }

it was working perfectly when i developed the App.

but ELM327 device available in the market was problematic.some times in pairing.

perhaps you should use getAddress() function instead of bdDevice.toString()

getAddress will give the mac which will be unique to the device .

 public String getAddress ()
   Added in API level 5

   Returns the hardware address of this BluetoothDevice.

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

getRemoteDevice expects a device address

  getRemoteDevice(byte[] address)
   Get a BluetoothDevice object for the given Bluetooth hardware address. 
Neo
  • 1,359
  • 14
  • 34