public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            if (!BLEHelpers.UuidGlucMeasurementChar.Equals(characteristic.Uuid))
            {
                return;
            }
            var offset = 0;
            int flags  = characteristic.GetIntValue(GattFormat.Uint8, offset).IntValue();

            offset += 1;

            bool timeOffsetPresent               = (flags & 0x01) > 0;
            bool typeAndLocationPresent          = (flags & 0x02) > 0;
            bool sensorStatusAnnunciationPresent = (flags & 0x08) > 0;

            // create and fill the new record
            _record = new GlucoseDeviceData {
                SequenceNumber = characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue()
            };
            offset += 2;

            int year    = characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue();
            int month   = characteristic.GetIntValue(GattFormat.Uint8, offset + 2).IntValue();
            int day     = characteristic.GetIntValue(GattFormat.Uint8, offset + 3).IntValue();
            int hours   = characteristic.GetIntValue(GattFormat.Uint8, offset + 4).IntValue();
            int minutes = characteristic.GetIntValue(GattFormat.Uint8, offset + 5).IntValue();
            int seconds = characteristic.GetIntValue(GattFormat.Uint8, offset + 6).IntValue();

            offset += 7;

            var calendar = Calendar.Instance;

            calendar.Set(year, month, day, hours, minutes, seconds);

            if (timeOffsetPresent)
            {
                characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue();
                offset += 2;
            }

            if (typeAndLocationPresent)
            {
                _record.GlucoseConcentration = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
                offset += 3;
            }

            if (sensorStatusAnnunciationPresent)
            {
                characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue();
            }

            gatt.Disconnect();
        }
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            var     record = new BloodPressureData();
            var     offset = 0;
            Integer flags  = characteristic.GetIntValue(GattFormat.Uint8, offset++);
            // See BPMManagerCallbacks.UNIT_* for unit options
            bool timestampPresent = ((int)flags & 0x02) > 0;
            bool pulseRatePresent = ((int)flags & 0x04) > 0;

            // following bytes - systolic, diastolic and mean arterial pressure
            record.Systolic  = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
            record.Diastolic = characteristic.GetFloatValue(GattFormat.Sfloat, offset + 2)
                               .FloatValue();
            offset += 6;
            if (timestampPresent)
            {
                record.RecordDateTime = new DateTime(
                    characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue(),
                    characteristic.GetIntValue(GattFormat.Uint8, offset + 2).IntValue(),
                    characteristic.GetIntValue(GattFormat.Uint8, offset + 3).IntValue(),
                    characteristic.GetIntValue(GattFormat.Uint8, offset + 4).IntValue(),
                    characteristic.GetIntValue(GattFormat.Uint8, offset + 5).IntValue(),
                    characteristic.GetIntValue(GattFormat.Uint8, offset + 6).IntValue());
                offset += 7;
            }
            if (pulseRatePresent)
            {
                record.PulseRate = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
            }
            Records.Add(record);
        }
Пример #3
0
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            try
            {
                //if (LastCharacteristicChange.HasValue)
                //    Console.WriteLine(DateTime.Now.Subtract(LastCharacteristicChange.Value).TotalMilliseconds + " " + FlowManager.CurrentStep1);

                if (BluetoothSensorTagAttributes.MovementData.ToString() != characteristic.Uuid.ToString())
                {
                    return;
                }

                if (Sensor.Instance.Status != SensorStatus.Connected)
                {
                    lock (Sensor.Instance)
                        Sensor.Instance.Status = SensorStatus.Connected;
                }

                if (ConnectionVerification == null || !ConnectionVerification.IsAlive)
                {
                    StartConnectionVerification();
                }

                LastCharacteristicChange = DateTime.Now;


                var gyroscopeX = (int)characteristic.GetIntValue(GattFormat.Sint16, 0);
                var gyroscopeY = (int)characteristic.GetIntValue(GattFormat.Sint16, 2);
                var gyroscopeZ = (int)characteristic.GetIntValue(GattFormat.Sint16, 4);

                var accelerometerX = (int)characteristic.GetIntValue(GattFormat.Sint16, 6);
                var accelerometerY = (int)characteristic.GetIntValue(GattFormat.Sint16, 8);
                var accelerometerZ = (int)characteristic.GetIntValue(GattFormat.Sint16, 10);

                var magnetometerX = (int)characteristic.GetIntValue(GattFormat.Sint16, 12);
                var magnetometerY = (int)characteristic.GetIntValue(GattFormat.Sint16, 14);
                var magnetometerZ = (int)characteristic.GetIntValue(GattFormat.Sint16, 16);

                Sensor.Instance.SetAcc(accelerometerX, accelerometerY, accelerometerZ);
                Sensor.Instance.SetGyro(gyroscopeX, gyroscopeY, gyroscopeZ);

                base.OnCharacteristicChanged(gatt, characteristic);
            }
            catch (System.Exception ex)
            {
                string title = this.GetType().Name + " - " + System.Reflection.MethodBase.GetCurrentMethod().Name;
                BalizaFacil.App.Instance.UnhandledException(title, ex);
            }
        }
