2017-04-12 103 views
0

使用:视窗10,C#.NET 2015年社区,UWP任务结束[C#UWP]

我试着去建立一个Windows的普及,应用程序,对我有BLE装置PC。

什么已经在工作是枚举附近的设备,与选定的一个配对,并获得像电池级和固件修订信息。

现在的问题是,当我试图让一个自定义的服务,我的任务结束,因为一个“System.Exception的”在.GetGattService

System.Exception.Message:从“找不到元素(例外。 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);' –