Can someone help me read OBD2 DTC codes from my Xamarin.Android project?

Hi everyone.
I am struggling to implement code that helps me read DTC codes via Bluetooth from OBD2 for cars....

All I want to do is be able to plug in a Bluetooth scanner, and read fault codes and display them in my app.

So far I have been able to connect to a Bluetooth device and open up a socket. I don't know what to do after.
installed the Strados package, but I don't know how to use it......

Can anyone help??

Here is what I have so far:

    EditText InputText = FindViewById<EditText>(Resource.Id.txtOBDInput);
            BluetoothSocket btSocket = null;   


            BluetoothDevice device = (from bd in bluetoothAdapter.BondedDevices
                                      where bd.Name == InputText.Text.Trim()
                                      select bd).FirstOrDefault();

            if (device == null)
            {
                Android.Widget.Toast.MakeText(this, "Device not found.", Android.Widget.ToastLength.Short).Show();
            }
            else
            {
                Android.Widget.Toast.MakeText(this, "Device bond state: " + device.BondState.ToString() + ". Device Address: " + device.Address.ToString() + ". Device bluetooth class: " + device.BluetoothClass.ToString() 
                    + ". Device UUID: " + device.GetUuids()[0].Uuid, Android.Widget.ToastLength.Short).Show();

                DEVICE_UUID = device.GetUuids()[0].Uuid;
            }

            btSocket = device.CreateRfcommSocketToServiceRecord(DEVICE_UUID);
            btSocket.Connect();
            Android.Widget.Toast.MakeText(this, "Socket connection successful.", Android.Widget.ToastLength.Short).Show();

Answers

  • asusmaniasusmani Member ✭✭

    Maybe I need some help.
    I took a little from what you shared and ended up with:

    How can i assign the data from Strados to the inputstream? Or am I approaching this wrong all together?

        private void CheckBTButton_Click(object sender, EventArgs e)
        {
            EditText InputText = FindViewById<EditText>(Resource.Id.txtOBDInput);
            BluetoothSocket btSocket = null;            
    
            BluetoothDevice device = (from bd in bluetoothAdapter.BondedDevices
                                      where bd.Name == InputText.Text.Trim()
                                      select bd).FirstOrDefault();
    
            if (device == null)
            {
                Android.Widget.Toast.MakeText(this, "Device not found.", Android.Widget.ToastLength.Short).Show();
            }
            else
            {
                Android.Widget.Toast.MakeText(this, "Device bond state: " + device.BondState.ToString() + ". Device Address: " + device.Address.ToString() + ". Device bluetooth class: " + device.BluetoothClass.ToString()
                    + ". Device UUID: " + device.GetUuids()[0].Uuid, Android.Widget.ToastLength.Short).Show();
    
                DEVICE_UUID = device.GetUuids()[0].Uuid;
            }            
        //How do I assign the OBD values to the inputstream?
       //Strados.Obd.Specification.MonitorStatus a = new MonitorStatus();
        //String to inputstream = a.CheckEngineLightOn
    
    
            btSocket = device.CreateRfcommSocketToServiceRecord(DEVICE_UUID);
            if (btSocket.IsConnected)
            {
                btSocket.Connect();
                Android.Widget.Toast.MakeText(this, "Socket connection successful.", Android.Widget.ToastLength.Short).Show();
    
    
                var mReader = new InputStreamReader(btSocket.InputStream);
                var buffer = new BufferedReader(mReader);
                String barcode = buffer.ReadLine();
                if (barcode.Length > 0)
                {
                    System.Diagnostics.Debug.WriteLine("Reading from device....");
                    //Xamarin.Forms.MessagingCenter.Send<Apptring>((App)Xamarin.Forms.Application.Current, "Barcode", barcode);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("No data");
                }
            }            
        }
    
  • JarvanJarvan Member, Xamarin Team Xamurai

    Tutorial and sample about the feature on native Andorid, hope it'll help you.
    https://github.com/HellaVentures/Android-OBD-Example-App

  • asusmaniasusmani Member ✭✭

    Thanks. How do I actually integrate it into my Xamarin Android project though? I was thinking I can just add a Nuget package, but i don't think those libraries are available....Do i have to manually pull in all the classes that would be needed?

  • asusmaniasusmani Member ✭✭

    Hi guys,
    I was able to connect to my bluetooth OBD scanner.

    Can you help me with ONE mor thing?

    Can you tell me if i am missing anything when I actually send me command to the OBD2 scanner?
    My code is below. I basically call ConnectBluetooth just to connect the socket. If it connects, it returns a 0 else a 1 if theres an error.

    SO, once I am connected to the socket, i want to issue the command to the scanner and receive its response.
    Am I doing it correctly?
    Or am I missing any steps?

                if (btSocket != null)
                {
                    Task<string> SubmitTask = new Task<string>(ConnectBluetooth);
                    SubmitTask.Start();
                    //Progress.Visibility = ViewStates.Visible;
    
                    string Result = await SubmitTask;
    
                    if (Result == "0")
                    {
                        Android.Widget.Toast.MakeText(this, "Connection Successful!", Android.Widget.ToastLength.Short).Show();
                        String CommandToOBD = "010D" + "\r";
                        // convert string to stream
                        byte[] byteArray = Encoding.ASCII.GetBytes(CommandToOBD);
                        mmOutputStream  = new MemoryStream(byteArray);
                        mmOutputStream = btSocket.OutputStream;
    
    
                        mmInputStream = btSocket.InputStream;
    
                        StreamReader reader = new StreamReader(mmInputStream);
                        string data = reader.ReadToEnd();
                        //string data = "41 0D FF";           //hex data from OBD2
                        var result = ObdParser.Parse(data);
    
                        Console.WriteLine(result.Mode);     //mode: 1
                        Console.WriteLine(result.Command);  //command: 13
                        Console.WriteLine(result.Name);     //name: VehicleSpeed
                        Console.WriteLine(result.Value);    //value: 255 (kph)
                    }
    
Sign In or Register to comment.