private void FClient_OnConnect(object Sender, int Error) { if (Error != wclErrors.WCL_E_SUCCESS) { lbLog.Items.Add("Connect failed: 0x" + Error.ToString("X8")); FManager.Close(); } else { wclGattService[] Services; Int32 Res = FClient.ReadServices(wclGattOperationFlag.goReadFromCache, out Services); if (Res != wclErrors.WCL_E_SUCCESS) { lbLog.Items.Add("Read services failed: 0x" + Res.ToString("X8")); } else { foreach (wclGattService s in Services) { lbLog.Items.Add(s.ToString()); } FClient.Disconnect(); } FManager.Close(); } }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { _client.Disconnect(); _client = null; _manager.Close(); _manager = null; Cleanup(); }
public string DisconnectUEI() { Subscribe(false); if (Client.State == wclClientState.csDisconnected) { return("Already disconnected"); } int Res = Client.Disconnect(); return(Res == wclErrors.WCL_E_SUCCESS ? "Disconnection succeeded" : "Disconnection failed"); }
private void btDisconnect_Click(object sender, EventArgs e) { Trace("Unsubscribe from all characteristics"); foreach (wclGattCharacteristic Char in FSubscribed) { FClient.WriteClientConfiguration(Char, false, wclGattOperationFlag.goNone, wclGattProtectionLevel.plNone); FClient.Unsubscribe(Char); } FSubscribed.Clear(); FSubscribed = null; Trace("Disconnecting"); FClient.Disconnect(); }
/* * public async void ConnectLostAsync() * { * _readerState = READERSTATE.READYFORDISCONNECT; * * _characteristicUpdate.ValueUpdated -= BLE_Recv; * _adapter.DeviceConnectionLost -= OnDeviceConnectionLost; * * _characteristicUpdate = null; * _characteristicWrite = null; * _service = null; * * try * { * if (_device.State == DeviceState.Connected) * { * await _adapter.DisconnectDeviceAsync(_device); * } * } * catch (Exception ex) * { * } * _device = null; * * _readerState = READERSTATE.DISCONNECT; * * FireReaderStateChangedEvent(new Events.OnReaderStateChangedEventArgs(null, Constants.ReaderCallbackType.CONNECTION_LOST)); * } */ async Task ClearConnection() { _readerState = READERSTATE.READYFORDISCONNECT; Int32 Res = wclErrors.WCL_E_MB_NOT_CREATED, errCnt = 0; while (Res != wclErrors.WCL_E_SUCCESS && errCnt++ < 20) { Res = Client.Disconnect(); //if (Res != wclErrors.WCL_E_SUCCESS) CSLibrary.Debug.WriteLine("Disconnect Error: 0x" + Res.ToString("X8")); } _readerState = READERSTATE.DISCONNECT; /* * // Stop Timer; * await _characteristicUpdate.StopUpdatesAsync(); * * _characteristicUpdate.ValueUpdated -= BLE_Recv; * _adapter.DeviceConnectionLost -= OnDeviceConnectionLost; * * _characteristicUpdate = null; * _characteristicWrite = null; * _service = null; * * try * { * if (_device.State == DeviceState.Connected) * { * await _adapter.DisconnectDeviceAsync(_device); * } * } * catch (Exception ex) * { * } * _device = null; * * _readerState = READERSTATE.DISCONNECT; */ }
// GATT client connect event handler. private void ClientConnect(Object Sender, Int32 Error) { // If connection was not established simple fire the event with error code. if (Error != wclErrors.WCL_E_SUCCESS) { DoConnected(Error); } else { FHubConnected = true; // Read services. We do it only once to save connection time. wclGattService[] Services; Int32 Res = FClient.ReadServices(wclGattOperationFlag.goNone, out Services); if (Res == wclErrors.WCL_E_SUCCESS) { if (Services == null) { Res = wclBluetoothErrors.WCL_E_BLUETOOTH_LE_ATTRIBUTE_NOT_FOUND; } else { // Try to connect to WeDo services. Res = FDeviceInformation.Connect(Services); if (Res == wclErrors.WCL_E_SUCCESS) { Res = FBatteryLevel.Connect(Services); } if (Res == wclErrors.WCL_E_SUCCESS) { Res = FIo.Connect(Services); } if (Res == wclErrors.WCL_E_SUCCESS) { Res = FHub.Connect(Services); } if (Res == wclErrors.WCL_E_SUCCESS) { // It does not matter if the function completed with or without success! FHub.ReadBatteryType(out FBatteryType); } Services = null; } } // If all the operations were success (services found, characteristics are found too) // we have to set connection flag. if (Res == wclErrors.WCL_E_SUCCESS) { FConnected = true; } else { // Otherwise we have to disconnect! FClient.Disconnect(); } // Now we can fire the OnConnected event. DoConnected(Res); } }