2017-03-05 41 views
3

私はBLEデバイス(フィットネストラッカー)を持っています。これはe.xです。特定の書き込み要求を送信すると、メッセージが表示されます。GETT(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アプリで

は、BLEデバイスがどんな反応を示さない:デバイスは、それがメッセージを受け取ったことを、瞬時に示し

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やPhoneの問題のようです。このコードを別のデバイスで実行しようとしましたか? – 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'でチェックしました)、私はアンドロイドアプリからUUIDを正確に使用しました。なぜそれがうまくいかないのか分かりません。 – Cristian126

関連する問題