2016-09-20 105 views
2

如何迫使Windows與UWP應用程序中使用的BLE設備斷開連接?我收到來自某些特徵的通知,但在某些時候我想停止接收它們,並確保從BLE設備斷開以保存BLE設備的電池?Windows BLE UWP斷開連接

回答

0

只需處理與設備相關的所有對象。這將斷開設備,除非有其他應用程序連接到它。

0

假設您的應用程序運行作爲GATT客戶端和你有以下情況,您將可以在你的代碼工作:

GattCharacteristic myGattchar; // The gatt characteristic you are reading or writing on your BLE peripheral 
GattDeviceService myGattServ; // The BLE peripheral' gatt service on which you are connecting from your application 
BluetoothLEDevice myBleDev; // The BLE peripheral device your are connecting to from your application 

當你已經連接到您的BLE外設,如果調用Dispose()這樣的方法:

myBleDev.Dispose();和/或myGattServ.Dispose();和/或myGattchar.Service.Dispose()

您肯定會在您的應用程序中釋放資源,但不會乾淨地關閉BLE連接:應用程序無法訪問控制連接的資源。儘管如此,連接仍然建立在堆棧的較低層(在我的外設上,藍牙連接激活 LED在調用任何Dispose()方法後保持ON)。

強制斷開由第一禁用對有關特性(即在上述我的例子myGattchar)通知和指示通過呼叫與參數寫入0(零)到Client Characteristic Configuration descriptor該特性方法WriteClientCharacteristicConfigurationDescriptorAsyncGattClientCharacteristicConfigurationDescriptorValue.None

GattCommunicationStatus status = 
await myGattchar.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.None); 
+0

這是Microsoft的UWP BLE示例顯示您應該執行的操作。它不適合我。 –

0

對於我UWP的應用程序,即使我用的Dispose()方法,我還是收到通知。什麼幫助我將我的設備和特徵設置爲空。例如:

device.Dispose();

device = null;

並不是所有的人都知道這個編程是如何「正確」的,但它到目前爲止一直工作的很好。

相關問題