2015-10-08 14 views
5

मैं यह जांचना चाहता हूं कि किसी डिवाइस पर ब्लूटूथ सक्षम है या नहीं (ताकि कोई ऐप उपयोगकर्ता इंटरैक्शन के बिना इसका उपयोग कर सके)। क्या उसे करने का कोई तरीका है? क्या मैं ब्लूटूथ और ब्लूटूथ कम ऊर्जा को अलग से देख सकता हूं?डिवाइस पर ब्लूटूथ सक्षम है या नहीं,

उत्तर

2

क्या ऐसा करने का कोई तरीका है? क्या मैं ब्लूटूथ और ब्लूटूथ कम ऊर्जा को अलग से देख सकता हूं?

"डिवाइस" से आपका क्या मतलब है, क्या यह डिवाइस डिवाइस पर चलता है, या डिवाइस उस ब्लूटूथ सेवा को होस्ट करता है जिसे ऐप को एक्सेस करने की आवश्यकता होती है?

जहां तक ​​मुझे पता है, यूडब्लूपी में कोई एपीआई नहीं है यह जांचने के लिए कि डिवाइस पर ब्लूटूथ सक्षम है या नहीं।

विंडोज मोबाइल डिवाइस पर, आप वर्कअराउंड के रूप में निम्न तरीके का उपयोग कर सकते हैं।

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

विंडोज पीसी डिवाइस पर, मेरा सुझाव है कि आप वर्कअराउंड के रूप में ब्लूटूथ सेवा स्तर में सेवाओं की पहुंच की जांच कर रहे हैं।

गैर-बीएलई सेवाओं जैसे आरएफसीओएमएम के लिए, आप एक विशिष्ट सेवा आईडी वाले उपकरणों की गिनती प्राप्त कर सकते हैं। ब्लूटूथ हार्डवेयर स्तर में अक्षम है, तो गिनती 0.

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

BLE सेवाओं के लिए किया जाएगा, तो आप BLE विज्ञापन प्राप्त करने के लिए BluetoothLEAdvertisementWatcher वर्ग का उपयोग कर सकते हैं। यदि ब्लूटूथ हार्डवेयर स्तर में अक्षम है, तो कोई विज्ञापन प्राप्त नहीं होगा।

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 कक्षा का उपयोग करके पूरा किया।

यह जांचने के लिए कि ब्लूटूथ सक्षम है:

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

यह जांचने के लिए ब्लूटूथ (सामान्य रूप में) समर्थित है:

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

ब्लूटूथ स्थापित नहीं है, तो वहाँ कोई Bluetooth रेडियो हो जाएगा रेडियो सूची में, और LINQ क्वेरी वहां शून्य हो जाएगी।

ब्लूटूथ क्लासिक और LE अलग से जांचने के लिए, मैं वर्तमान में ऐसा करने के तरीकों की जांच कर रहा हूं और इस जवाब को अपडेट कर दूंगा जब मुझे पता चलेगा कि एक तरीका मौजूद है और काम करता है।

1

@Zenel जवाब और नए BluetoothAdapter वर्ग (विन 10 रचनाकारों अपडेट से) मिश्रण:

/// <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; 
} 
संबंधित मुद्दे