private Int32 ReadCharacteristics(wclGattService Service) { // Did we already read the characteristics for given service? if (FCharacteristics != null) { return(wclErrors.WCL_E_SUCCESS); } Int32 Res = FClient.ReadCharacteristics(Service, wclGattOperationFlag.goNone, out FCharacteristics); if (Res != wclErrors.WCL_E_SUCCESS) { return(Res); } if (FCharacteristics == null) { return(wclBluetoothErrors.WCL_E_BLUETOOTH_LE_ATTRIBUTE_NOT_FOUND); } return(wclErrors.WCL_E_SUCCESS); }
public bool GetFeatures() { stage = 4; int Res = Client.ReadCharacteristics(UEIservice, wclGattOperationFlag.goReadFromDevice, out Characteristics); if (Res != wclErrors.WCL_E_SUCCESS || Characteristics == null) { return(false); } stage = 5; int flags = 0; foreach (wclGattCharacteristic Character in Characteristics) { String s; if (Character.Uuid.IsShortUuid) { s = Character.Uuid.ShortUuid.ToString("X4"); } else { s = Character.Uuid.LongUuid.ToString(); } if (s.Equals("FFE1")) { WriteCh = Character; flags |= 1; } else if (s.Equals("FFE2")) { ReadCh = Character; flags |= 2; } } if (flags != 3) { return(false); } stage = 6; Res = Client.ReadDescriptors(ReadCh, wclGattOperationFlag.goReadFromDevice, out Descriptors); if (Res != wclErrors.WCL_E_SUCCESS || Descriptors == null) { return(false); } stage = 7; foreach (wclGattDescriptor Descriptor in Descriptors) { String s; if (Descriptor.Uuid.IsShortUuid) { s = Descriptor.Uuid.ShortUuid.ToString("X4"); } else { s = Descriptor.Uuid.LongUuid.ToString(); } if (s.Equals("2902")) { hasCCCD = true; CCCDdescriptor = Descriptor; } else if (s.Equals("2901")) { ReadChUserDesc = Descriptor; } } stage = 8; Res = Client.ReadDescriptors(WriteCh, wclGattOperationFlag.goReadFromDevice, out Descriptors); if (Res != wclErrors.WCL_E_SUCCESS || Descriptors == null) { return(false); } foreach (wclGattDescriptor Descriptor in Descriptors) { String s; if (Descriptor.Uuid.IsShortUuid) { s = Descriptor.Uuid.ShortUuid.ToString("X4"); } else { s = Descriptor.Uuid.LongUuid.ToString(); } if (s.Equals("2901")) { WriteChUserDesc = Descriptor; } } stage = 9; Subscribe(true); Client.OnCharacteristicChanged += Client_OnCharacteristicChanged; return(true); }
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; } }
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(); }
private void GetService() { _fServices = null; var resServices = _client.ReadServices(wclGattOperationFlag.goNone, out _fServices); if (resServices != wclErrors.WCL_E_SUCCESS) { MessageBox.Show("Error: 0x" + resServices.ToString("X8"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); TraceEvent(_client.Address, "Services", "FF00", "Failed"); _client.Disconnect(); return; } TraceEvent(_client.Address, "Services", "FF00", "Success"); if (_fServices == null) { return; } foreach (var service in _fServices) { if (service.Uuid.IsShortUuid) { if (_uuid.ShortUuid == service.Uuid.ShortUuid) { var resChar = _client.ReadCharacteristics(service, wclGattOperationFlag.goNone, out _fCharacteristics); if (resChar != wclErrors.WCL_E_SUCCESS) { return; } foreach (var character in _fCharacteristics) { if (character.Uuid.ShortUuid == _rxUuid) { var resSubs = _client.Subscribe(character); if (resSubs != wclErrors.WCL_E_SUCCESS) { TraceEvent(_client.Address, "Subs", "RX", "Failed"); return; } else { TraceEvent(_client.Address, "Subs", "RX", "Success"); var resReadDesc = _client.ReadDescriptors(character, wclGattOperationFlag.goNone, out _fDescriptors); if (resReadDesc != wclErrors.WCL_E_SUCCESS) { TraceEvent(_client.Address, "Subs", "RX", "Failed to get description"); return; } } } else if (character.Uuid.ShortUuid == _txUuid) { _txCharacteristic = character; } } } } } }