使用Xamarin的Visual Studio(C#)中的OBD II应用程序

我正在开发一款从OBD II蓝牙适配器接收数据的Android应用程序。 设置应用程序和适配器之间的连接工作,但是当我发送命令到适配器我不会得到正确的结果。 它要么返回:

+连接<

或我刚刚发送给适配器的命令。

该应用程序在Visual Studio中使用Xamarin进行编码以针对Android进行开发。 该代码仅显示应用程序的简化版本,只是在01-1f之间发布支持的PID的代码。

BluetoothAdapter myAdapter;
BluetoothSocket socket = null; 
string rawData;
public BluetoothConnect()
    {

        myAdapter = BluetoothAdapter.DefaultAdapter;
        if (myAdapter == null)
        {
            //Device has no Bluetooth
        }
        if (!myAdapter.IsEnabled)
        {
            myAdapter.Enable();
        }
        BluetoothDevice d = myAdapter.GetRemoteDevice("AA:BB:CC:11:22:33");
        socket = d.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B34FB"));
        socket.Connect();

        byte[] cmd = Encoding.ASCII.GetBytes("AT D");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT Z");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT E0");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT L0");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT S0");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT H0");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();

        cmd = Encoding.ASCII.GetBytes("AT SP 0");
        socket.OutputStream.Write(cmd, 0, cmd.Length);
        ReadAnswer();
        socket.OutputStream.Flush();




        cmd = Encoding.ASCII.GetBytes("0100");
        socket.OutputStream.Write(cmd, 0, cmd.Length);

        ReadAnswer();


    }

    private void ReadAnswer() {
        try {
            rawData = "";
            int a = 0;

            System.Text.StringBuilder b = new   System.Text.StringBuilder();
            char c;
            while (((a = (byte)socket.InputStream.ReadByte()) > -1)) 
            {

                c = (char)a;
                if (c == '>')
                {
                    break;
                }
                b.Append(c);
            }

            rawData = b.ToString();
            Log.Info("-----------------------------------", "RawData: " + rawData);
            socket.InputStream.Flush();
        }
        catch(System.Exception e)
        {
            Log.Info("", "" + e.Message);
        }



    } 

我已经尝试了初始化命令的不同组合。

我可以想象,这个问题必须通过阅读输入流或发送命令的方法来完成,但我不知道该怎么做,因为我是Visual Studio,Xamarin和Android开发人员的新手。

希望你们能给我一些建议!

链接地址: http://www.djcxy.com/p/95123.html

上一篇: OBD II App in Visual Studio(C#) with Xamarin

下一篇: Android Bluetooth OBDII communication