我想在我的 Windows MAUI 应用程序中使用蓝牙。
为此,我必须在包清单中指定设备功能: https://learn.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capability-for-bluetooth
我尝试在我的
Package.appxmanifest
文件中添加以下内容:
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest"
IgnorableNamespaces="uap rescap">
...
<Capabilities>
<rescap:Capability Name="runFullTrust" />
<m2:DeviceCapability Name="bluetooth" />
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="serviceId:BA020AC9-7BDB-41CB-9B26-93A285E0066B" />
</m2:Device>
</m2:DeviceCapability>
</Capabilities>
</Package>
启动我的应用程序时,我收到此错误消息(由我自己从德语翻译):
DEP0700: Error while registering the app. [0x80080204]
Error 0xC00CE014: Validation issue in the app manifest:
The app manifest needs to be valid, related to the schema:
Line 40, Colomn 6.
Hint:
The given scheme for MaxVersionTested does recognize XML fields with the http://schemas.microsoft.com/appx/2013/manifest-Namespace. Make sure, that MaxVersionTested is given correctly.
Reason: The content of the element '{http://schemas.microsoft.com/appx/2013/manifest}DeviceCapability' is not guilty to the parent element '{http://schemas.microsoft.com/appx/manifest/foundation/windows10}Capabilities'.
Expected: {http://schemas.microsoft.com/appx/manifest/foundation/windows10}CapabilityChoice, {http://schemas.microsoft.com/appx/manifest/foundation/windows10}CustomCapabilityChoice, {http://schemas.microsoft.com/appx/manifest/foundation/windows10}DeviceCapability.
相同的清单适用于本机 Android 应用程序,但不适用于 MAUI 应用程序,但两者都使用 TargetFramework“net8.0-android34.0”。
您不必使用 m2 命名空间。 Maui windows 基于 WinUI 3 而不是 UWP。您可以使用以下代码:
<Capabilities>
<rescap:Capability Name="runFullTrust" />
<DeviceCapability Name="bluetooth" />
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="serviceId:BA020AC9-7BDB-41CB-9B26-93A285E0066B" />
</Device>
</DeviceCapability>
</Capabilities>
并删除
xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest"
。