我正在使用WPF应用程序中的以下代码进行配对测试,但它总是以失败状态失败。
要使用BluetoothLe库,我刚刚添加了参考(C:\ Program Files(x86)\ Windows Kits \ 10 \ UnionMetadata \ Windows.winmd)
<!DOCTYPE html>
<html ng-app="app">
<head>
<script data-require="jquery@3.1.1" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link data-require="bootstrap@3.3.0" data-semver="3.3.0" rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script data-require="bootstrap@3.3.0" data-semver="3.3.0" src="https://netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script data-require="angular.js@1.6.0" data-semver="1.6.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.js"></script>
<script data-require="ng-messages@1.3.16" data-semver="1.3.16" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-messages.min.js"></script>
<script src="script.js"></script>
<script src="directive.js"></script>
</head>
<body class="container" ng-controller="AppController as vm">
<h1>Hello Plunker!</h1>
<form class="form-horizontal" name="wheelForm" novalidate="">
<div class="row">
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-8" ng-class="{ 'has-error': wheelForm.username.$touched && wheelForm.username.$invalid }">
<input type="text" name="username" class="form-control" ng-model="vm.formdata.username" required="" />
<div ng-if="wheelForm.username.$touched && wheelForm.username.$invalid" ng-messages="wheelForm.username.$error" class="has-error">
<span ng-message="required">Please enter name.</span>
</div>
</div>
</div>
<div class="form-group ">
<!--<label class="col-md-4"># of Bikes</label>{{wheelForm.vehicle_bike | json }}-->
<div class="col-md-8" ng-class="{ 'has-error': wheelForm.vehicle_bike.$touched && wheelForm.vehicle_bike.$invalid }">
<input type="text" placeholder="Enter 0 for no vehicles" name="vehicle_bike" class="form-control" ng-model="vm.formdata.vehicles.bike" validate-number="{min:1, max:16}" ng-required="true" />
<div ng-messages="wheelForm.vehicle_bike.$error" ng-if="wheelForm.vehicle_bike.$touched && wheelForm.vehicle_bike.$invalid" class="text-danger">
<!-- <span ng-message="parse">Enter 0 if no bike servlet required</span> -->
<span ng-message="validRange">Number must be between 0 and 16.</span>
<span ng-message="validNumber">Please enter valid Number.</span>
</div>
</div>
</div>
<div class="form-group ">
<!--<label class="col-md-4"># of Cars</label>{{wheelForm.vehicle_car | json }}-->
<div class="col-md-8" ng-class="{ 'has-error': wheelForm.vehicle_car.$touched && wheelForm.vehicle_car.$invalid }">
<input type="text" placeholder="Enter 0 for no vehicles" name="vehicle_car" class="form-control" ng-model="vm.formdata.vehicles.car" ng-required="true" validate-number="{min:1, max:16}" required />
<div ng-messages="wheelForm.vehicle_car.$error" ng-if="wheelForm.vehicle_car.$touched && wheelForm.vehicle_car.$invalid" class="text-danger">
<span ng-message="validRange">Number must be between 0 and 16.</span>
<span ng-message="validNumber">Please enter valid Number.</span>
</div>
</div>
</div>
<div class="form-group ">
<!--<label class="col-md-4"># of Cycles</label>{{wheelForm.vehicle_cycle | json }}-->
<div class="col-md-8" ng-class="{ 'has-error': wheelForm.vehicle_cycle.$touched && wheelForm.vehicle_cycle.$invalid }">
<input type="text" placeholder="Enter 0 for no vehicles" min="1" name="vehicle_cycle" class="form-control" ng-model="vm.formdata.vehicles.cycle" ng-required="true" validate-number="{min:1, max:16}" required />
<div ng-messages="wheelForm.vehicle_cycle.$error" ng-if="wheelForm.vehicle_cycle.$touched && wheelForm.vehicle_cycle.$invalid" class="text-danger">
<span ng-message="validRange">Number must be between 0 and 16.</span>
<span ng-message="validNumber">Please enter valid Number.</span>
</div>
</div>
</div>
<div class="form-actions m-t-lg text-center">
<button ng-disabled="wheelForm.$invalid" class="btn btn-block btn-success" ng-click="vm.updateForm()">
Update
</button>
</div>
</div>
</form>
</body>
</html>
奇怪的是
使用相同的代码与UWP App进行配对。
在UWP和WPF应用程序中取消配对都没问题。
不同之处在于UWP应用程序总是弹出系统对话框以确认配对和取消,但WPF应用程序不会显示任何对话框。
有人能帮助我吗?
解决!谢谢。 我刚刚使用了自定义配对。
if (!DeviceInformation.Pairing.IsPaired)
{
Logger.Info($"{DeviceInformation.Name} Try Pairing");
var result = await DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);
Logger.Info($"{result.Status}");
}
答案 0 :(得分:3)
此时“经典”桌面Windows应用程序不支持配对功能。您可以尝试使用Desktop Bridge转换您的应用,或者您可以尝试通过DeviceInformationCustomPairing
自行配对,但它需要您拥有用户界面。
(资料来源:this MSDN discussion)
答案 1 :(得分:2)
我遇到类似问题的类似问题 - 尽管它并不完全是你所要求的,但我认为对于遇到这个问题的其他人可能会有用:
我的问题是 args.Accept()似乎对配对过程没有任何影响,有时配对会失败,有时会超时。
即使我不确定原因,原因是我从 App.Current.Dispatcher.InvokeAsync()中调用了 Accept(),而不是直接调用它。在 Task.Run()中调用也可以正常工作。
答案 2 :(得分:2)
正如MS人员所说here,非UWP程序(如桌面和控制台应用程序)不正式支持应用程序内配对。但是,正如Peter Torr所示,您可以尝试通过DeviceInformationCustomPairing&#34;自己进行配对。
这段代码对我有用;但是,仅适用于DevicePairingKinds.ConfirmOnly
和DevicePairingKinds.ProvidePin
(其他选项会导致RequiredHandlerNotRegistered
错误,但没有其他处理程序可以注册。):
DeviceInformationCustomPairing p = DeviceInformation.Pairing.Custom;
p.PairingRequested += PairingRequestedHandler;
var pairingResult = await p.PairAsync(DevicePairingKinds.ConfirmOnly);
//or:
//var pairingResult = await p.PairAsync(DevicePairingKinds.ProvidePin);
可以从official samples获取处理程序,或者使用这个非常简化的版本:
private static void PairingRequestedHandler(DeviceInformationCustomPairing sender, DevicePairingRequestedEventArgs args)
{
switch (args.PairingKind)
{
case DevicePairingKinds.ConfirmOnly:
// Windows itself will pop the confirmation dialog as part of "consent" if this is running on Desktop or Mobile
// If this is an App for 'Windows IoT Core' or a Desktop and Console application
// where there is no Windows Consent UX, you may want to provide your own confirmation.
args.Accept();
break;
case DevicePairingKinds.ProvidePin:
// A PIN may be shown on the target device and the user needs to enter the matching PIN on
// this Windows device. Get a deferral so we can perform the async request to the user.
var collectPinDeferral = args.GetDeferral();
string pinFromUser = "952693";
if (!string.IsNullOrEmpty(pinFromUser))
{
args.Accept(pinFromUser);
}
collectPinDeferral.Complete();
break;
}
}
常量变量pinFromUser
只是一个例子。显然必须要求用户提供!