/// <summary>
            /// Look up a property(service or characteristic) based upon its UUID.
            /// Avoid possible error if UUID isn't in the table
            /// </summary>
            /// <param name="uuid"></param>
            /// <returns>The Service or Characteristic(Property)</returns>
            internal SensorTagProperties GetProperty(string uuid)
            {
                SensorTagProperties property = SensorTagProperties.NOTFOUND;

                if (UUIDsPropertyTable.Keys.Contains(uuid.ToUpper()))
                {
                    property = UUIDsPropertyTable[uuid.ToUpper()];
                }
                return(property);
            }
            /// <summary>
            /// NB: Should be able to merge this with the previous method (2Do)
            /// Look up a property(service or characteristic) based upon its UUID.
            /// Avoid possible error if UUID isn't in the table
            /// </summary>
            /// <param name="uuid"></param>
            /// <returns>The Service or Characteristic(Property)</returns>
            internal SensorTagProperties GetPropertyCharacteristicType(string uuid)
            {
                SensorTagProperties res = SensorTagProperties.NOTFOUND;

                if (UUIDsPropertyTable.Keys.Contains(uuid.ToUpper()))
                {
                    res = UUIDsPropertyTable[uuid.ToUpper()];
                }
                return(res);
            }
            /// <summary>
            /// Look up all property values for SensorTag
            /// </summary>
            /// <returns>list of properties and byte[]</returns>
            public async Task <Dictionary <SensorTagProperties, byte[]> > GetProperties(bool doBattery)
            {
                if (!Properties.Keys.Contains(SensorTagProperties.PropertiesService))
                {
                    Debug.WriteLine("Error: Properties database not set up.");
                    return(null);
                }

                byte[] bytes = null;
                Dictionary <SensorTagProperties, byte[]> deviceProprties = new Dictionary <SensorTagProperties, byte[]>();

                Array values = Enum.GetValues(typeof(SensorTagProperties));

                //foreach (SensorTagProperties val in values)
                for (SensorTagProperties val = SensorTagProperties.SysId; val <= SensorTagProperties.PNPId; val++)
                {
                    bytes = null;
                    if (val == SensorTagProperties.BatteryLevel)
                    {
                        if (doBattery)
                        {
                            bytes = await GetBatteryLevel();
                        }
                        else
                        {
                            continue;
                        }
                    }
                    else
                    {
                        bytes = await ReadProperty(val, false);
                    }
                    if (bytes != null)
                    {
                        if (!showbytes.Contains(val))
                        {
                            string res = System.Text.Encoding.UTF8.GetString(bytes);
                            if (res != null)
                            {
                                if (res != "")
                                {
                                    deviceProprties.Add(val, bytes);
                                    Debug.WriteLine("{0} [{1}]: {2}", val.ToString(), res.Length, res);
                                }
                            }
                        }
                        else
                        {
                            if (bytes != null)
                            {
                                deviceProprties.Add(val, bytes);
                                string str = val.ToString() + "[" + bytes.Length.ToString() + "] {";
                                Debug.Write(str);
                                for (int i = 0; i < bytes.Length; i++)
                                {
                                    Debug.Write(" " + bytes[i].ToString("X2"));
                                }
                                Debug.WriteLine(" }");
                            }
                            //NB:
                            //    Re: PNP_ID App got: pnp_id[7] { 01 0D 00 00 00 10 01 }
                            //    From:
                            //    https://e2e.ti.com/support/wireless_connectivity/bluetooth_low_energy/f/538/p/434053/1556237
                            //
                            //    In devinfoservice.c, you can find vendor ID and product ID information below where TI's vendor ID is 0x000D.
                            //    static uint8 devInfoPnpId[DEVINFO_PNP_ID_LEN] ={
                            //    1, // Vendor ID source (1=Bluetooth SIG)
                            //    LO_UINT16(0x000D), HI_UINT16(0x000D), // Vendor ID (Texas Instruments)
                            //    LO_UINT16(0x0000), HI_UINT16(0x0000), // Product ID (vendor-specific)
                            //    LO_UINT16(0x0110), HI_UINT16(0x0110) // Product version (JJ.M.N)};
                            //
                        }
                    }
                }
                return(deviceProprties);
            }
            /// <summary>
            /// Look up a specific SensorTag property using Property Service
            /// </summary>
            /// <param name="property">The property to look upo</param>
            /// <param name="showStartEndMsg">Debugging option</param>
            /// <returns>Byte[]</returns>
            public async Task <byte[]> ReadProperty(SensorTagProperties property, bool showStartEndMsg)
            {
                if (showStartEndMsg)
                {
                    Debug.WriteLine("Begin read property: {0} ", property);
                }

                byte[] bytes = null;

                if (!Properties.Keys.Contains(SensorTagProperties.PropertiesService))
                {
                    Debug.WriteLine("Error: Properties database not set up.");
                    return(null);
                }

                if (!Properties[SensorTagProperties.PropertiesService].CharcteristicsP.Keys.Contains(property))
                {
                    Debug.WriteLine("Missing Property {0}", property);
                    return(null);
                }

                GattCharacteristicProperties flag           = GattCharacteristicProperties.Read;
                GattCharacteristic           characteristic = Properties[SensorTagProperties.PropertiesService].CharcteristicsP[property];

                if (characteristic.CharacteristicProperties.HasFlag(flag))
                {
                    try
                    {
                        GattReadResult result = null;
                        try
                        {
                            result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
                        }
                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                            Debug.WriteLine("Error ReadProperty1(): " + msg);
                        }

                        var status = result.Status;
                        if (status == GattCommunicationStatus.Success)
                        {
                            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);
                            IncProgressCounter();
                        }
                    }

                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                        Debug.WriteLine("Error ReadProperty2(): " + msg);
                    }
                }

                if (showStartEndMsg)
                {
                    Debug.WriteLine("End read property: {0} ", property);
                }

                return(bytes);
            }
