예제 #1
0
        Task <GattDescriptor> DoGetDescriptor(BluetoothUuid descriptor)
        {
            var gattDescriptor = _characteristic.GetDescriptor(descriptor);

            if (gattDescriptor is null)
            {
                return(Task.FromResult <GattDescriptor>(null));
            }

            return(Task.FromResult(new GattDescriptor(this, gattDescriptor)));
        }
예제 #2
0
        private void PlatformSetNotification(bool enabled)
        {
            if (!_gatt.SetCharacteristicNotification(_characteristic, enabled))
            {
                RaiseSetNotificationFailed(BLEErrorCode.InternalError);
                return;
            }
            using (var uuid = Java.Util.UUID.FromString(BLEConstants.CLIENT_CHARACTERISTIC_CONFIGURATION))
            {
                var descriptor = _characteristic.GetDescriptor(uuid);
                if (!descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray()))
                {
                    RaiseSetNotificationFailed(BLEErrorCode.InternalError);
                    return;
                }
                if (!_gatt.WriteDescriptor(descriptor))
                {
                    RaiseSetNotificationFailed(BLEErrorCode.InternalError);
                    return;
                }
            }

            PlatformIsNotifying = enabled;
            RaiseNotificationSet();
        }
예제 #3
0
파일: BLEDevice.cs 프로젝트: poz1/Poz1.BLE
        public Task <bool> UnsubscribeCharacteristic(string serviceGUID, string characteristicGUID, string descriptorGUID)
        {
            unsubscribeCharacteristicTCS = new TaskCompletionSource <bool>();

            try
            {
                if (_gatt == null)
                {
                    Debug.WriteLine("Connect to Bluetooth Device first");
                    unsubscribeCharacteristicTCS.TrySetException(new Exception("Connect to Bluetooth Device first"));
                }

                BluetoothGattCharacteristic chara = _gatt.GetService(UUID.FromString(serviceGUID)).GetCharacteristic(UUID.FromString(characteristicGUID));
                if (null == chara)
                {
                    subscribeCharacteristicTCS.TrySetException(new Exception("Characteristic Id: " + characteristicGUID + " Not Found in Service: " + serviceGUID));
                }

                _gatt.SetCharacteristicNotification(chara, false);

                BluetoothGattDescriptor descriptor = chara.GetDescriptor(UUID.FromString(descriptorGUID));
                descriptor.SetValue(BluetoothGattDescriptor.DisableNotificationValue.ToArray());
                _gatt.WriteDescriptor(descriptor);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                unsubscribeCharacteristicTCS.TrySetException(new Exception(e.Message));
            }

            return(unsubscribeCharacteristicTCS.Task);
        }
예제 #4
0
 public Characteristic(Service service, BluetoothGattCharacteristic characteristic)
 {
     _service                = service;
     _characteristic         = characteristic;
     _uuid                   = new CharacteristicUuid(new Uuid(characteristic.Uuid.ToString()));
     _clientConfigDescriptor = characteristic.GetDescriptor(s_uuidClientConfiguration);
 }
예제 #5
0
        private Boolean SetNotifyEnable(bool enable)
        {
            if (gatt == null || characteristic == null)
            {
                return(false);
            }

            bool isSuccess = gatt.SetCharacteristicNotification(characteristic, enable);

            if (!isSuccess)
            {
                return(false);
            }

            BluetoothGattDescriptor descriptor = characteristic.GetDescriptor(UUID.FromString("00002902-0000-1000-8000-00805f9b34fb"));

            byte[] value = new byte[BluetoothGattDescriptor.EnableNotificationValue.Count];

            for (int i = 0; i < BluetoothGattDescriptor.EnableNotificationValue.Count; i++)
            {
                value[i] = BluetoothGattDescriptor.EnableNotificationValue[i];
            }

            descriptor.SetValue(value);

            isSuccess = gatt.WriteDescriptor(descriptor);

            return(isSuccess);
        }
        public override void OnCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
        {
            if (characteristic.Uuid.Equals(DT1WatchDogService.DT1WatchdogDataCharacteristicUUID))
            {
                log.Debug("Watchdog data characteristic changed.");

                // we got data - fine we disconnect and wait for the next alarm

                var CLIENT_CHARACTERISTIC_CONFIG        = Java.Util.UUID.FromString("00002902-0000-1000-8000-00805f9b34fb");
                var clientCharacteristiConfigDescriptor = characteristic.GetDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
                clientCharacteristiConfigDescriptor.SetValue(BluetoothGattDescriptor.DisableNotificationValue.ToArray());
                gatt.WriteDescriptor(clientCharacteristiConfigDescriptor);

                gatt.Disconnect();

                var data    = characteristic.GetValue();
                var reading = GlucoseReading.ParseRawCharacteristicData(data);

                // fill in source
                reading.Source = gatt.Device.Name;

                dataService.PersistReading(reading);

                if (reading.ErrorCode == GlucoseReading.ReadingErrorCode.NoError)
                {
                    dataService.LastValidReading = DateTimeOffset.UtcNow;
                    EvaluateReading(reading);
                }

                var dataIntent = new Intent(DT1WatchDogService.IntentIncommingData);
                service.SendBroadcast(dataIntent);
            }
        }
