【问题标题】:How can I create a UWP Class Library with Bluetooth permissions?如何创建具有蓝牙权限的 UWP 类库?
【发布时间】:2018-04-28 16:43:53
【问题描述】:

我昨天问了一个类似的问题,但我认为这是错误的问题。我已经有使用 Xamarin 在 iOS 和 Android 上实现蓝牙 LE 支持的 PCL,但现在我必须实现 Windows BT 支持。这样做的唯一方法似乎是通过 UWP,但是在创建了一个 UWP 类库来执行此操作并通过我的 PCL 引用它之后,DeviceWatcher 从 Created 跳到 EnumerationComplete。

我的猜测是,这是因为没有授予程序蓝牙权限——因为必须在清单中指定,而在类库中没有使用。你们碰巧知道这是不是真的吗?如何授予对我的 PCL 和/或 UWP 类库的权限?以下是我在 UWP 中编写的一些代码(粘贴自我之前的问题。)提前感谢您提供的任何帮助。

Adapter.cs

private DeviceWatcher deviceWatcher;

public override IList<IDevice> ConnectedDevices => ConnectedDeviceRegistry.Values.ToList();

    /// <summary>
    /// Used to store all connected devices
    /// </summary>
    public Dictionary<string, IDevice> ConnectedDeviceRegistry { get; }


    /// <summary>
    /// Registry used to store device instances for pending operations : connect 
    /// </summary>
    public Dictionary<string, IDevice> DeviceOperationRegistry { get; }

    public Adapter(DeviceWatcher deviceWatcher)
    {
        Platform = PLATFORM.WINDOWS;
        DeviceOperationRegistry = new Dictionary<string, IDevice>();
        ConnectedDeviceRegistry = new Dictionary<string, IDevice>();

        this.deviceWatcher = deviceWatcher;
        /*DeviceInformation.CreateWatcher(
            aqsAllBluetoothLEDevices,
            requestedProperties,
            DeviceInformationKind.AssociationEndpoint);*/

        deviceWatcher.Added += DeviceWatcher_Added;
        deviceWatcher.Updated += DeviceWatcher_Updated;
        deviceWatcher.Removed += DeviceWatcher_Removed;
        deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompleted;
        deviceWatcher.Stopped += DeviceWatcher_Stopped;
    }

    protected override Task StartScanningForDevicesNativeAsync(Guid[] serviceUuids, bool allowDuplicatesKey, CancellationToken scanCancellationToken)
    {

        // clear out the list
        DiscoveredDevices.Clear();

        deviceWatcher.Start();

        return Task.FromResult(true);
    }

BleImplementation.cs

string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected", "System.Devices.Aep.Bluetooth.Le.IsConnectable" };

    // BT_Code: Example showing paired and non-paired in a single query.
    string aqsAllBluetoothLEDevices = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

    DeviceWatcher deviceWatcher;

    protected override IAdapter CreateNativeAdapter()
    {
        deviceWatcher = DeviceInformation.CreateWatcher(
                aqsAllBluetoothLEDevices,
                requestedProperties,
                DeviceInformationKind.AssociationEndpoint);
        return new Adapter(deviceWatcher);
    }

Package.appxmanifest

    <?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
  <Prerequisites>
    <OSMinVersion></OSMinVersion>
    <OSMaxVersionTested></OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="" />
  </Resources>
  <Applications>
    <Application Id="" StartPage="">
      <VisualElements DisplayName="" Description=""
           Logo="" SmallLogo=""
           ForegroundText="" BackgroundColor="">
        <SplashScreen Image="" />
      </VisualElements>
    </Application>
  </Applications>

  <Identity Name="MyCompany.MySuite.MyApp"
            Version="1.0.0.0"
            Publisher="CN=MyCompany, O=MyCompany, L=MyCity, S=MyState, C=MyCountry"/>

  <Properties>
    <DisplayName>MyApp</DisplayName>
    <PublisherDisplayName>MyCompany</PublisherDisplayName>
    <Logo>images\icon.png</Logo>
  </Properties>

  <Capabilities>
    <Capability Name="internetClient" />
    <!--BT_Code: Always declare the bluetooth capability when using Bluetooth-->
    <DeviceCapability Name="bluetooth" />
  </Capabilities>
</Package>

【问题讨论】:

    标签: c# uwp bluetooth-lowenergy printer-control-language


    【解决方案1】:

    如果类库在 UWP 应用包中,则继承 UWP 应用的权限。

    【讨论】:

    • UWP 类库在 PCL 库内部使用,这是混淆的一部分。如果我为使用 PCL 库的 UWP 应用设置 BT 权限,PCL 库内的 UWP 类库的函数调用是否仍能检测到/可用?
    【解决方案2】:

    在使用 PCL 时请注意:

    便携式类库 (PCL) 现已正式弃用 [2017 年 8 月 16 日]
    如果您现在在不同的 .NET 实现之间共享代码,您可能知道可移植类库 (PCL)。随着 .NET Standard 2.0 的发布,我们现在正式弃用 PCL,您应该将项目迁移到 .NET Standard。

    来源:[https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-net-standard-2-0/]

    这可能与您的 deviceWatcher 问题无关。 此外,您不会遇到异常,因此您的代码可以正常工作
    请注意,deviceWatcher 的枚举来自系统级别。与自动添加的 USB 设备不同,必须先在 Windows 设置中添加蓝牙设备。
    要查找设备,请尝试过滤不那么严格或从 DeviceInformation.CreateWatcher 中省略第二个参数。

    您也可以先尝试 FindAllAsync 方法来检查是否可以找到您的设备,但这只是一个快照,因此您无法监控更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2023-02-10
      • 1970-01-01
      • 2013-09-01
      • 2021-11-26
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      相关资源
      最近更新 更多