Beispiel #1
0
        public IGattCharacteristicBuilder SetNotification(Action <CharacteristicSubscription> onSubscribe = null, NotificationOptions options = NotificationOptions.Notify)
        {
            this.onSubscribe = onSubscribe;
            if (options.HasFlag(NotificationOptions.Indicate))
            {
                this.properties = GattProperty.Indicate;
            }

            if (options.HasFlag(NotificationOptions.Notify))
            {
                this.properties = GattProperty.Notify;
            }

            return(this);
        }
Beispiel #2
0
        public IGattCharacteristicBuilder SetRead(Func <ReadRequest, ReadResult> onRead, bool encrypted = false)
        {
            this.onRead      = onRead;
            this.properties |= GattProperty.Read;
            if (encrypted)
            {
                this.permissions |= GattPermission.ReadEncrypted;
            }
            else
            {
                this.permissions |= GattPermission.Read;
            }

            return(this);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public IGattCharacteristicBuilder SetWrite(Func <WriteRequest, GattState> onWrite, WriteOptions options = WriteOptions.Write)
        {
            this.onWrite = onWrite;
            if (options.HasFlag(WriteOptions.EncryptionRequired))
            {
                this.permissions = GattPermission.WriteEncrypted;
            }
            else if (options.HasFlag(WriteOptions.AuthenticatedSignedWrites))
            {
                this.properties  |= GattProperty.SignedWrite;
                this.permissions |= GattPermission.WriteSigned;
            }
            else
            {
                this.properties  |= GattProperty.Write;
                this.permissions |= GattPermission.Write;
            }
            if (options.HasFlag(WriteOptions.WriteWithoutResponse))
            {
                this.properties |= GattProperty.WriteNoResponse;
            }

            return(this);
        }
            /// <summary>
            /// Method that loads service and characteristics and checks they are suitable
            /// </summary>
            /// <param name="p0">Gatt server</param>
            /// <returns>True if successful</returns>
            protected override bool IsRequiredServiceSupported(BluetoothGatt p0)
            {
                if (_manager._services != null)
                {
                    foreach (ServiceAndCharacteristicsParcel parcel in _manager._services)
                    {
                        // Get the service
                        BluetoothGattService service = p0.GetService(UUID.FromString(parcel.Service.ToString()));
                        if (service != null)
                        {
                            // Check each characteristic
                            foreach (KeyValuePair <Guid, CharacteristicsParcel> characteristicsParcel in parcel.Characteristics)
                            {
                                if (!_manager._characteristics.ContainsKey(characteristicsParcel.Key))
                                {
                                    // Get the characteristic
                                    BluetoothGattCharacteristic characteristic = service.GetCharacteristic(UUID.FromString(characteristicsParcel.Key.ToString()));

                                    if (characteristic != null)
                                    {
                                        // Add the service to the local dictionary
                                        _manager._characteristics.Add(characteristicsParcel.Key, characteristic);

                                        // Find the required properties of this characteristic
                                        CharacteristicProperties properties = characteristicsParcel.Value.Properties;

                                        // Now check that the characteristic supports the required properties
                                        GattProperty rxProperties = characteristic.Properties;

                                        // Read request
                                        if (properties.HasFlag(CharacteristicProperties.Read))
                                        {
                                            if (!rxProperties.HasFlag(GattProperty.Read))
                                            {
                                                return(false);
                                            }
                                        }

                                        // Write request
                                        if (properties.HasFlag(CharacteristicProperties.Write))
                                        {
                                            if (!rxProperties.HasFlag(GattProperty.Write))
                                            {
                                                return(false);
                                            }
                                        }

                                        // Notifications
                                        if (properties.HasFlag(CharacteristicProperties.Notify))
                                        {
                                            if (!rxProperties.HasFlag(GattProperty.Notify))
                                            {
                                                return(false);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                }
                return(true);
            }
Beispiel #6
0
 public static BLECharacteristicProperty ToShared(this GattProperty properties)
 => (BLECharacteristicProperty)properties;