예제 #7
0
        /**
         * Enables or disables notification on a give characteristic.
         *
         * @param characteristic Characteristic to act on.
         * @param enabled If true, enable notification.  False otherwise.
         */
        internal void SetCharacteristicNotification(BluetoothGattCharacteristic characteristic, bool enabled)
        {
            if (bluetoothAdapter == null || bluetoothGatt == null)
            {
                logger.TraceWarning("BluetoothAdapter not initialized");
                return;
            }

            if (characteristic.Uuid.Equals(UUID.FromString(SampleGattAttributes.PRESSURE_NOTIFICATION_HANDLE)))
            {
                bluetoothGatt.SetCharacteristicNotification(characteristic, enabled);
                logger.TraceInformation("Setting notification: Pressure Characteristic detected ");

                BluetoothGattDescriptor descriptor = characteristic.GetDescriptor(UUID.FromString("00002902-0000-1000-8000-00805F9B34FB"));
                if (descriptor != null)
                {
                    logger.TraceInformation("Setting notification: Pressure Descriptor Found");
                    descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray <byte>());
                    bluetoothGatt.WriteDescriptor(descriptor);
                    logger.TraceInformation("Setting notification: Write Pressure Descriptor");
                }
                else
                {
                    logger.TraceWarning("NOTIFICATION SET UP IGNORED");
                }
            }
        }
        // Overridden from BaseLeConnection
        protected override bool ValidateServices()
        {
            if (gatt == null)
            {
                return(false);
            }

            readCharacteristic  = null;
            writeCharacteristic = null;

            var baseService = gatt.GetService(BASE_SERVICE);

            if (baseService != null)
            {
                readCharacteristic = baseService.GetCharacteristic(READ_CHARACTERISTIC);

                if (readCharacteristic != null)
                {
                    readCharacteristicDescriptor = readCharacteristic.GetDescriptor(READ_CHARACTERISTIC_DESCRIPTOR);
                }

                writeCharacteristic = baseService.GetCharacteristic(WRITE_CHARACTERISTIC);
            }

            return(readCharacteristic != null && readCharacteristicDescriptor != null && writeCharacteristic != null);
        }
