2015-10-08 29 views

答えて

2

これを行う方法はありますか? BluetoothとBluetooth Low Energyを別々にチェックすることはできますか?

「デバイス」とは、アプリケーションが実行されているデバイス、またはデバイスがアクセスする必要のあるBluetoothサービスをホストしているデバイスを意味します。

私が知る限り、UWPにはBluetoothがデバイスで有効になっているかどうかを確認するAPIはありません。

Windows Mobileデバイスでは、回避策として次の方法を使用できます。

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"); 
     } 
    } 
} 

Windows PCデバイスでは、回避策として、Bluetoothサービスレベルでサービスのアクセシビリティをチェックすることをお勧めします。

RFCOMMのようなBLE以外のサービスの場合、特定のサービスIDを持つデバイスの数を取得できます。 Bluetoothは、ハードウェアレベルで無効になっている場合、カウントはBLEサービスについては0

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

だろう、あなたはBLEの広告を受信するBluetoothLEAdvertisementWatcherクラスを使用することができます。 Bluetoothがハードウェアレベルで無効になっている場合、広告は受信されません。

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

はwatcher.Stopped' 'にハンドラを追加し、BLEが利用できない場合、あなたは' args.Error == BluetoothErrorを得ました.RadioNotAvailable'。 – xmedeko

8

これはRadioクラスを使用してこれを達成しました。

は、Bluetoothが有効になっているかどうかを確認するには、次の

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; 
} 

は(一般的に)Bluetoothがサポートされているかどうかを確認するには:ブルートゥースがインストールされていない場合

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

、その後、何のBluetooth無線は存在しませんラジオリストでは、LINQクエリはnullを返します。

Bluetooth ClassicとLEを別々に調べてみると、私は現在その方法を調査しており、方法が存在していることがわかっているときにこの回答を更新します。 (10勝クリエイターUpdateから)@Zenelの答えと新しいBluetoothAdapterクラスをミキシング

1

/// <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; 
} 
関連する問題