Esempio n. 1
0
        private async Task <bool> WriteSensor(byte[] bytes, ServiceCharacteristicsEnum character)
        {
            Debug.WriteLine("Begin write sensor: " + SensorIndex.ToString());
            bool ret = false;

            if (GattService != null)
            {
                GattCharacteristic           characteristic = null;
                GattCharacteristicProperties flag           = GattCharacteristicProperties.Write;
                switch (character)
                {
                case ServiceCharacteristicsEnum.Data:
                    characteristic = this.Data;
                    break;

                case ServiceCharacteristicsEnum.Notification:
                    flag           = GattCharacteristicProperties.Notify;
                    characteristic = this.Notification;
                    break;

                case ServiceCharacteristicsEnum.Configuration:
                    characteristic = this.Configuration;
                    break;

                case ServiceCharacteristicsEnum.Period:
                    characteristic = this.Period;
                    break;

                case ServiceCharacteristicsEnum.Address:
                    characteristic = this.Address;
                    break;

                case ServiceCharacteristicsEnum.Device_Id:
                    characteristic = this.Device_Id;
                    break;
                }
                if (characteristic != null)
                {
                    if (characteristic.CharacteristicProperties.HasFlag(flag))
                    {
                        var writer = new Windows.Storage.Streams.DataWriter();
                        writer.WriteBytes(bytes);

                        var status = await characteristic.WriteValueAsync(writer.DetachBuffer());

                        if (status == GattCommunicationStatus.Success)
                        {
                            ret = true;
                        }
                    }
                }
            }
            Debug.WriteLine("End write sensor: " + SensorIndex.ToString());
            return(ret);
        }
Esempio n. 2
0
        private async Task <bool> ReadSensor(byte[] bytes, ServiceCharacteristicsEnum character)
        {
            Debug.WriteLine("Begin read sensor: " + SensorIndex.ToString());
            bool ret = false;

            if (GattService != null)
            {
                GattCharacteristic           characteristic = null;
                GattCharacteristicProperties flag           = GattCharacteristicProperties.Read;
                switch (character)
                {
                case ServiceCharacteristicsEnum.Data:
                    characteristic = this.Data;
                    break;

                case ServiceCharacteristicsEnum.Notification:
                    characteristic = this.Notification;
                    break;

                case ServiceCharacteristicsEnum.Configuration:
                    characteristic = this.Configuration;
                    break;

                case ServiceCharacteristicsEnum.Period:
                    characteristic = this.Period;
                    break;

                case ServiceCharacteristicsEnum.Address:
                    characteristic = this.Address;
                    break;

                case ServiceCharacteristicsEnum.Device_Id:
                    characteristic = this.Device_Id;
                    break;
                }
                if (characteristic != null)
                {
                    if (characteristic.CharacteristicProperties.HasFlag(flag))
                    {
                        var result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);

                        var status = result.Status;
                        if (status == GattCommunicationStatus.Success)
                        {
                            ret = true;
                            var dat = result.Value;
                        }
                    }
                }
            }
            Debug.WriteLine("End read sensor: " + SensorIndex.ToString());
            return(ret);
        }
Esempio n. 3
0
        //Constants for the Sensor device
        /// <summary>
        /// The relative "address" of the characteristic in the service.
        /// There is an overlap.
        /// </summary>
        /// <param name="characteristic">A Service Characteristic</param>
        /// <returns>The relative "address"</returns>
        public int ReAddr(ServiceCharacteristicsEnum characteristic)
        {
            int ret = (int)characteristic;

            switch (characteristic)
            {
            case ServiceCharacteristicsEnum.Address:
                ret = 2;
                break;

            case ServiceCharacteristicsEnum.Device_Id:
                ret = 3;
                break;
            }
            return(ret);
        }
Esempio n. 4
0
        /// <summary>
        /// Read a readable sensor characteristic, typically the Data characteristic
        /// </summary>
        /// <param name="character">The characteristic to read from.</param>
        /// <returns>Buffer for data. Is created in the call</returns>
        public async Task <byte[]> ReadSensorBase(ServiceCharacteristicsEnum character)
        {
            byte[] bytes = null;
            Debug.WriteLine("Begin ReadSensorBase: " + SensorIndex.ToString());
            bool ret = false;

            try
            {
                if (GattService != null)
                {
                    bytes = new byte[DataLength[(int)SensorIndex]];
                    GattCharacteristic           characteristic = null;
                    GattCharacteristicProperties flag           = GattCharacteristicProperties.Read;
                    switch (character)
                    {
                    case ServiceCharacteristicsEnum.Data:
                        characteristic = this.Data;
                        break;

                    case ServiceCharacteristicsEnum.Notification:
                        characteristic = this.Notification;
                        break;

                    case ServiceCharacteristicsEnum.Configuration:
                        characteristic = this.Configuration;
                        break;

                    case ServiceCharacteristicsEnum.Period:
                        characteristic = this.GattCharacteristicPeriod;
                        break;

                    case ServiceCharacteristicsEnum.Address:
                        characteristic = this.Address;
                        break;

                    case ServiceCharacteristicsEnum.Device_Id:
                        characteristic = this.Device_Id;
                        break;
                    }
                    if (characteristic != null)
                    {
                        if (characteristic.CharacteristicProperties.HasFlag(flag))
                        {
                            GattReadResult result = null;
                            try
                            {
                                result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Error-1: ReadSensorBase(): " + SensorIndex.ToString() + " " + ex.Message);
                                result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Cached);
                            }
                            if (result != null)
                            {
                                var status = result.Status;
                                if (status == GattCommunicationStatus.Success)
                                {
                                    ret = true;
                                    var dat = result.Value;
                                    var xx  = dat.GetType();
                                    var yy  = dat.Capacity;
                                    var zz  = dat.Length;

                                    bytes = new byte[result.Value.Length];

                                    Windows.Storage.Streams.DataReader.FromBuffer(result.Value).ReadBytes(bytes);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: ReadSensorBase(): " + SensorIndex.ToString() + " " + ex.Message);
            }
            Debug.WriteLine("End ReadSensorBase: " + SensorIndex.ToString());
            if (!ret)
            {
                bytes = null;
            }
            return(bytes);
        }