我已经编写了一个UWP类库,它似乎无法扫描BLE设备。查看DeviceWatcher,它会从Created状态跳转到EnumerationComplete。我尝试过为我的图书馆创建一个清单,但问题仍然存在。下面是一些代码和我的清单。
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>