获取GattService [C#UWP]时任务结束

时间:2017-04-12 07:37:42

标签: c# win-universal-app bluetooth-lowenergy

使用:Windows 10,C#.NET 2015社区,UWP

我正在尝试构建一个将我的PC与BLE设备配对的windows-universal-app。

已经工作的是枚举附近的设备,与选定的设备配对并获取电池级和固件版本等信息。

现在的问题是,当我尝试获取自定义服务时,我的任务因为" System.Exception"而结束。 at .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;
        }            
    }

我希望有人能帮助我或告诉我我做错了什么。

1 个答案:

答案 0 :(得分: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;
            }
        }

可能存在拼写错误和其他不幸事件,希望它有所帮助。