Пример #1
0
        /// <summary> Subscribes to the changes notifications of the given characteristic. </summary>
        /// <param name="Characteristic"> The characteristic. </param>
        /// <returns> If the method completed with success the returning value is
        ///   <see cref="wclErrors.WCL_E_SUCCESS" />. If the method failed the returning value is
        ///   one of the Bluetooth Framework error code. </returns>
        /// <seealso cref="wclGattCharacteristic"/>
        protected Int32 SubscribeForNotifications(wclGattCharacteristic?Characteristic)
        {
            if (Characteristic == null)
            {
                return(wclBluetoothErrors.WCL_E_BLUETOOTH_LE_ATTRIBUTE_NOT_FOUND);
            }

            // Windows does not support dual indicatable and notifiable chars so select one.
            wclGattCharacteristic chr = Characteristic.Value;

            if (chr.IsIndicatable && chr.IsNotifiable)
            {
                chr.IsIndicatable = false;
            }
            Int32 Res = FClient.Subscribe(chr);

            if (Res == wclErrors.WCL_E_SUCCESS)
            {
                Res = FClient.WriteClientConfiguration(chr, true, wclGattOperationFlag.goNone);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    FClient.Unsubscribe(chr);
                }
            }
            return(Res);
        }
Пример #2
0
        private void Subscribe(bool doSubscribe)
        {
            if (doSubscribe)
            {
                int Res = Client.Subscribe(ReadCh);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    subscription = "Subscription failed";
                    return;
                }
                else if (!hasCCCD)
                {
                    subscription = "Subscription without CCCD succeeded";
                    return;
                }
                Res = Client.WriteClientConfiguration(ReadCh, true, wclGattOperationFlag.goReadFromDevice);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    subscription = "CCCD write failed";
                    return;
                }

                Res = Client.ReadDescriptorValue(CCCDdescriptor, wclGattOperationFlag.goReadFromDevice, out wclGattDescriptorValue cccdVal);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    subscription = "CCCD read failed";
                    return;
                }
                subscription = cccdVal.ClientCharacteristicConfiguration.IsSubscribeToNotification
                    ? "CCCD subscribed to notification" : "CCCD not subscribed";
            }
            else
            {
                int Res = Client.Unsubscribe(ReadCh);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    subscription = "Unsubscription failed";
                    return;
                }
                if (hasCCCD)
                {
                    Res = Client.WriteClientConfiguration(ReadCh, false, wclGattOperationFlag.goReadFromDevice);
                    if (Res != wclErrors.WCL_E_SUCCESS)
                    {
                        subscription = "CCCD write failed";
                        return;
                    }
                }
                subscription = "Unsubscription succeeded";
            }
        }
Пример #3
0
        void FClient_OnConnect(object Sender, int Error)
        {
            if (Error != wclErrors.WCL_E_SUCCESS)
            {
                Trace("Connection failed: 0x" + Error.ToString("X8"));
                CloseBluetoothManager();
            }
            else
            {
                FSubscribed = new List <wclGattCharacteristic>();

                Trace("Connected with success");

                Trace("Read services");
                wclGattService[] Services;
                Int32            Res = FClient.ReadServices(wclGattOperationFlag.goNone, out Services);
                if (Res != wclErrors.WCL_E_SUCCESS)
                {
                    Trace("Read services failed: 0x" + Res.ToString("X8"));
                }
                else
                {
                    foreach (wclGattService Service in Services)
                    {
                        Trace("Service " + ShowUuid(Service.Uuid));
                        wclGattCharacteristic[] Chars;
                        Res = FClient.ReadCharacteristics(Service, wclGattOperationFlag.goNone, out Chars);
                        if (Res != wclErrors.WCL_E_SUCCESS)
                        {
                            Trace("  Unable to read characteristics: 0x" + Res.ToString("X8"));
                        }
                        else
                        {
                            foreach (wclGattCharacteristic Char in Chars)
                            {
                                Trace("  Characteristic " + ShowUuid(Char.Uuid));
                                if (Char.IsReadable)
                                {
                                    Trace("    Readable");
                                }
                                if (Char.IsWritable || Char.IsSignedWritable || Char.IsWritableWithoutResponse)
                                {
                                    Trace("    Writable");
                                }
                                if (Char.IsIndicatable || Char.IsNotifiable)
                                {
                                    Trace("    Notifiable");
                                    Trace("    Subscribe");
                                    Res = FClient.Subscribe(Char);
                                    if (Res != wclErrors.WCL_E_SUCCESS)
                                    {
                                        Trace("      Subscribed failed: 0x" + Res.ToString("X8"));
                                    }
                                    else
                                    {
                                        Res = FClient.WriteClientConfiguration(Char, true, wclGattOperationFlag.goNone, wclGattProtectionLevel.plNone);
                                        if (Res != wclErrors.WCL_E_SUCCESS)
                                        {
                                            Trace("      Write CCD failed: 0x" + Res.ToString("X8"));
                                            FClient.Unsubscribe(Char);
                                        }
                                        else
                                        {
                                            Trace("      Subscribed");
                                            FSubscribed.Add(Char);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                btDisconnect.Enabled = true;
            }
        }