private async void ConnectBtn_Click(object sender, EventArgs e) { var device = _listViewAdapter.GetSelectedDevice(); if (device != null) { await _adapter.ConnectToDeviceAsync(device); if (device.State == Plugin.BLE.Abstractions.DeviceState.Connected) { _inodeDevice = new InodeDevice(device); await _inodeDevice.InitAsync(); } } }
private async void _adapter_DeviceDiscovered(object sender, Plugin.BLE.Abstractions.EventArgs.DeviceEventArgs e) { if (_status == Status.STATUS_SCANNING_FOR_DEVICE) { _log("_adapter_DeviceDiscovered", $"Device {e.Device.Name}"); if (e.Device.Name == _deviceName) { try { _device = e.Device; await _adapter.ConnectToDeviceAsync(_device); _serviceHR = await e.Device.GetServiceAsync(SERVICE_HEARTRATE); _characteristicHR = await _serviceHR?.GetCharacteristicAsync(CHARACTERISTIC_HEARTRATE); _serviceBattery = await e.Device.GetServiceAsync(SERVICE_BATTERY); if (_serviceBattery != null) { _characteristicBatteryLevel = await _serviceBattery?.GetCharacteristicAsync(CHARACTERISTIC_BATTERYLEVEL); } else { _characteristicBatteryLevel = null; } if (_characteristicHR != null) { _status = Status.STATUS_CONNECTED; _cancelScanningForDevice(); _characteristicHR.ValueUpdated += CharacteristicHR_ValueUpdated; await _characteristicHR.StartUpdatesAsync(); _log("_adapter_DeviceDiscovered", $"Device {e.Device.Name} STARTED"); } } catch (Exception err) { _log("_adapter_DeviceDiscovered", err.Message); } } } }
private async void BtnConnectClicked(object sender, EventArgs e) { try { if (device != null && !ble.Adapter.IsScanning) { try { if (device.State == Plugin.BLE.Abstractions.DeviceState.Connected) { this.RunOnUiThread(() => { Toast.MakeText(this, "Connection sucessfull!!", ToastLength.Long).Show(); }); } else { var token = new CancellationTokenSource(); ConnectParameters connectParams = new ConnectParameters(false); await adapter.StopScanningForDevicesAsync(); await adapter.ConnectToDeviceAsync(device, connectParams, token.Token); token.Cancel(); this.RunOnUiThread(() => { Toast.MakeText(this, "Connection sucessfull!!", ToastLength.Long).Show(); }); } } catch (System.Exception exp) { this.RunOnUiThread(() => { Toast.MakeText(this, "Notice in connect: " + exp.Message, ToastLength.Long).Show(); }); } } else { Toast.MakeText(this, "No Device selected", ToastLength.Long).Show(); } } catch (System.Exception exp) { Toast.MakeText(this, "Notice: " + exp.Message, ToastLength.Long).Show(); } }
private async void bleScan() { IDevice esp32 = null; while (esp32 == null) { Android.Util.Log.Debug("BLE", "Scanning"); adapter.ScanTimeout = 10000; adapter.ScanMode = ScanMode.LowLatency; adapter.DeviceDiscovered += async (s, a) => { Android.Util.Log.Debug("BLE", a.Device.Id.ToString() + " (" + a.Device.Name + ")"); if(a.Device.Id.ToString() == "00000000-0000-0000-0000-d8a01d463ef2") { Android.Util.Log.Debug("BLE", "ESP32 Found"); esp32 = a.Device; } }; await adapter.StartScanningForDevicesAsync(); } if(esp32 != null) { await adapter.StopScanningForDevicesAsync(); await adapter.ConnectToDeviceAsync(esp32); if (esp32.State == DeviceState.Disconnected) { Android.Util.Log.Debug("BLE", "ESP32 Connect failed"); } if (esp32.State == DeviceState.Connected || esp32.State == DeviceState.Limited) { Android.Util.Log.Debug("BLE", "ESP32 Connected"); IService svc = await esp32.GetServiceAsync(new Guid(ServiceUUID.ToLower())); var chr = await svc.GetCharacteristicsAsync(); foreach (var characteristic in chr) { var uuid = characteristic.Uuid.ToUpper(); if (characteristic.CanUpdate) { await characteristic.StartUpdatesAsync(); characteristic.ValueUpdated += Characteristic_ValueUpdated; } } } } }
private async Task Connect() { BluetoothDevice nativeDevice; if (!_isConnecting) { _isConnecting = true; nativeDevice = (BluetoothDevice)_band.Device.NativeDevice; using (CancellationTokenSource tokenSource = new CancellationTokenSource()) { try { Log.Debug("MAIN", "##### Trying to connect..."); await _adapter.ConnectToDeviceAsync(_band.Device, true, tokenSource.Token).TimeoutAfter(TimeSpan.FromSeconds(10), tokenSource); if (nativeDevice.BondState == Bond.None) { Log.Debug("MAIN", "##### Bonding..."); nativeDevice.CreateBond(); } else { Log.Debug("MAIN", "##### Already bonded"); } _isConnecting = false; } catch (Exception ex) { Log.Error("MAIN", $"Connection attempt failed: {ex.Message}"); _isConnecting = false; await Connect(); } } } else { Log.Debug("MAIN", "##### Connection underway, skipping attempt"); } }
private async void ConnectToPMDDeviceAsync() { string errorstr = ""; if (_bleAdapter.DiscoveredDevices.Count > 0) { try { await _bleAdapter.ConnectToDeviceAsync(_currDevice); _isConnectedButton.SetBackgroundColor(Android.Graphics.Color.LawnGreen); StopScanningForDevice(); Toast.MakeText(this, "Connected to " + _currDevice.Name + "!", ToastLength.Short).Show(); Button transmitButton = FindViewById <Button>(Resource.Id.trasmitData); transmitButton.Enabled = true; } catch (DeviceConnectionException er) { errorstr = er.Message; } } }