Пример #4
0
        public sealed override void BuildCharacteristic(BluetoothGattCharacteristic characteristic)
        {
            if (!characteristic.Uuid.Equals(Uuid))
            {
                throw new GattCharactersticMismatch(this, characteristic.Uuid);
            }
            var bytes = characteristic.GetValue();

            if (bytes == null)
            {
                return;
            }
            //1 bit per flag, as a boolean
            TimeOffsetPresent               = bytes[0].BitAt(0);
            GlucoseConcentrationPresent     = bytes[0].BitAt(1);
            _glucoseConcentrationUnits      = bytes[0].BitAt(2);
            SensorStatusAnnunciationPresent = bytes[0].BitAt(3);
            ContextInformationFollows       = bytes[0].BitAt(4);

            SequenceNumber = characteristic.GetIntValue(GattFormat.Uint16, 1).IntValue();
            _baseTime.ConvertFromCharacteristicByBytes(bytes.SubarrayAt(3, 9)); //7 bytes for the date.
            BaseTime = _baseTime.Date;

            TimeOffset = characteristic.GetIntValue(GattFormat.Sint16, 10).IntValue();
            var glcConcentration = characteristic.GetFloatValue(GattFormat.Sfloat, 12).FloatValue();

            if (_glucoseConcentrationUnits)
            {
                GlucoseConcentration = Math.Round(glcConcentration * 100000 / MmollToMgdl, 1);
            }
            else
            {
                GlucoseConcentration = Math.Round(glcConcentration * 1000 * MmollToMgdl, 1);
            }

            Type           = (GlucoseTypes)bytes.SubarrayAt(14, 14).ToInt16().NibbleAt(false);
            SampleLocation = (GlucoseSampleLocation)bytes.SubarrayAt(14, 14).ToInt16().NibbleAt(true); //need a nibble at method


            //SensorStatusAnnunciation = bytes.SubarrayAt(17, 19);
        }
