Exemple #1
0
        /// <summary>
        /// Class constructor. Instantiates a new <c>BleDevice</c> object with
        /// the given native device.
        /// </summary>
        /// <param name="device">The native Bluetooth device.</param>
        public BleDevice(IDevice device)
        {
            Device = device ?? throw new ArgumentNullException("Device cannot be null");
            Rssi   = device.Rssi;

            XBeeDevice = new XBeeBLEDevice(device, null);
        }
 internal BluetoothRelay(IDevice device_in, string password, IEmissionProcessor receiver_in)
 {
     //
     this.TranmissionFormatter = new MicropyEmissionObjectFormatter();
     this.receiver             = receiver_in;
     this.device = new XBeeBLEDevice(device_in, null);
     //this.device.PacketReceived += Device_PacketReceived;
     //this.device.SerialDataReceived += Device_SerialDataReceived;
     //this.device.UserDataRelayReceived += Device_UserDataRelayReceived;
     this.device.ReceiveTimeout = 1000;
     this.device.SetBluetoothPassword(password);
     this.device.Open();
     this.device.EnableBluetooth();
     return;
 }
        /// <summary>
        /// Determines the type of the given irrigation device.
        /// </summary>
        /// <param name="device">Bluetooth device.</param>
        /// <returns>The type of the irrigation device.</returns>
        private string DetermineDeviceType(BleDevice device)
        {
            XBeeBLEDevice xbeeDevice = device.XBeeDevice;
            string        type       = null;

            // Register an event handler for incoming data from the serial interface of the
            // Bluetooth device (XBee Gateway).
            EventHandler <SerialDataReceivedEventArgs> serialHandler = (object sender, SerialDataReceivedEventArgs e) =>
            {
                type = ParseResponse(device, e.Data);
            };

            xbeeDevice.SerialDataReceived += serialHandler;

            // Register an event handler for incoming data from the MicroPython interface of the
            // Bluetooth device (XBee module).
            EventHandler <MicroPythonDataReceivedEventArgs> mpHandler = (object sender, MicroPythonDataReceivedEventArgs e) =>
            {
                type = ParseResponse(device, e.Data);
            };

            xbeeDevice.MicroPythonDataReceived += mpHandler;

            JObject json = JObject.Parse(@"{
				'"                 + JsonConstants.ITEM_OP + "': '" + JsonConstants.OP_ID + "'" +
                                         "}");

            // Send the ID request to both interfaces, only one will respond.
            xbeeDevice.SendSerialData(Encoding.UTF8.GetBytes(json.ToString()));
            xbeeDevice.SendMicroPythonData(Encoding.UTF8.GetBytes(json.ToString()));

            // Wait until the type is received or the timeout elapses.
            long deadline = Environment.TickCount + DEV_INIT_TIMEOUT;

            while (type == null && Environment.TickCount < deadline)
            {
                Task.Delay(100);
            }

            // Unregister the callbacks.
            xbeeDevice.SerialDataReceived      -= serialHandler;
            xbeeDevice.MicroPythonDataReceived -= mpHandler;

            return(type);
        }