예제 #9
0
            private void EnableNotification()
            {
                _gatt.SetCharacteristicNotification(_characteristic, true);
                var descriptor = _characteristic.GetDescriptor(UUID.FromString(NotifyDescriptorUuid));

                descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                _gatt.WriteDescriptor(descriptor);
            }
        private void SubscribeCharacteristic(BluetoothGattCharacteristic characteristic)
        {
            _gatt.SetCharacteristicNotification(characteristic, true);

            var descriptor = characteristic.GetDescriptor(UUID.FromString("00002902-0000-1000-8000-00805f9b34fb"));

            descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
            _gatt.WriteDescriptor(descriptor);
        }
 private void EnableGlucoseContextNotification(BluetoothGatt gatt)
 {
     if (_glucoseMeasurementContextCharacteristic != null)
     {
         gatt.SetCharacteristicNotification(_glucoseMeasurementContextCharacteristic, true);
         BluetoothGattDescriptor descriptor =
             _glucoseMeasurementContextCharacteristic.GetDescriptor(BLEHelpers.BLE_DESCRIPTOR);
         descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
         gatt.WriteDescriptor(descriptor);
     }
 }
        private void EnableRecordAccessControlPointIndication(BluetoothGatt gatt)
        {
            if (_racpCharacteristic == null)
            {
                return;
            }
            gatt.SetCharacteristicNotification(_racpCharacteristic, true);
            BluetoothGattDescriptor descriptor = _racpCharacteristic.GetDescriptor(BLEHelpers.BLE_DESCRIPTOR);

            descriptor.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
            gatt.WriteDescriptor(descriptor);
        }
        private void EnableTimeSyncIndication(BluetoothGatt gatt)
        {
            if (_customTimeCharacteristic == null)
            {
                return;
            }
            gatt.SetCharacteristicNotification(_customTimeCharacteristic, true);
            BluetoothGattDescriptor descriptor = _customTimeCharacteristic.GetDescriptor(BLEHelpers.BLE_DESCRIPTOR);

            descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
            gatt.WriteDescriptor(descriptor);
        }
        private static void SetCharacteristicNotification_private(BluetoothGatt gatt, UUID serviceUuid, UUID characteristicUuid)
        {
            try {
                BluetoothGattCharacteristic characteristic = gatt.GetService(serviceUuid).GetCharacteristic(characteristicUuid);
                gatt.SetCharacteristicNotification(characteristic, true);
                BluetoothGattDescriptor descriptor = characteristic.GetDescriptor(BLEHelpers.ClientCharacteristicConfig);
                bool indication = (Convert.ToInt32(characteristic.Properties) & 32) != 0;
                descriptor.SetValue(indication
                    ? BluetoothGattDescriptor.EnableIndicationValue.ToArray()
                    : BluetoothGattDescriptor.EnableNotificationValue.ToArray());

                gatt.WriteDescriptor(descriptor);
            } catch (Exception e) {
                Log.Error("BloodPressureGattCallbackerror", e.Message);
            }
        }
예제 #15
0
        /// <summary>
        ///
        /// </summary>
        public virtual void EnableTXNotification()
        {
            if (m_Gatt == null)
            {
                Log.e("BleSerialPort", "No BluetoothGatt to EnableTXNotification!!!");
                return;
            }

            // Get the service.
            BluetoothGattService RxService = m_Gatt
                                             .GetService(m_UuidServ);

            if (RxService == null)
            {
                Log.e("BleSerialPort", "RxService==null");
                return;
            }

            // Get the characteristic.
            BluetoothGattCharacteristic txChar = RxService
                                                 .GetCharacteristic(m_UuidTx);

            if (txChar == null)
            {
                Log.e("BleSerialPort", "txChar==null");
                return;
            }

            // Set the characteristic notification.
            bool result = m_Gatt.SetCharacteristicNotification(txChar, true);

            if (!result)
            {
                Log.e("BleSerialPort", "m_Gatt.SetCharacteristicNotification(txChar,true) failed!!!");
            }

            // (Set the characteristic notification ???).
            BluetoothGattDescriptor descriptor = txChar
                                                 .GetDescriptor(m_UuidCCCD);

            descriptor.SetValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            m_Gatt.WriteDescriptor(descriptor);

            Log.i("BleSerialPort", "EnableTXNotification successfully.");
        }