예제 #5
0
        public static async Task <byte[]> ReadProperty(SensorTagProperties property, bool showStartEndMsg)
        {
            //if (showStartEndMsg)
            Debug.WriteLine("Begin read property: {0} ", property);
            string guidstr = "";

            byte[] bytes = null;
            switch (property)
            {
            case SensorTagProperties.FirmwareDate:
                guidstr = UUID_PROPERTY_FW_NR;
                break;

            case SensorTagProperties.HardwareRevision:
                guidstr = UUID_PROPERTY_HW_NR;
                break;

            case SensorTagProperties.ManufacturerId:
                guidstr = UUID_PROPERTY_MANUF_NR;
                break;

            case SensorTagProperties.ModelName:
                guidstr = UUID_PROPERTY_MODEL_NR;
                break;

            case SensorTagProperties.PNPId:
                guidstr = UUID_PROPERTY_PNP_ID;
                break;

            case SensorTagProperties.SerialNumber:
                guidstr = UUID_PROPERTY_SERIAL_NR;
                break;

            case SensorTagProperties.SoftwareRevision:
                guidstr = UUID_PROPERTY_SW_NR;
                break;

            case SensorTagProperties.SysId:
                guidstr = UUID_PROPERTY_SYSID;
                break;

            case SensorTagProperties.BTSigCertification:
                guidstr = UUID_PROPERTY_CERT;
                break;

            case SensorTagProperties.DeviceName:
                guidstr = UUID_PROPERTY_NAME;
                break;

            case SensorTagProperties.BatteryLevel:
                return(bytes);
            }

            IReadOnlyList <GattCharacteristic> sidCharacteristicList = DevicePropertyService.GetCharacteristics(new Guid(guidstr));
            GattCharacteristicProperties       flag = GattCharacteristicProperties.Read;

            if (sidCharacteristicList != null)
            {
                if (sidCharacteristicList.Count != 0)
                {
                    GattCharacteristic characteristic = sidCharacteristicList[0];
                    if (characteristic.CharacteristicProperties.HasFlag(flag))
                    {
                        try
                        {
                            GattReadResult result = null;
                            try
                            {
                                result = await characteristic.ReadValueAsync(Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
                            }
                            catch (Exception ex)
                            {
                                string msg = ex.Message;
                                Debug.WriteLine("Error ReadProperty1(): " + msg);
                            }

                            var status = result.Status;
                            if (status == GattCommunicationStatus.Success)
                            {
                                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);
                                IncProgressCounter();
                            }
                        }

                        catch (Exception ex)
                        {
                            string msg = ex.Message;
                            Debug.WriteLine("Error ReadProperty2(): " + msg);
                        }
                    }
                }
            }
            //if(showStartEndMsg)
            Debug.WriteLine("End read property: {0} ", property);

            return(bytes);
        }