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?

Sign In or Register to comment.