2015-10-08 24 views

risposta

2

C'è un modo per farlo? Posso anche controllare Bluetooth e Bluetooth Low Energy separatamente?

Cosa intendi per "dispositivo", è il dispositivo su cui viene eseguita l'app o il dispositivo ospita il servizio Bluetooth a cui l'app deve accedere?

Per quanto ne so, non ci sono API in UWP per verificare se Bluetooth è abilitato sul dispositivo.

Sul dispositivo Windows Mobile, è possibile utilizzare il seguente modo come soluzione alternativa.

private async void FindPaired() 
{ 
    // Search for all paired devices 
    PeerFinder.AlternateIdentities["Bluetooth:Paired"] = ""; 

    try 
    { 
     var peers = await PeerFinder.FindAllPeersAsync(); 

     // Handle the result of the FindAllPeersAsync call 
    } 
    catch (Exception ex) 
    { 
     if ((uint)ex.HResult == 0x8007048F) 
     { 
      MessageBox.Show("Bluetooth is turned off"); 
     } 
    } 
} 

Nel dispositivo PC Windows, vi suggerisco di controllare l'accessibilità dei servizi nel livello di servizio Bluetooth come una soluzione.

Per servizi non BLE come RFCOMM, è possibile ottenere il conteggio dei dispositivi con un ID servizio specifico. Se il Bluetooth è disattivato nel livello hardware, il conteggio sarà 0.

rfcommServiceInfoCollection = await DeviceInformation.FindAllAsync(
    RfcommDeviceService.GetDeviceSelector(RfcommServiceId.ObexObjectPush)); 

Per i servizi BLE, è possibile utilizzare la classe BluetoothLEAdvertisementWatcher per ricevere la pubblicità BLE. Se il Bluetooth è disabilitato a livello hardware, non verrà ricevuto nessun annuncio.

watcher = new BluetoothLEAdvertisementWatcher(); 
watcher.Received += OnAdvertisementReceived; 

     private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) 
     { 
      var address = eventArgs.BluetoothAddress; 
      BluetoothLEDevice device = await BluetoothLEDevice.FromBluetoothAddressAsync(address); 
      var cnt =device.GattServices.Count; 
      watcher.Stop(); 
     } 
+0

Aggiungere gestore per 'watcher.Stopped' e se BLE non è disponibile, si ha' args.Error == BluetoothError .RadioNotAvailable'. – xmedeko

8

Ho compiuto questo utilizzando la classe Radio.

Per verificare se Bluetooth è attivato:

public static async Task<bool> GetBluetoothIsEnabledAsync() 
{ 
    var radios = await Radio.GetRadiosAsync(); 
    var bluetoothRadio = radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth); 
    return bluetoothRadio != null && bluetoothRadio.State == RadioState.On; 
} 

Per verificare se è supportato Bluetooth (in generale):

public static async Task<bool> GetBluetoothIsSupportedAsync() 
{ 
    var radios = await Radio.GetRadiosAsync(); 
    return radios.FirstOrDefault(radio => radio.Kind == RadioKind.Bluetooth) != null; 
} 

Se non è installato Bluetooth, quindi non ci sarà alcun radio Bluetooth nell'elenco delle radio e la query LINQ restituirà null.

Come per il controllo di Bluetooth Classic e LE separatamente, attualmente sto studiando modi per farlo e aggiornerò questa risposta quando so per certo che esiste un modo e funziona.

1

miscelazione @Zenel risposta e nuova BluetoothAdapter classe (da Win 10 Creatori Update):

/// <summary> 
/// Check, if any Bluetooth is present and on. 
/// </summary> 
/// <returns>null, if no Bluetooth LE is installed, false, if BLE is off, true if BLE is on.</returns> 
public static async Task<bool?> IsBleEnabledAsync() 
{ 
    BluetoothAdapter btAdapter = await BluetoothAdapter.GetDefaultAsync(); 
    if (btAdapter == null) 
     return null; 
    if (!btAdapter.IsCentralRoleSupported) 
     return null; 
    var radios = await Radio.GetRadiosAsync(); 
    var radio = radios.FirstOrDefault(r => r.Kind == RadioKind.Bluetooth); 
    if (radio == null) 
     return null; // probably device just removed 
    // await radio.SetStateAsync(RadioState.On); 
    return radio.State == RadioState.On; 
}