2017-04-12 122 views
0

:Windowsの10、そのペアBLEデバイスで自分のPCをWindowsベースのユニバーサルアプリを構築しようとしているのC#.NET 2015コミュニティ、UWPタスクが終了し、[C#UWP]使用

イム。

すでに動作しているのは、近くのデバイスを列挙し、選択したデバイスとペアにして、バッテリレベルやファームウェアリビジョンなどの情報を取得することです。今

問題は、私はカスタムサービスを取得しようとすると、私の仕事が原因.GetGattService

System.Exception.Messageで「のSystem.Exception」で終わっていることです。「要素が見つかりません(からの例外HRESULT:0x80070490)」

System.Exception.Stack: "Windows.Devices.Bluetooth.BluetoothLEDevice.GetGattService(GUID serviceUuid)で\ R \ nはSettingsCs.Settings.d__23.MoveNextで()"

この

private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings) 
    { 
     //Check if device is available 
     if (device != null) 
     { 
      Guid SERVICE_CUSTOM = new Guid("7e0bc6be-8271-4f5a-a126-c24220e6250c"); 
      GattDeviceService service = device.GetGattService(SERVICE_CUSTOM); 
      //Check if service is available 
      if (service == null) 
      { 
       return SettingsReturn.INIT_ERROR; 
      } 
      GattCharacteristic characteristic = service.GetCharacteristics(BLETestApp.CHAR_SETTINGS)[0]; 
      //Check if characteristic is available 
      if (characteristic == null) 
      { 
       return SettingsReturn.INIT_ERROR; 
      } 

      var writer = new DataWriter(); 
      writer.WriteBytes(byteSettings); 
      var buffer = writer.DetachBuffer(); 
      await characteristic.WriteValueAsync(buffer);//******** 
      bool success = characteristic.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Write); 

      if (success == true) 
      { 
       // Take care of the 8 bit byte for the counter (max = 255 (unsigned)) 
       if (TRANSACTION_ID > 250) 
       { 
        TRANSACTION_ID = 0; 
       } 
       else 
       { 
        // Count TANSACTION_ID one up 
        TRANSACTION_ID++; 
       } 
       return SettingsReturn.OK; 
      } 
      else 
      { 
       return SettingsReturn.WRITE_ERROR; 
      } 
     } 
     else 
     { 
      return SettingsReturn.INIT_ERROR; 
     }    
    } 

私はsomenoneが私を助けたり、私が間違っていることを教えてくれることを願っています。

+0

投稿を編集して例外の詳細(メッセージとスタックトレース)を追加できますか? –

+0

@PedroLamas申し訳ありません、私はそれを忘れました。今詳細を追加しました。 –

+0

必要な機能を設定しましたか? [this](https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth)を確認する必要があります。 –

答えて

0

エラーを特定できません。デバッガを使用して特性に書き込み権限があるかどうかを確認してください。

デバイスに書き込む方法でコードを変更しました。 try catchブロックも追加されました。 はここにある:

private async Task<SettingsReturn> writeSettingTransition(BluetoothLEDevice device, byte[] byteSettings) 
     { 
      bool success = false; 
      //Check if device is available 
      if (device != null) 
      { 
       Guid SERVICE_CUSTOM = new Guid("7e0bc6be-8271-4f5a-a126-c24220e6250c"); 
       GattDeviceService service = device.GetGattService(SERVICE_CUSTOM); 
       //Check if service is available 
       if (service == null) 
       { 
        return SettingsReturn.INIT_ERROR; 
       } 
       GattCharacteristic characteristic = service.GetCharacteristics(BLETestApp.CHAR_SETTINGS)[0]; 
       //Check if characteristic is available 
       if (characteristic == null) 
       { 
        return SettingsReturn.INIT_ERROR; 
       } 

       IBuffer writeBuffer = byteSettings.AsBuffer();// using Windows.Storage.Streams 

       try 
       { 
        // BT_Code: Writes the value from the buffer to the characteristic. 
        var result = await characteristic.WriteValueAsync(writeBuffer); 
        if (result == GattCommunicationStatus.Success) 
        { 
         // NotifyUser("Successfully wrote value to device"); 
         success = true; 
        } 
        else 
        { 
         // NotifyUser($"Write failed: {result}"); 
         success = false; 
        } 
       } 
       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. 
        // NotifyUser(ex.Message, NotifyType.ErrorMessage); 
       } 

       if (success) 
       { 
        // Take care of the 8 bit byte for the counter (max = 255 (unsigned)) 
        if (TRANSACTION_ID > 250) 
        { 
         TRANSACTION_ID = 0; 
        } 
        else 
        { 
         // Count TANSACTION_ID one up 
         TRANSACTION_ID++; 
        } 
        return SettingsReturn.OK; 
       } 
       else 
       { 
        return SettingsReturn.WRITE_ERROR; 
       } 
      } 
      else 
      { 
       return SettingsReturn.INIT_ERROR; 
      } 
     } 

が存在することができ、タイプミスやその他の事故、それが役に立てば幸い。

+0

ご協力いただきありがとうございます。悲しいことに、私はまだ 'GattDeviceService service = device.GetGattService(SERVICE_CUSTOM);で同じ例外を取得します。 –

関連する問題