简体   繁体   English

如何创建具有蓝牙权限的UWP类库?

[英]How can I create a UWP Class Library with Bluetooth permissions?

I asked a similar question yesterday, but I think it was the wrong one. 昨天我问了类似的问题,但我认为这是错误的。 I have PCL that uses Xamarin to implement Bluetooth LE support on iOS & Android already, but now I must implement Windows BT support. 我已经拥有使用Xamarin在iOS和Android上实现Bluetooth LE支持的PCL,但是现在我必须实现Windows BT支持。 It appears the only way to do so is through UWP, but after creating a UWP class library to do so, and referencing it through my PCL, the DeviceWatcher skips from Created to EnumerationComplete. 似乎唯一的方法是通过UWP,但是在创建UWP类库并通过我的PCL引用它之后,DeviceWatcher从Created跳到EnumerationComplete。

My guess is this is due to there not being bluetooth permission granted to the program -- as that has to be specified in the manifest, which isn't used in a class library. 我的猜测是这是由于没有向程序授予蓝牙权限-必须在清单中指定该权限,而类清单库中没有使用该权限。 Do you guys happen to know if this is true? 你们碰巧知道这是真的吗? How can I grant permissions to my PCL and/or UWP class library? 如何为我的PCL和/或UWP类库授予权限? Below is some of the code I wrote in the UWP (pasted from my previous question.) Thank you in advance for any help you may offer. 以下是我在UWP中编写的一些代码(从我之前的问题中粘贴而来)。在此先感谢您提供的任何帮助。

Adapter.cs 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 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 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>

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

As you are using PCL be aware of this: 使用PCL时,请注意以下几点:

Portable Class Libraries (PCLs) now officially deprecated [16 August 2017] 便携式类库(PCL)现已正式弃用[2017年8月16日]
If you're sharing code between different .NET implementations today, you're probably aware of Portable Class Libraries (PCLs). 如果您今天在不同的.NET实现之间共享代码,那么您可能已经知道可移植类库(PCL)。 With the release of .NET Standard 2.0, we're now officially deprecating PCLs and you should move your projects to .NET Standard. 随着.NET Standard 2.0的发布,我们现在正式弃用PCL,您应该将项目移至.NET Standard。

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

This will probably not be related to your problem with the deviceWatcher. 这可能与您的deviceWatcher问题无关。 Also you don't get exceptions, so you're your code works as it should 而且您没有异常,因此您的代码可以正常工作
Be aware that the enumeration of deviceWatcher comes from system level. 请注意,deviceWatcher的枚举来自系统级别。 Bluetooth devices have to be added first in Windows Settings , unlike USB-devices that are added automatically. 必须在Windows设置中首先添加蓝牙设备,这与自动添加的USB设备不同。
To find the device, try to filter less strict or omit the second parameter from DeviceInformation.CreateWatcher. 要查找设备,请尝试过滤不太严格的设备,或者从DeviceInformation.CreateWatcher中省略第二个参数。

You can also try the FindAllAsync method first to check if your device can be found, but this is just a snapshot so you cannot monitor changes. 您也可以先尝试使用FindAllAsync方法来检查是否可以找到您的设备,但这只是一个快照,因此您无法监视更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在类库UWP中使用resw资源文件 - how can I use a resw resources file inside a class library UWP 如何为 UWP 应用创建 Headless Unittest 库 - How to create a Headless Unittest library for UWP Apps 如何从 C# Class 库中的颜色在 UWP 中创建 SolidColorBrush? - How do you create a SolidColorBrush in UWP from a color in a C# Class Library? 如何在ViewModel类中使用LocalSettings(UWP)? - How can I use LocalSettings (UWP) in my ViewModel class? 如何以编程方式创建翻译情节提要动画UWP? - How can I create Translate storyboard animation programmatically UWP? 如何在UWP C#中创建Flashlight应用 - How can i create Flashlight app in uwp C# 因为EmguCV对于UWP不可用,我如何从uwp调用c#类/方法 - SInce EmguCV is unavailable for UWP , how can i call c# class/methods from uwp 如何授予用户在MSMQ中创建新队列的权限 - How can I give a user permissions to Create a new Queue in MSMQ 我可以在类库项目中创建数据库连接吗? - Can I create a Database connection in a Class Library Project? 如何在 UWP 中启动应用程序时显示音乐库中的所有文件 - How can I display all files in Music Library on startup of app in UWP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM