void LookWhatYouMadeMeDo(BluetoothLeDevice device) { tmr = new Timer(1000); tmr.Elapsed += TMR_INTV; tmr.Start(); Sport_modebtn.Clicked += Sport_modebtn_Clicked; try { vibr = Vibrator.Vibrators.FirstOrDefault(); } catch { } if (device != null) { this.device = device; client = device.GattConnect(false); //Connect.Clicked += Connect_Clicked; WaiterReaderConnecter(); } else { } try { } catch { } }
void connect(int connected_index) { if (Core_list.SelectedItem != null) { BluetoothLeDevice core = devices[connected_index]; try { try { BluetoothAdapter.StopLeScan(); } catch { } Device = core; Navigation.PushModalAsync(new CoreView(core)); if (nononononono != null) { nononononono.Dispose(); } } catch (Exception ex) { exc = ex; DisplayAlert("Error", ex.ToString(), "OK"); } } }
/// <summary> /// Populates the device list and initializes all the various models. /// /// Waiting for the async calls to complete takes a while, so we want to call this /// function somewhat sparingly. /// </summary> /// <returns></returns> public static async Task PopulateDeviceListAsync() { // Remove all devices and start from scratch PairedDevices.Clear(); // Asynchronously get all paired/connected bluetooth devices. var infoCollection = await DeviceInformation.FindAllAsync(BluetoothLeDevice.GetDeviceSelector()); // Re-add devices foreach (var info in infoCollection) { // Make sure we don't initialize duplicates if (PairedDevices.FindIndex(device => device.DeviceId == info.Id) >= 0) { continue; } var wrtDevice = await BluetoothLeDevice.FromIdAsync(info.Id); var deviceM = new BeDeviceModel(); deviceM.Initialize(wrtDevice, info); PairedDevices.Add(deviceM); } /* * FUTURE * * Consider reading one characteristic from each device uncached to trigger a connection to the * device, in case the device does not have notifiable characteristics. * * Also consider registering for DeviceConnectionChangeTrigger, in case a device does not have * notifiable characteristics. But that may be overkill - what's the likelihood that a device * won't have notifiable characteristics? * */ }
public BluetoothLeMessageHandler(BluetoothLeDevice deviceModel, BluetoothLeProfile profile, IBluetoothLeCommander commander) { _discoveredDeviceList = new Dictionary <string, BluetoothLeDevice>(); _discoveredCharacteristics = new HashSet <BluetoothLeCharacteristic>(); _pendingCommands = new BluetoothMessageCommandQueue(); _deviceModel = deviceModel; _profile = profile; _commander = commander; }
public IActionResult GetBluetoothDevices(string name, double rssi, int txPower, double dist, bool isBeacon, int testNr) { var bluetoothDevice = new BluetoothLeDevice() { NameofBeacon = name, Rssi = rssi, TxPower = txPower, EstimatedDistance = dist, IsBeacon = isBeacon, TestNr = testNr }; _context.Add(bluetoothDevice); _context.SaveChanges(); return(Ok()); }
async Task Asker(BluetoothLeDevice e) { if (e.Rssi < -71 || e.Rssi > 71) { asked_devices.Add(e); bool accepted = await DisplayAlert("Rssi: " + e.Rssi.ToString(), "Do you want to connect to " + e.DeviceName, "Yes", "No"); if (accepted) { connect(devices.IndexOf(e)); } } }
/// <summary> /// Called when a Bluetooth Low Energy peripheral device was discovered. /// </summary> /// <param name="deviceAddress">The device address.</param> /// <param name="deviceName">Name of the device.</param> public void OnCentralDiscoveredDevice(string deviceAddress, string deviceName) { _deviceModel.BluetoothState = BluetoothLeDeviceState.DiscoveredDevices; BluetoothLeDevice device; if (!_discoveredDeviceList.TryGetValue(deviceAddress, out device)) { Debug.Log("Discovered peripheral:" + deviceName + ", " + deviceAddress); device = new BluetoothLeDevice(deviceAddress, deviceName); _discoveredDeviceList[deviceAddress] = device; } device.TimeStamp = DateTime.UtcNow; }
/// <summary> /// Unregisters GattConnectionStateChanged event handler /// and breaks the connection with GATT and GATT Client. /// </summary> /// <param name="leDevice">Bluetooth LE Device</param> public void GattClientExit(BluetoothLeDevice leDevice) { if (leDevice != null) { leDevice.GattConnectionStateChanged -= Device_GattConnectionStateChanged; leDevice.GattDisconnect(); leDevice = null; GattClient.DestroyClient(); GattClient = null; } StateChangedFlag = false; }
/// <summary> /// This function does all post processing. /// First it unregisters the event handler registered /// for GattConnectionStateChanged. Then it iniates GATT /// disconnect. Finally it destroys the GATT Client. /// </summary> /// <param name="leDevice">BluetoothLeDevice</param> public void GattClientExit(BluetoothLeDevice leDevice) { if (!leDevice.Equals(null)) { GattClient.ConnectionStateChanged -= Device_GattConnectionStateChanged; GattClient.DisconnectAsync(); leDevice = null; GattClient.Dispose(); GattClient = null; } StateChanged_flag = false; }
/// <summary> /// Connect BLE device via ID /// </summary> /// <param name="Id"></param> async void ConnectDevice(string Id) { // Note: BluetoothLEDevice.FromIdAsync must be called from a UI thread because it may prompt for consent. HaveData?.Invoke(this, "Connecting..."); BluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(Id); HaveData?.Invoke(this, "Reading service"); var res = await BluetoothLeDevice.GetGattServicesForUuidAsync(UARTServiceId); if (res.Status == GattCommunicationStatus.Success) { Service = res.Services.FirstOrDefault(); } else { HaveData?.Invoke(this, "service denied."); return; } HaveData?.Invoke(this, "Connecting to characteristic"); GattCharacteristicsResult cres = await Service.GetCharacteristicsForUuidAsync(RXCharacteristicsId); if (cres.Status == GattCommunicationStatus.Success) { Characteristic = cres.Characteristics.FirstOrDefault(); } else { HaveData?.Invoke(this, "Reading characteristic failed"); } Characteristic.ValueChanged += Characteristic_ValueChanged; HaveData?.Invoke(this, "Subscribing for notification"); GattCommunicationStatus status = await Characteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status == GattCommunicationStatus.Success) { // Server has been informed of clients interest. HaveData?.Invoke(this, "Subscribed."); } else { HaveData?.Invoke(this, "Subsrcription failed"); } }
public CoreView(BluetoothLeDevice device) { try { InitializeComponent(); Sport_modebtn.IsVisible = true;//!Variables.debug_mode; gtbtn.IsVisible = false; //gtbtn.IsVisible = Variables.debug_mode; if (Variables.ServerMode) { byte[] data = { 50 }; WebRequest request = WebRequest.Create(Variables.URL); request.Method = "POST"; request.GetRequestStream().Write(data, 0, data.Length); request.GetRequestStream().Close(); GC.SuppressFinalize(request); GC.Collect(); } if (device != null) { //4WaiterX60( ; LookWhatYouMadeMeDo(device); } if (Variables.debug_mode) { Timer tmr = new Timer(3000); tmr.AutoReset = true; tmr.Enabled = true; tmr.Elapsed += Tmr_Elapsed; tmr.Start(); //DisplayAlert("Info", "Running in debug mode", "OK"); } } catch (Exception ecx) { DisplayAlert("Error in Main Loop", ecx.Message, "OK"); } if (device == null) { ComputeTemp(true); } }
private void AddDeviceComponent(BluetoothLeDevice deviceToAdd) { var originalButton = GameObject.Find("DeviceButton0"); SetButtonVisibility(originalButton, false); GameObject newButton = (GameObject)Instantiate(originalButton); newButton.transform.SetParent(ListContentView.transform, false); //newButton.transform.position = originalButton.transform.position + new Vector3(0, -ListEntryHeight * this.deviceComponents.Count, 0); Text textComponent = newButton.transform.Find("Text").GetComponent <Text>(); textComponent.text = deviceToAdd.DisplayName + Environment.NewLine + deviceToAdd.Address; SetButtonVisibility(newButton, true); var component = newButton.GetComponent <Button>(); component.onClick.AddListener(() => OnConnect(deviceToAdd.Address)); _deviceComponents.Add(deviceToAdd, newButton); }
public IDevice GetDevice(BluetoothLeDevice btDevice) => this.devices.GetOrAdd( btDevice.RemoteAddress, x => new Device(btDevice) );
public AdvertisementData(BluetoothLeDevice native) { this.native = native; }
public SettingsPage(BluetoothLeDevice devi, ref Settings_t set) { InitializeComponent(); settings = set; }
public Device(BluetoothLeDevice native) { this.native = native; }
public AdvertisementData(BluetoothLeDevice native) { this.native = native; //this.native.GetServiceDataList().Select(x => x.ServiceUuid]) }
public ScanResult(BluetoothLeDevice native, IDevice device) { this.Device = device; this.Rssi = native.Rssi; this.AdvertisementData = new AdvertisementData(native); }
private void Disconnect_Clicked(object sender, EventArgs e) { GC.Collect(); device.GattDisconnect(); device = null; }