/// <summary>
		/// Connect to the EV3 brick.
		/// </summary>
		public async Task ConnectAsync()
		{
			BluetoothAdapter adapter = BluetoothAdapter.DefaultAdapter;
			if(adapter == null)
				throw new Exception("No Bluetooth adapter found");

			if(!adapter.IsEnabled)
				throw new Exception("Bluetooth adapter is not enabled");

			BluetoothDevice device = (from bd in adapter.BondedDevices where bd.Name == _deviceName select bd).FirstOrDefault();
			if(device == null)
				throw new Exception("LEGO EV3 brick named '" + _deviceName + "' not found.");

			_socket = device.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805f9b34fb"));
			await _socket.ConnectAsync();

			_tokenSource = new CancellationTokenSource();

			Task t = Task.Factory.StartNew(async () =>
			{
				while(!_tokenSource.IsCancellationRequested)
				{
					Stream stream = _socket.InputStream;

					// if the stream is valid and ready
					if(stream != null && stream.CanRead)
					{
						await stream.ReadAsync(_sizeBuffer, 0, _sizeBuffer.Length);

						short size = (short)(_sizeBuffer[0] | _sizeBuffer[1] << 8);
						if(size == 0)
							return;

						byte[] report = new byte[size];
						await stream.ReadAsync(report, 0, report.Length);
						if (ReportReceived != null)
							ReportReceived(this, new ReportReceivedEventArgs { Report = report });
					}
				}
			}, _tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current);
		}
Example #2
0
        public async Task<bool> Init(bool simulatormode = false)
        {
            running = true;
            //initialize _data
            data = new Dictionary<string, string> {{"vin", DefValue}};
            //VIN
            piDs = ObdShare.ObdUtil.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)
            {
                // Close the socket
                try
                {
                    connected = false;
                    bluetoothSocket.Close();
                }
                catch (Java.IO.IOException)
                {
                }
                catch (Exception)
                {
                }

                return false;
            }
            catch (Exception)
            {
            }
            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;
        }