Пример #1
0
        public DeviceReading ConnectToDevice(FoundBluetoothDevice device)
        {
            device.Client.SetPin(device.Configuration.Pin); // Set the pin access for the device to auto connect
            device.Client.Connect(device.Device.DeviceAddress,
                                  InTheHand.Net.Bluetooth.BluetoothService.SerialPort);

            return(Connect(device));
        }
Пример #2
0
 /// <summary>
 /// Starts the listening from Senders.
 /// </summary>
 /// <param name="reportAction">
 /// The report Action.
 /// </param>
 /// <param name="device"></param>
 public DeviceReading ReadData(FoundBluetoothDevice device)
 {
     try
     {
         return(device.Client.Connected ? Connect(device) : ConnectToDevice(device));
     }
     catch (SocketException ex)
     {
         Debug.WriteLine(ex.Message);
         Debug.WriteLine("A reading from the device has tried to be read and failed, remove the device");
         throw;
     }
     catch (ObjectDisposedException ex)
     {
         Debug.WriteLine(ex.Message);
         throw;
     }
     catch (IOException ex)
     {
         Debug.WriteLine(ex.Message);
         Debug.WriteLine("Unable to get network stream");
         throw;
     }
     catch (IndexOutOfRangeException ex)
     {
         Debug.WriteLine("The index extracting the data was out of range");
         throw;
     }
     catch (SleepException ex)
     {
         throw;
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Unexpected exception has occurred whilst reading the bluetooth data");
         Debug.Write(ex.Message);
         throw;
     }
 }
Пример #3
0
        private DeviceReading Connect(FoundBluetoothDevice device)
        {
            // client is connected
            NetworkStream stream = device.Client.GetStream();

            if (stream.CanRead)
            {
                do
                {
                    stream.ReadTimeout = 2500; // 500 millisecond timeout, if unable to get data feed
                    int numberOfBytesRead = stream.Read(_myReadBuffer, 0, _myReadBuffer.Length);
                    if (numberOfBytesRead <= 1)
                    {
                        continue;
                    }

                    string sensorTempValue = Encoding.ASCII.GetString(_myReadBuffer, 0, numberOfBytesRead);
                    if (string.IsNullOrWhiteSpace(sensorTempValue))
                    {
                        return(null);
                    }

                    string[] arr = sensorTempValue.Split(Environment.NewLine).ToArray();

                    if (arr.Length == 0)
                    {
                        return(null); // nothing to get
                    }
                    DeviceReading deviceReading = new DeviceReading();

                    string latestReading = arr.LastOrDefault(x => !string.IsNullOrWhiteSpace(x));

                    if (string.IsNullOrWhiteSpace(latestReading))
                    {
                        return(null);
                    }

                    string[] latestData = latestReading.Split(' ')
                                          .Where(x => !string.IsNullOrWhiteSpace(x))
                                          .ToArray();

                    if (latestData.Length == 1) // only one value in the array suspect stoppage
                    {
                        if (latestData[0].Equals("-sleep", StringComparison.CurrentCultureIgnoreCase))
                        {
                            throw new SleepException("The device is now sleeping");
                        }
                    }

                    int sdCardIndex = Array.IndexOf(latestData, "-sdCardData");
                    if (sdCardIndex > -1) // has sd card data
                    {
                        // loop through elements after sd card data element
                        SdCardDeviceReading sdCardData = ExtractBluetoothData(latestData, sdCardIndex + 1);
                        deviceReading.SdCardDeviceReading = sdCardData;
                    }

                    LiveDeviceReading liveDeviceReading = ExtractBluetoothData(latestData);
                    deviceReading.LiveDeviceReading = liveDeviceReading;

                    return(deviceReading);
                }while (stream.DataAvailable); // only continue if there is more to stream and the parse was successful.

                return(null);
            }

            return(null);
        }