protected override async void InitializeNative() { //create local helper using the app context BluetoothLEHelper localHelper = BluetoothLEHelper.Context; this._bluetoothHelper = localHelper; // BloubulLE: We want to get the actually state and its changes. try { RadioAccessStatus tAccessStatus = await Radio.RequestAccessAsync(); if (tAccessStatus == RadioAccessStatus.Allowed) { BluetoothAdapter tAdapter = await BluetoothAdapter.GetDefaultAsync(); if (tAdapter != null) { this.DefaultRadio = await tAdapter.GetRadioAsync(); if (this.DefaultRadio != null) { this.DefaultRadio.StateChanged += this.OnRadioStateChanged; this.OnRadioStateChanged(this.DefaultRadio, this); } } } } catch (Exception iEx) { //- } }
private async Task CheckBluetoothStatus(bool activateIfOff) { if (bluetoothRadio != null && bluetoothRadio.State != RadioState.On) { // if bluetooth is off and "activateIfOff" == true , try to activate it if (activateIfOff) { AddLog($"Bluetooth state: {bluetoothRadio.State.ToString()}, trying to turn it on...", AppLog.LogCategory.Debug); RadioAccessStatus radioAccess = RadioAccessStatus.Unspecified; try { radioAccess = await bluetoothRadio.SetStateAsync(RadioState.On); // try to turn on the bluetooth adapter } catch (Exception e) { AddLog($"Can't modify state of {bluetoothRadio.Name}. \nMessage: {e.Message}", AppLog.LogCategory.Debug); return; } // if the operation finished sucessfully change the bool variable if (radioAccess == RadioAccessStatus.Allowed && bluetoothRadio.State == RadioState.On) { AddLog($"Bluetooth activated. New state: {bluetoothRadio.State.ToString()}.", AppLog.LogCategory.Debug); IsBluetoothEnabled = true; } else { AddLog($"Something gone wrong. New bluetooth state: {bluetoothRadio.State.ToString()}.", AppLog.LogCategory.Debug); IsBluetoothEnabled = false; } // if activateIfOff == false, just change the state of the variable } else { AddLog($"Bluetooth state: {bluetoothRadio.State.ToString()}", AppLog.LogCategory.Debug); IsBluetoothEnabled = false; } // bluetooth == null means that there is no access to the bluetooth device } else if (bluetoothRadio == null) { AddLog("Can not comunicate with bluetooth.", AppLog.LogCategory.Error); IsBluetoothEnabled = false; return; // if radio state == on ht ebluetooth is already on } else if (bluetoothRadio != null && bluetoothRadio.State == RadioState.On) { AddLog($"Bluetooth state: {bluetoothRadio.State.ToString()}", AppLog.LogCategory.Debug); IsBluetoothEnabled = true; } }
public static AccessStatus ToAccessStatus(this RadioAccessStatus status) { switch (status) { case RadioAccessStatus.Allowed: return(AccessStatus.Success); case RadioAccessStatus.DeniedBySystem: return(AccessStatus.DeniedBySystem); case RadioAccessStatus.DeniedByUser: return(AccessStatus.DeniedByUser); default: return(AccessStatus.UnknownError); } }
/// <summary> /// If WiFi is off, will try to turn it on. Then it will return true if the WiFi is on. /// </summary> public async Task <bool> CheckRadio() { IReadOnlyList <Radio> MyRadios = await Radio.GetRadiosAsync(); foreach (var item in MyRadios) { if (item.Kind == RadioKind.WiFi) { RadioAccessStatus AccessStatus = await item.SetStateAsync(RadioState.On); if (AccessStatus != RadioAccessStatus.Allowed) { bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:privacy-radios")); } } if (item.State == RadioState.On) { return(true); } } return(false); }