Пример #5
0
        public void BroadcastUpdate(String action, BluetoothGattCharacteristic characteristic)
        {
            Intent intent = new Intent(action);

            // This is special handling for the Heart Rate Measurement profile.  Data parsing is
            // carried out as per profile specifications:
            // http://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.heart_rate_measurement.xml
            if (UUID_HEART_RATE_MEASUREMENT == (characteristic.Uuid))
            {
                GattProperty flag   = characteristic.Properties;
                GattFormat   format = (GattFormat)(-1);

                if (((int)flag & 0x01) != 0)
                {
                    format = GattFormat.Uint16;
                    Log.Debug(TAG, "Heart rate format UINT16.");
                }
                else
                {
                    format = GattFormat.Uint8;
                    Log.Debug(TAG, "Heart rate format UINT8.");
                }

                var heartRate = characteristic.GetIntValue(format, 1);
                Log.Debug(TAG, String.Format("Received heart rate: {0}", heartRate));
                intent.PutExtra(EXTRA_DATA, heartRate);
            }
            else
            {
                // For all other profiles, writes the data formatted in HEX.
                byte[] data = characteristic.GetValue();

                if (data != null && data.Length > 0)
                {
                    StringBuilder stringBuilder = new StringBuilder(data.Length);
                    foreach (byte byteChar in data)
                    {
                        stringBuilder.Append(String.Format("{0}02X ", byteChar));
                    }
                    intent.PutExtra(EXTRA_DATA, Convert.ToBase64String(data) + "\n" + stringBuilder.ToString());
                }
            }

            SendBroadcast(intent);
        }
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            if (BLEHelpers.BLE_CHAR_CUSTOM_TIME_MC.Equals(characteristic.Uuid))
            {
                return;
            }
            if (BLEHelpers.BLE_CHAR_CUSTOM_TIME_TI.Equals(characteristic.Uuid) ||
                BLEHelpers.BLE_CHAR_CUSTOM_TIME_TI_NEW.Equals(characteristic.Uuid))
            {
                int intValue = characteristic.GetIntValue(GattFormat.Uint8, 0).IntValue();
                switch (intValue)
                {
                case 5 when !_isIsensMeter || _timesyncUtcTzCnt >= 3:
                    RequestSequence(gatt);
                    break;

                case 5:
                    GetCustomTimeSync(gatt);
                    break;

                case 192 when characteristic.GetIntValue(GattFormat.Uint8, 1).IntValue() == 2: {
                    if (gatt == null || _racpCharacteristic == null)
                    {
                        return;
                    }

                    GetCustomTimeSync(gatt);
                    break;
                }
                }
            }
            else
            {
                if (BLEHelpers.BLE_CHAR_GLUCOSE_MEASUREMENT.Equals(characteristic.Uuid))
                {
                    if (characteristic.GetValue()[0] == 6 || characteristic.GetValue()[0] == 7)
                    {
                        return;
                    }

                    int intValue2 = characteristic.GetIntValue(GattFormat.Uint8, 0).IntValue();
                    if (intValue2 == 5)
                    {
                        return;
                    }
                    bool checkIfTimeIsAvailable        = (intValue2 & 1) > 0;
                    bool checkIfGlucoseDataIsAvailable = (intValue2 & 2) > 0;
                    bool checkIfValueIsHighOrLow       = (intValue2 & 8) > 0;
                    var  glucoseRecord  = new GlucoseRecord();
                    int  year           = characteristic.GetIntValue(GattFormat.Uint16, 3).IntValue();
                    int  month          = characteristic.GetIntValue(GattFormat.Uint8, 5).IntValue();
                    int  date           = characteristic.GetIntValue(GattFormat.Uint8, 6).IntValue();
                    int  hourOfDay      = characteristic.GetIntValue(GattFormat.Uint8, 7).IntValue();
                    int  minutes        = characteristic.GetIntValue(GattFormat.Uint8, 8).IntValue();
                    int  seconds        = characteristic.GetIntValue(GattFormat.Uint8, 9).IntValue();
                    var  dateTimeRecord = new DateTime(year, month, date, hourOfDay, minutes, seconds);
                    var  offset         = 10;
                    if (checkIfTimeIsAvailable)
                    {
                        glucoseRecord.DateTimeRecord = dateTimeRecord;
                        offset = 12;
                    }

                    if (checkIfGlucoseDataIsAvailable)
                    {
                        var value = characteristic.GetValue();
                        glucoseRecord.GlucoseData = BytesToInt(value[offset], value[offset + 1]);
                        offset += 3;
                    }

                    if (checkIfValueIsHighOrLow)
                    {
                        glucoseRecord.FlagHilow = characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue() == 64
                                                        ? -1
                                                        : 1;
                    }

                    try {
                        if (glucoseRecord.GlucoseData >= 20)
                        {
                            _records.Add(_records.Count, glucoseRecord);
                        }
                    } catch (Exception e) {
                        Log.Error("Data insertion failed", e.Message);
                    }
                }
                else if (BLEHelpers.BLE_CHAR_GLUCOSE_RACP.Equals(characteristic.Uuid))
                {
                    int status = characteristic.GetIntValue(GattFormat.Uint8, 0).IntValue();
                    switch (status)
                    {
                    case 5 when gatt == null || _racpCharacteristic == null:
                        return;

                    case 5 when _serial == null:
                        Log.Error("Error", "serial is null");
                        return;

                    case 5:
                        RequestBleAll(gatt);
                        break;

                    case 6: {
                        if (!_isDownloadComplete)
                        {
                            _isDownloadComplete = true;
                            gatt.WriteCharacteristic(characteristic);
                        }

                        break;
                    }
                    }
                }
            }
        }
