2017-03-05 56 views
3

我有一個BLE設備(健身追蹤器),這是e.x.當我向其發送特定的寫入請求時顯示一條消息。通過GATT(UWP)發送給BLE設備的寫請求

在Android中它一切正常。該裝置就會立即顯示,它收到的消息:

BluetoothGattCharacteristic characteristic = ... //connecting to BLE device with specific UUID 
byte[] data = new byte[] {(byte)0x01, 0x01}; 
characteristic.setValue(data); 
mBluetoothGatt.writeCharacteristic(characteristic); 

但在我UWP應用中,藍牙設備沒有表現出任何反應:

var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("00001811-0000-1000-8000-00805f9b34fb")), null); 
GattDeviceService service = await GattDeviceService.FromIdAsync(devices[0].Id); 
var gattCharacteristic = service.GetCharacteristics(new Guid("00002a46-0000-1000-8000-00805f9b34fb")).First(); 

//Writing request 
var writer = new DataWriter(); 
writer.WriteBytes(new byte[] { 0x01, 0x01 }); 
await gattCharacteristic.WriteValueAsync(writer.DetachBuffer()); 

有沒有人有一個想法?

+0

什麼是WriteValueAsync結果值? – Emil

+0

結果值是GattCommunicationStatus.Success – Cristian126

+1

我遇到了同樣的問題。此外,我的應用程序在筆記本上運行良好,但在Windows手機上無效。這似乎是Windows或手機中的問題。您是否嘗試在不同設備上運行此代碼? – Knyaz

回答

2

可能是因爲您沒有連接,或者您使用的是錯誤的gattCharacteristic服務。

反正我就是這樣成功寫信給我的設備:

private async Task SendValues(byte[] toSend) 
{  
    IBuffer writer = toSend.AsBuffer(); 
    try 
    { 
    // BT_Code: Writes the value from the buffer to the characteristic.   
    var result = await gattCharacteristic.WriteValueAsync(writer); 
    if (result == GattCommunicationStatus.Success) 
    { 
     //Use for debug or notyfy 
     var dialog = new Windows.UI.Popups.MessageDialog("Succes"); 
     await dialog.ShowAsync(); 
    } 
    else 
    { 
     var dialog = new Windows.UI.Popups.MessageDialog("Failed"); 
     await dialog.ShowAsync(); 
    } 
    } 
    catch (Exception ex) when ((uint)ex.HResult == 0x80650003 || (uint)ex.HResult == 0x80070005) 
    { 
    // E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED or E_ACCESSDENIED 
    // This usually happens when a device reports that it 
    //support writing, but it actually doesn't. 
    var dialog = new Windows.UI.Popups.MessageDialog(ex.Message); 
    await dialog.ShowAsync(); 
    } 
} 
+0

我使用了和你一樣的代碼,但結果是一樣的。設備已連接(我使用'service.Device.ConnectionStatus'進行了檢查),並使用了我的android應用程序中的UUID。我無法猜測爲什麼它不起作用。 – Cristian126