예제 #16
0
        /**
         * Enables or disables notification on a give characteristic.
         *
         * @param characteristic Characteristic to act on.
         * @param enabled If true, enable notification.  False otherwise.
         */
        public void SetCharacteristicNotification(BluetoothGattCharacteristic characteristic, bool enabled)
        {
            if (mBluetoothAdapter == null || mBluetoothGatt == null)
            {
                Log.Warn(TAG, "BluetoothAdapter not initialized");
                return;
            }
            mBluetoothGatt.SetCharacteristicNotification(characteristic, enabled);

            // This is specific to Heart Rate Measurement.
            if (UUID_HEART_RATE_MEASUREMENT == characteristic.Uuid)
            {
                BluetoothGattDescriptor descriptor = characteristic.GetDescriptor(
                    UUID.FromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
                descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                mBluetoothGatt.WriteDescriptor(descriptor);
            }
        }
예제 #17
0
        public void StartUpdates()
        {
            Console.WriteLine("Enabling indication/notification...");
            BluetoothGattDescriptor descriptor = _nativeCharacteristic.GetDescriptor(UUID.FromString("00002902-0000-1000-8000-00805f9b34fb"));

            Console.WriteLine("Descriptor UUID: " + descriptor.Uuid.ToString());
            if ((_nativeCharacteristic.Properties & GattProperty.Indicate) == GattProperty.Indicate)
            {
                Console.WriteLine("Enabling indication");
                descriptor.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
            }

            if ((_nativeCharacteristic.Properties & GattProperty.Notify) == GattProperty.Notify)
            {
                Console.WriteLine("Enabling notification");
                descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
            }
            _gatt.WriteDescriptor(descriptor);
        }
        private static void SetCharacteristicNotification_private(BluetoothGatt gatt, UUID serviceUuid,
                                                                  UUID characteristicUuid)
        {
            try {
                bool indication;
                BluetoothGattCharacteristic characteristic =
                    gatt.GetService(serviceUuid).GetCharacteristic(characteristicUuid);
                gatt.SetCharacteristicNotification(characteristic, true);
                BluetoothGattDescriptor descriptor =
                    characteristic.GetDescriptor(BLEHelpers.ClientCharacteristicConfig);
                indication = (Convert.ToInt32(characteristic.Properties) & 32) != 0;
                Log.Error("Indication", indication.ToString());
                descriptor.SetValue(indication
                                        ? BluetoothGattDescriptor.EnableIndicationValue.ToArray()
                                        : BluetoothGattDescriptor.EnableNotificationValue.ToArray());

                gatt.WriteDescriptor(descriptor);
            } catch (Exception e) {
                e.PrintStackTrace();
            }
        }
예제 #19
0
        /// <summary>
        /// Process a callback from the Gatt for device services discovered while we're connecting
        /// </summary>
        private void GattCallback_ServicesDiscovered(object sender, EventArgs e)
        {
            Debug.WriteLineIf(sw.TraceInfo, "++> GattCallback_ServicesDiscovered");
            _service = _gatt.GetService(uuidService);
            _TX      = _service.GetCharacteristic(uuidTX);
            _RX      = _service.GetCharacteristic(uuidRX);
            BluetoothGattDescriptor config = _RX.GetDescriptor(uuidCharacteristicConfig);

            if (config == null)
            {
                Debug.WriteLineIf(sw.TraceError, "--> _RX.GetDescriptor failed");
                ErrorMessage = "RX.GetDescriptor failed";
                Disconnect();
                return;
            }
            bool b = _gatt.SetCharacteristicNotification(_RX, true);

            b = config.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
            b = _gatt.WriteDescriptor(config);
            // now that services are connected, we're ready to go
            State = BlueState.Connected;
        }
예제 #20
0
        /// <summary>
        /// Connets the service and enable notifications for the chars we need
        /// </summary>
        /// <param name="gatt">Gatt.</param>
        public void connectToService(BluetoothGatt gatt)
        {
            try
            {
                if (handle == null)
                {
                    handle = new SensorHandler(deviceDetails, AppConfig.UserID);
                }

                BluetoothDevice d = gatt.Device;

                // Getting Char/services UUIDS
                UUID serviceUUID = null;
                UUID charUUID    = null;
                UUID ccdUUID     = UUID.FromString(BluetoothConstants.CCD_UUID);

                // Can add other ones later or maybe swap out config files?
                if (gatt.Device.Name.StartsWith("MVSS", StringComparison.CurrentCulture))
                {
                    serviceUUID = UUID.FromString(BluetoothConstants.MVSS_SERVICE);
                    charUUID    = UUID.FromString(BluetoothConstants.MVSS_CHAR);
                }

                if (gatt.Device.Name.StartsWith("Zephyr", StringComparison.CurrentCulture))
                {
                    serviceUUID = UUID.FromString(BluetoothConstants.HEART_RATE_SERVICE);
                    charUUID    = UUID.FromString(BluetoothConstants.HEART_RATE_CHAR);
                }


                /*	if (serviceUUID == null || charUUID == null || ccdUUID == null)
                 *  {
                 *      // dont continue
                 *      // throw error?
                 *  }*/

                notifyChars.Add(serviceUUID.ToString());
                notifyChars.Add(charUUID.ToString());

                // Getting Service
                BluetoothGattService ser = gatt.GetService(serviceUUID);

                // Getting custom characteristic and enabling the notifications for it
                BluetoothGattCharacteristic cha = ser.GetCharacteristic(charUUID);

                // Getting Descriptor from characteristic
                BluetoothGattDescriptor ds = cha.GetDescriptor(ccdUUID);

                // Setting desc to notify
                ds.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                //ds.SetValue(BluetoothGattDescriptor.EnableIndicationValue.ToArray());
                gatt.WriteDescriptor(ds);

                // Enabling the notifications
                gatt.SetCharacteristicNotification(cha, true);
            }
            catch (NullReferenceException e)
            {
                // Service could not be reached
                // How to get this back to the user?
                string t = e.ToString();
            }
        }