You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows UWP GATT Server请求对象始终为NULL问题咨询

Hey Christian, let's break down why you're getting a null request object when handling read requests on Windows Creators Update, and fix this issue step by step.

First, the core issue in your code

Your current code doesn't account for cases where GetRequestAsync() might return null—this can happen if the client cancels the request, the request times out, or there's a mismatch in characteristic permissions/properties. Additionally, while your deferral usage is technically correct, using a using statement makes it safer to ensure the deferral is always completed.

Fixed code snippet for handling read requests

Here's the corrected version with proper null checks and safer deferral handling:

async void ReadCharacteristic_ReadRequested(GattLocalCharacteristic sender, GattReadRequestedEventArgs args)
{
    // Use 'using' to automatically complete the deferral when done
    using (var deferral = args.GetDeferral())
    {
        var request = await args.GetRequestAsync();
        
        // Always check if the request is not null before proceeding
        if (request != null)
        {
            var writer = new DataWriter();
            // Populate the writer with your data (example below)
            writer.WriteString("Sample response from GATT Server");
            
            request.RespondWithValue(writer.DetachBuffer());
        }
    }
}

Other potential causes to check

  • Incorrect characteristic permissions/properties: When creating your GattLocalCharacteristic, make sure you've enabled both read properties and permissions. If either is missing, the client's read request might not be properly routed, leading to a null request. Example setup:
    var charParams = new GattLocalCharacteristicParameters
    {
        CharacteristicProperties = GattCharacteristicProperties.Read,
        Permissions = GattCharacteristicPermission.Read,
        UserDescription = "My Readable Characteristic"
    };
    
  • Creators Update API behavior: Windows 10 Creators Update (1703) had some edge cases with GATT request handling where requests could be dropped if the server didn't respond quickly enough. Adding the null check ensures your code doesn't crash when this happens.

Working UWP GATT Server example

Microsoft provides a fully functional Bluetooth GATT Server sample that covers all core scenarios, including handling read/write requests, setting up services, and managing client connections. This sample aligns with the correct API usage for all Windows 10 versions, including Creators Update.

内容的提问来源于stack exchange,提问作者Christian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:46:39