Пример #7
0
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            BluetoothGatt = gatt;
            try
            {
                //if (LastCharacteristicChange.HasValue)
                //Console.WriteLine(DateTime.Now.Subtract(LastCharacteristicChange.Value).TotalMilliseconds + " " + FlowManager.CurrentStep1);


                if (Sensor.Instance.Status != SensorStatus.Connected)
                {
                    lock (Sensor.Instance)
                        Sensor.Instance.Status = SensorStatus.Connected;
                }

                if (ConnectionVerification == null || !ConnectionVerification.IsAlive)
                {
                    StartConnectionVerification();
                }

                LastCharacteristicChange = DateTime.Now;

                if (BluetoothSensorAttributes.GyroscopeData.ToString() == characteristic.Uuid.ToString())
                {
                    int millisecondsPassedg = 0;

                    if (gyroLastTime != DateTime.MinValue)
                    {
                        millisecondsPassedg = (int)DateTime.Now.Subtract(gyroLastTime).TotalMilliseconds;
                    }

                    gyroLastTime = DateTime.Now;

                    gyroscopeX     = (int)characteristic.GetIntValue(GattFormat.Sint16, 0);
                    gyroscopeY     = (int)characteristic.GetIntValue(GattFormat.Sint16, 2);
                    gyroscopeZ     = (int)characteristic.GetIntValue(GattFormat.Sint16, 4);
                    accelerometerX = (int)characteristic.GetIntValue(GattFormat.Sint8, 6);
                    accelerometerY = (int)characteristic.GetIntValue(GattFormat.Sint8, 7);
                    accelerometerZ = (int)characteristic.GetIntValue(GattFormat.Sint8, 8);

                    Sensor.Instance.SetGyro(gyroscopeX, gyroscopeY, gyroscopeZ);
                    Sensor.Instance.SetAcc(accelerometerX, accelerometerY, accelerometerZ);
                }

                //if (BluetoothSensorAttributes.AccelerometerData.ToString() == characteristic.Uuid.ToString())
                //{
                //    int millisecondsPasseda = 0;

                //    if (accLastTime != DateTime.MinValue)
                //        millisecondsPasseda = (int)DateTime.Now.Subtract(accLastTime).TotalMilliseconds;

                //    accLastTime = DateTime.Now;

                //    accelerometerX = (int)characteristic.GetIntValue(GattFormat.Sint8, 0);
                //    accelerometerY = (int)characteristic.GetIntValue(GattFormat.Sint8, 1);
                //    accelerometerZ = (int)characteristic.GetIntValue(GattFormat.Sint8, 2);

                //    accSetted = true;
                //    //Console.WriteLine($"ACC: {millisecondsPasseda} ms");

                //    if(gyroSetted && accSetted)
                //    {
                //        accSetted = gyroSetted = false;
                //        Sensor.Instance.SetAcc(accelerometerX, accelerometerY, accelerometerZ);
                //        Sensor.Instance.SetGyro(gyroscopeX, gyroscopeY, gyroscopeZ);
                //    }

                //}
                base.OnCharacteristicChanged(gatt, characteristic);
            }
            catch (System.Exception ex)
            {
                string title = this.GetType().Name + " - " + System.Reflection.MethodBase.GetCurrentMethod().Name;
                BalizaFacil.App.Instance.UnhandledException(title, ex);
            }
        }
        public override void BuildCharacteristic(BluetoothGattCharacteristic characteristic)
        {
            if (!characteristic.Uuid.Equals(Uuid))
            {
                throw new GattCharactersticMismatch(this, characteristic.Uuid);
            }
            var bytes = characteristic.GetValue();

            if (bytes == null)
            {
                return;
            }

            var offset = 0;

            //1 bit per flag, as a boolean
            CarbohydratePresent   = bytes[0].BitAt(0);
            MealPresent           = bytes[0].BitAt(1);
            TesterHealthPresent   = bytes[0].BitAt(2);
            ExercisePresent       = bytes[0].BitAt(3);
            MedicationPresent     = bytes[0].BitAt(4);
            _medicationUnitsValue = bytes[0].BitAt(5);
            HbA1cPresent          = bytes[0].BitAt(6);
            ExtendedFlags         = bytes[0].BitAt(7);

            offset++;
            SequenceNumber = characteristic.GetIntValue(GattFormat.Uint16, offset).IntValue();
            if (CarbohydratePresent)
            {
                offset++;
                CarbohydrateId = (CarbohydrateTypes)characteristic.GetIntValue(GattFormat.Uint8, offset).IntValue();
                offset++;
                CarbohydrateUnits = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
                offset++;
            }

            if (MealPresent)
            {
                offset++;
                Meal = (MealTypes)characteristic.GetIntValue(GattFormat.Uint8, offset).IntValue();
            }
            if (TesterHealthPresent)
            {
                offset++;
                Tester = (TesterTypes)bytes.SubarrayAt(offset, offset).ToInt16().NibbleAt(true);
                Health = (HealthTypes)bytes.SubarrayAt(offset, offset).ToInt16().NibbleAt(false);
            }

            if (ExercisePresent)
            {
                offset++;
                ExerciseDuration = bytes.SubarrayAt(offset, offset + 1).ToUInt16();
                offset++;
                ExerciseIntensity = (ushort)characteristic.GetIntValue(GattFormat.Uint8, offset).ShortValue();
            }
            if (MedicationPresent)
            {
                offset++;
                MedicationId = (MedicationTypes)characteristic.GetIntValue(GattFormat.Uint8, offset).IntValue();
                offset++;
                MedicationUnits = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
                offset++;
            }
            if (HbA1cPresent)
            {
                offset++;
                HbA1c = characteristic.GetFloatValue(GattFormat.Sfloat, offset).FloatValue();
            }
        }