Exemple #1
0
        public async Task <bool> Init(bool simulatormode = false)
        {
            running = true;
            //initialize _data
            data = new Dictionary <string, string> {
                { "vin", DefValue }
            };
            //VIN
            piDs = ObdExtension.GetPIDs();
            foreach (var v in piDs.Values)
            {
                data.Add(v, DefValue);
            }

            this.simulatormode = simulatormode;
            if (simulatormode)
            {
                PollObd();

                return(true);
            }
            bool isObdReaderAvailable = false;

            foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
                    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
                    {
                        var ipaddr = addrInfo.Address;
                        if (ipaddr.ToString().StartsWith("192.168.0"))
                        {
                            isObdReaderAvailable = true;
                            break;
                        }
                    }
                }
            }
            if (!isObdReaderAvailable)
            {
                socket    = null;
                running   = false;
                connected = false;
                return(false);
            }

            if (!ConnectSocket())
            {
                socket    = null;
                running   = false;
                connected = false;
                return(false);
            }


            if (connected)
            {
                //initialize the device
                string s;
                s = await SendAndReceive("ATZ\r");

                s = await SendAndReceive("ATE0\r");

                s = await SendAndReceive("ATL1\r");

                s = await SendAndReceive("ATSP00\r");

                PollObd();

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #2
0
        public async Task <bool> Init(bool simulatormode = false)
        {
            running = true;
            //initialize _data
            data = new Dictionary <string, string> {
                { "vin", DefValue }
            };
            //VIN
            piDs = ObdExtension.GetPIDs();

            foreach (var v in piDs.Values)
            {
                data.Add(v, DefValue);
            }

            this.simulatormode = simulatormode;
            if (simulatormode)
            {
                return(true);
            }

            bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
            if (bluetoothAdapter == null)
            {
                System.Diagnostics.Debug.WriteLine("Bluetooth is not available");
                return(false);
            }
            try
            {
                var ba = bluetoothAdapter.BondedDevices;
                foreach (var bd in ba)
                {
                    if (bd.Name.ToLower().Contains("obd"))
                    {
                        bluetoothDevice = bd;
                    }
                }
                if (bluetoothDevice == null)
                {
                    return(false);
                }
                bluetoothSocket = bluetoothDevice.CreateRfcommSocketToServiceRecord(SppUuid);

                await bluetoothSocket.ConnectAsync();

                connected = true;
            }
            catch (Java.IO.IOException ex)
            {
                // Close the socket
                try
                {
                    connected = false;
                    bluetoothSocket.Close();
                }
                catch (Java.IO.IOException)
                {
                }
                catch (Exception)
                {
                }

                return(false);
            }
            catch (Exception ex)
            {
            }
            if (connected)
            {
                reader = bluetoothSocket.InputStream;
                writer = bluetoothSocket.OutputStream;

                string s;
                s = await SendAndReceive("ATZ\r");

                s = await SendAndReceive("ATE0\r");

                s = await SendAndReceive("ATL1\r");

                s = await SendAndReceive("ATSP00\r");

                PollObd();

                return(true);
            }
            else
            {
                return(false);
            }
        }