public async void PublishService() { await CreateService(); AdvertisementData advertisementData = new AdvertisementData { LocalName = "RemoteX Controller" }; CrossBleAdapter.Current.Advertiser.Start(advertisementData); }
protected virtual IObservable <ScanResult> PreLollipopScan(ScanConfig config) => Observable.Create <ScanResult>(ob => { var cb = new PreLollipopScanCallback((native, rssi, sr) => { var ad = new AdvertisementData(sr); if (this.IsFiltered(ad, config)) { var scanResult = this.ToScanResult(native, rssi, ad); ob.OnNext(scanResult); } }); this.manager.Adapter.StartLeScan(cb); return(() => this.manager.Adapter.StopLeScan(cb)); });
protected virtual IObservable <ScanResult> PreLollipopScan(ScanConfig config) => Observable.Create <ScanResult>(ob => { this.oldCallbacks = new PreLollipopScanCallback((native, rssi, sr) => { var ad = new AdvertisementData(sr); if (this.IsFiltered(ad, config)) { var scanResult = this.ToScanResult(native, rssi, ad); ob.OnNext(scanResult); } }); #pragma warning disable CS0618 // Type or member is obsolete this.manager.Adapter.StartLeScan(this.oldCallbacks); return(() => this.manager.Adapter.StopLeScan(this.oldCallbacks)); #pragma warning restore CS0618 // Type or member is obsolete });
protected bool IsFiltered(AdvertisementData ad, ScanConfig config) { if (config.ServiceUuids == null || !config.ServiceUuids.Any() || ad.ServiceUuids == null || !ad.ServiceUuids.Any()) { return(true); } foreach (var adUuid in ad.ServiceUuids) { foreach (var uuid in config.ServiceUuids) { if (uuid.Equals(adUuid)) { return(true); } } } return(false); }
// Update Device Data public void UpdateDevice(AdvertisementData Device) { sqliteconnection.Update(Device); }
// Insert new Device to DB public void InsertDevice(AdvertisementData Device) { sqliteconnection.Insert(Device); }
public void UpdateDevice(AdvertisementData data) { _databaseHelper.UpdateDevice(data); }
public void InsertDevice(AdvertisementData data) { _databaseHelper.InsertDevice(data); }
public async Task CreateServer() { if (CrossBleAdapter.Current.Status == AdapterStatus.PoweredOn) { try { RaiseInfoEvent("Creating Gatt Server"); _server = await CrossBleAdapter.Current.CreateGattServer(); RaiseInfoEvent("Gatt Server Created"); _service = _server.CreateService(_primaryServiceUUID, true); RaiseInfoEvent("Primary Service Created"); _serverReadWriteCharacteristic = _service.AddCharacteristic ( _readWriteCharacteristicUUID, CharacteristicProperties.Read | CharacteristicProperties.Write | CharacteristicProperties.WriteNoResponse, GattPermissions.Read | GattPermissions.Write ); //_serverNotifyCharacteristic = _service.AddCharacteristic //( // _notifyServiceUUID, // CharacteristicProperties.Indicate | CharacteristicProperties.Notify, // GattPermissions.Read | GattPermissions.Write //); _serverReadWriteCharacteristic.WhenReadReceived().Subscribe(x => { _serverReadCount++; x.Value = Encoding.UTF8.GetBytes($"Server Response: {_serverReadCount}"); x.Status = GattStatus.Success; // you can optionally set a status, but it defaults to Success RaiseInfoEvent("Received Read Request"); }); _serverReadWriteCharacteristic.WhenWriteReceived().Subscribe(x => { var textReceivedFromClient = Encoding.UTF8.GetString(x.Value, 0, x.Value.Length); RaiseInfoEvent(textReceivedFromClient); }); RaiseInfoEvent("Characteristics Added"); var adData = new AdvertisementData { LocalName = _serverName, ServiceUuids = new List <Guid> { _primaryServiceUUID } }; var manufacturerData = new ManufacturerData { CompanyId = 1, Data = Encoding.UTF8.GetBytes("Tomorrow Never Dies") }; adData.ManufacturerData = manufacturerData; RaiseInfoEvent("Starting Ad Service"); CrossBleAdapter.Current.Advertiser.Start(adData); RaiseInfoEvent("Server and Service Started"); RaiseServerClientStarted(true); } catch (Exception e) { RaiseErrorEvent(e); } } else { var exception = new Exception("Bluetooth is OFF"); RaiseErrorEvent(exception); } }
private void HandleSelectedDevice(DeviceListItemViewModel device) { var config = new ActionSheetConfig(); if (device.IsConnected) { config.Add("Update RSSI", async() => { try { _userDialogs.ShowLoading(); await device.Device.UpdateRssiAsync(); device.RaisePropertyChanged(nameof(device.Rssi)); _userDialogs.HideLoading(); _userDialogs.ShowSuccess($"RSSI updated {device.Rssi}", 1000); } catch (Exception ex) { _userDialogs.HideLoading(); _userDialogs.ShowError($"Failed to update rssi. Exception: {ex.Message}"); } }); config.Destructive = new ActionSheetOption("Disconnect", () => DisconnectCommand.Execute(device)); } else { config.Add("Connect", async() => { if (await ConnectDeviceAsync(device)) { ShowViewModel <ServiceListViewModel>(new MvxBundle(new Dictionary <string, string> { { DeviceIdKey, device.Device.Id.ToString() } })); } }); //config.Add("Connect & Dispose", () => ConnectDisposeCommand.Execute(device)); config.Add("Save Advertised Data", () => { if (device.Device.AdvertisementRecords == null || device.Device.AdvertisementRecords.Count == 0) { _userDialogs.Alert("No Data Found"); return; } var advModel = new AdvertisementData() { Id = Guid.NewGuid(), DeviceId = device.Id.ToString(), Name = device.Name, Data = device.Device.AdvertisementRecords[0].Data }; _advertisementDataRepository.InsertDevice(advModel); }); } config.Add("Copy ID", () => CopyGuidCommand.Execute(device)); config.Cancel = new ActionSheetOption("Cancel"); config.SetTitle("Device Options"); _userDialogs.ActionSheet(config); }