I have a Bluetooth OBDII dongle for my car (the brand is Veepeak), and I'm trying to write a Windows app that can communicate with it. So far it seems that I'm able to connect to the device from my laptop, send commands, and receive some sort of response, but the responses I receive are not what I expect. I am using the 32feet communication library to handle the Bluetooth stuff.
Here is the code I am using to connect and also the functions I am using to send messages:
BluetoothClient client;
Stream stream;
client = new BluetoothClient();
Guid uuid = new Guid("00001101-0000-1000-8000-00805f9b34fb");
client.BeginConnect(SelectedDevice.DeviceAddress, uuid, bluetoothClientConnectCallback, client);
private void bluetoothClientConnectCallback(IAsyncResult result)
{
client = (BluetoothClient)result.AsyncState;
client.EndConnect(result);
clientConnected = true;
stream = client.GetStream();
UIWriteLine("Client connected");
}
private string sendMessage(string message)
{
byte[] encodedMessage = Encoding.ASCII.GetBytes(message);
stream.Write(encodedMessage, 0, encodedMessage.Length);
Thread.Sleep(100);
int count = 0;
byte[] buffer = new byte[1024];
string retVal = string.Empty;
count = stream.Read(buffer, 0, buffer.Length);
retVal += Encoding.ASCII.GetString(buffer, 0, count);
return retVal.Replace("\n", "");
}
private string getValue(string pid)
{
byte[] encodedMessage = Encoding.ASCII.GetBytes(pid + "\r");
stream.Write(encodedMessage, 0, encodedMessage.Length);
Thread.Sleep(100);
bool cont = true;
int count = 0;
byte[] buffer = new byte[1024];
string retVal = string.Empty;
while (cont)
{
count = stream.Read(buffer, 0, buffer.Length);
retVal += Encoding.ASCII.GetString(buffer, 0, count);
if (retVal.Contains(">"))
{
cont = false;
}
}
return retVal.Replace("\n", "");
}
I use the sendMessage method to send AT commands, and the getValue method to get a specific PID (these methods are borrowing code from an OBDII library I found here).
When I send AT commands, I seem to only get an echo of whatever I send, and when I send PIDs, I get a response of a single question mark, which to my understanding means the command is invalid.
Is it possible that my dongle does not have an ELM327? Am I doing something wrong with my Bluetooth communication or is my UUID wrong? Thanks.