/// <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); }
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"; } }
void Client_OnConnect(object Sender, int Error) { CSLibrary.Debug.WriteLine("Connected"); int Res; // Get Services FServices = null; Res = Client.ReadServices(wclGattOperationFlag.goNone, out FServices); if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("ReadServices Error: 0x" + Res.ToString("X8")); return; } if (FServices == null) { return; } //CS108Service = null; foreach (wclGattService Service in FServices) { if (Service.Uuid.IsShortUuid) { if (Service.Uuid.ShortUuid == 0x9800) { CS108Service = Service; } } } //if (CS108Service == null) // return; // Get Chatacteristics Res = Client.ReadCharacteristics(CS108Service, wclGattOperationFlag.goReadFromDevice, out FCharacteristics); if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("ReadCharacteristics Error: 0x" + Res.ToString("X8")); return; } if (FCharacteristics == null) { return; } foreach (wclGattCharacteristic Character in FCharacteristics) { String s; if (Character.Uuid.IsShortUuid) { if (Character.Uuid.ShortUuid == 0x9900) { WriteCharacteristic = Character; } else if (Character.Uuid.ShortUuid == 0x9901) { UpdateCharacteristic = Character; } } } Res = Client.Subscribe(UpdateCharacteristic); if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("Subscribe Error: 0x" + Res.ToString("X8")); } Res = Client.WriteClientConfiguration(UpdateCharacteristic, true, wclGattOperationFlag.goNone); // for library 7.3.9.0 above if (Res != wclErrors.WCL_E_SUCCESS) { CSLibrary.Debug.WriteLine("WriteClientConfiguration Error: 0x" + Res.ToString("X8")); } _readerState = READERSTATE.IDLE; BTTimer = new Timer(TimerFunc, this, 0, 1000); HardwareInit(); }
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; } }