Windows Phone 8 蓝牙错误 HRESULT:0x8007271D [英] Windows Phone 8 Bluetooth Error HRESULT: 0x8007271D
问题描述
我一直在尝试开发我的 Windows Phone 8 应用程序来访问配对的蓝牙设备(打印机)并发送一些打印数据.
I have been trying to develop my Windows Phone 8 app to access a paired Bluetooth device (a printer) and send over some print data.
我正在 Windows 8 64 位上开发并使用 VS2012 Express.由于模拟器不支持蓝牙,我一直将构建上传到诺基亚 Lumia 820 以进行测试.
I'm developing on Windows 8 64bit and using VS2012 Express. Due to the Emulator not supporting Bluetooth I have been uploading the build to a Nokia Lumia 820 for testing purposes.
我使用了以下两个网站作为参考:
I have used the following two sites for references:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207007(v=vs.105).aspx
http://www.geekchamp.com/articles/getting-started-with-bluetooth-in-windows-phone-8
App 找到配对设备并通过 Debug 命令输出打印机名称.
The App finds the pair device and ouputs the printer name by the Debug command.
代码运行到此为止:
await socket.ConnectAsync(selectedDevice.HostName, "1");
然后它因以下异常而中断:
And then it breaks with the following exception:
********** EXCEPTION OCCURED **********
Data: System.Collections.ListDictionaryInternal
InnerException:
Message: An attempt was made to access a socket in a way forbidden by its access permissions. (Exception from HRESULT: 0x8007271D)
StackTrace: at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at mobility.PrinterSettings.<AppToDevice>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__0(Object state)
********** EXCEPTION OCCURED **********
如果在 socket.ConnectAsync(selectedDevice.HostName, "1") 之前删除await";那么代码会继续没有任何错误,但没有建立蓝牙连接?
If a remove "await" before socket.ConnectAsync(selectedDevice.HostName, "1"); then the code will continue without any errors but no Bluetooth connection is made?
我已经尝试了教程中说明的从 1 到 30 的每个数字,并且我还确保在 WMAppManifest.xml 中启用了 ID_CAP_NETWORKING.
I have tried every number from 1 to 30 as it states in the tutorials and I have also made sure that ID_CAP_NETWORKING is enabled in WMAppManifest.xml.
请问有人知道吗?
完整代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Windows.Networking.Proximity;
using System.Diagnostics;
using Windows.Networking.Sockets;
using Microsoft.Phone.Tasks;
using System.Text;
using Windows.Storage.Streams;
namespace mobility
{
public partial class PrinterSettings : PhoneApplicationPage
{
public PrinterSettings()
{
InitializeComponent();
PrinterName.Text = App.loadString("PrinterName");
if (PrinterName.Text == null || PrinterName.Text == "")
{
PrinterName.Text = "QL420";
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
if (PrinterName.Text != null && PrinterName.Text != "")
{
App.saveString(PrinterName.Text, "PrinterName");
MessageBox.Show("Printer Name has been saved.");
}
else
{
MessageBox.Show("Error: The Printer Name appears to be missing.");
}
}
private async void AppToDevice()
{
try
{
// Configure PeerFinder to search for all paired devices.
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
MessageBox.Show("No paired devices were found.");
}
else
{
// Select a paired device. In this example, just pick the first one.
PeerInformation selectedDevice = pairedDevices[0];
// Attempt a connection
Debug.WriteLine(selectedDevice.DisplayName); // Make sure we are trying to connect to the correct device.
//Debug.WriteLine(selectedDevice.HostName.RawName);
//Debug.WriteLine(selectedDevice.HostName.IPInformation.NetworkAdapter.NetworkAdapterId.ToString());
//Debug.WriteLine(selectedDevice.ServiceName);
StreamSocket socket = new StreamSocket();
// Make sure ID_CAP_NETWORKING is enabled in your WMAppManifest.xml, or the next
// line will throw an Access Denied exception.
// In this example, the second parameter of the call to ConnectAsync() is the RFCOMM port number, and can range
// in value from 1 to 30.
await socket.ConnectAsync(selectedDevice.HostName, "1");
string newLabel = App.loadString("Template");
newLabel = newLabel.Replace("$n", "
");
string epl = App.loadString("PrintHeader");
epl = epl + newLabel;
Debug.WriteLine(epl);
var data = GetBufferFromByteArray(Encoding.UTF8.GetBytes(epl));
//socket.OutputStream.WriteAsync(data);
MessageBox.Show("Device Found.");
}
}
catch (Exception ex)
{
if ((uint)ex.HResult == 0x8007048F)
{
var result = MessageBox.Show("Bluetooth is turned off. To see the current Bluetooth settings tap 'ok'", "Bluetooth Off", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
ShowBluetoothcControlPanel();
}
}
else if ((uint)ex.HResult == 0x80070005)
{
MessageBox.Show("To run this app, you must have ID_CAP_PROXIMITY enabled in WMAppManifest.xaml");
}
else
{
MessageBox.Show(ex.Message);
Debug.WriteLine(ex.StackTrace);
Debug.WriteLine(ex.HResult);
}
}
}
private IBuffer GetBufferFromByteArray(byte[] package)
{
using (DataWriter dw = new DataWriter())
{
dw.WriteBytes(package);
return dw.DetachBuffer();
}
}
private void ShowBluetoothcControlPanel()
{
ConnectionSettingsTask connectionSettingsTask = new ConnectionSettingsTask();
connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.Bluetooth;
connectionSettingsTask.Show();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
AppToDevice();
});
}
}
}
推荐答案
经过多次尝试并将手机重置为原始状态但没有成功.
After much playing around and resetting the phone back to its original state with no success.
我在 WMAppManifest.xml 中勾选了ID_CAP_PROXIMITY",它立即开始工作!
I ticked "ID_CAP_PROXIMITY" in WMAppManifest.xml and it started working straight away!
看起来我为ID_CAP_PROXIMITY"设置的错误代码可能是错误的,所以这里是代码更新以及此后我遇到的更多错误消息.
It looks like the Error Code I had for "ID_CAP_PROXIMITY" was maybe wrong so here is an update in code plus a few more error messages I have come across since.
我希望这可以帮助遇到类似问题的人.
I hope this might help somebody that is having a similar issue.
catch (Exception ex)
{
if ((uint)ex.HResult == 0x8007048F)
{
var result = MessageBox.Show("Bluetooth is turned off.
To see the current Bluetooth settings tap 'ok'", "Bluetooth Off", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
ShowBluetoothcControlPanel();
}
}
else if ((uint)ex.HResult == 0x8007271D)
{
//0x80070005 - previous error code that may be wrong?
MessageBox.Show("To run this app, you must have ID_CAP_PROXIMITY enabled in WMAppManifest.xaml");
}
else if ((uint)ex.HResult == 0x80072740)
{
MessageBox.Show("The Bluetooth port is already in use.");
}
else if ((uint)ex.HResult == 0x8007274C)
{
MessageBox.Show("Could not connect to the selected Bluetooth Device.
Please make sure it is switched on.");
}
else
{
//App.handleException(ex);
MessageBox.Show(ex.Message);
}
}
这篇关于Windows Phone 8 蓝牙错误 HRESULT:0x8007271D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!