的Windows Phone 8的蓝牙错误HRESULT:0x8007271D [英] Windows Phone 8 Bluetooth Error HRESULT: 0x8007271D
问题描述
我一直在努力发展自己的Windows Phone 8应用访问配对的蓝牙设备(打印机)和送过来一些打印数据。
我开发的Windows 8 64位,并使用VS2012前preSS。
由于仿真器不支持蓝牙,我一直在上传生成到诺基亚Lumia 820进行测试。
我已经使用了以下两个网站的引用:
的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
应用程序找到对器件和调试命令。OUPUTS打印机名称。
在code工作,直到点:
等待socket.ConnectAsync(selectedDevice.HostName,1);
然后将它与下面的异常中断:
**********出现异常**********
数据:System.Collections.ListDictionaryInternal
的InnerException:
消息:试图通过其访问权限不允许的方式来访问的插座。 (从HRESULT异常:0x8007271D)
堆栈跟踪:在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在System.Runtime.CompilerServices.TaskAwaiter.GetResult()
在mobility.PrinterSettings< AppToDevice> d__0.MoveNext()
---从previous位置堆栈跟踪,其中引发异常的结尾---
在System.Runtime.CompilerServices.AsyncMethodBuilderCore< ThrowAsync> b__0(对象状态)
**********出现异常**********
如果一个删除等待前socket.ConnectAsync(selectedDevice.HostName,1);那么code将继续没有任何错误,但没有蓝牙连接时?
我已经想尽从1数到30,因为它在教程中指出,我也确信,ID_CAP_NETWORKING在WMAppManifest.xml启用。
请没有任何人有任何想法的?
全部code:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Net;
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Navigation;
使用Microsoft.Phone.Controls;
使用Microsoft.Phone.Shell;
使用Windows.Networking.Proximity;
使用System.Diagnostics程序;
使用Windows.Networking.Sockets;
使用Microsoft.Phone.Tasks;
使用System.Text;
使用Windows.Storage.Streams;命名空间的流动性
{
公共部分类PrinterSettings:的PhoneApplicationPage
{
公共PrinterSettings()
{
的InitializeComponent();
PrinterName.Text = App.loadString(PrinterName的);
如果(PrinterName.Text == NULL || PrinterName.Text ==)
{
PrinterName.Text =QL420;
}
} 私人无效Save_Click(对象发件人,RoutedEventArgs E)
{
如果(PrinterName.Text = NULL&放大器;!&安培;!PrinterName.Text =)
{
App.saveString(PrinterName.TextPrinterName的);
的MessageBox.show(打印机名称已保存。);
}
其他
{
的MessageBox.show(错误:打印机名称似乎缺少。);
}
} 私人异步无效AppToDevice()
{
尝试
{
//配置PeerFinder搜索所有配对设备。
PeerFinder.AlternateIdentities [蓝牙:配对] =;
VAR pairedDevices =等待PeerFinder.FindAllPeersAsync(); 如果(pairedDevices.Count == 0)
{
的MessageBox.show(没有找到配对的设备。);
}
其他
{
//选择配对的设备。在这个例子中,只挑选的第一个。
PeerInformation selectedDevice = pairedDevices [0];
//尝试连接
的Debug.WriteLine(selectedDevice.DisplayName); //确保我们正在尝试连接到正确的设备。
//Debug.WriteLine(selectedDevice.HostName.RawName);
//Debug.WriteLine(selectedDevice.HostName.IPInformation.NetworkAdapter.NetworkAdapterId.ToString());
//Debug.WriteLine(selectedDevice.ServiceName);
StreamSocket插座=新StreamSocket();
//确保ID_CAP_NETWORKING在您的WMAppManifest.xml启用,或者下一个
//线将抛出一个拒绝访问异常。
//在本实施例中,调用ConnectAsync()的第二个参数是RFCOMM端口号,并且范围可以
//价值从1到30。 等待socket.ConnectAsync(selectedDevice.HostName,1); 串newLabel = App.loadString(模板);
newLabel = newLabel.Replace($ N,\\ n);
字符串EPL = App.loadString(PrintHeader);
EPL =英超+ newLabel;
的Debug.WriteLine(EPL);
VAR数据= GetBufferFromByteArray(Encoding.UTF8.GetBytes(EPL)); //socket.OutputStream.WriteAsync(data);
MessageBox.Show(设备中找到的。);
}
}
赶上(异常前)
{
如果((UINT)ex.HResult == 0x8007048F)
{
VAR的结果= MessageBox.Show(蓝牙关闭要查看当前的蓝牙设置点击OK。,蓝牙关,MessageBoxButton.OKCancel);
如果(结果== MessageBoxResult.OK)
{
ShowBluetoothcControlPanel();
}
}
否则,如果((UINT)ex.HResult == 0X80070005)
{
的MessageBox.show(要运行这个程序,你必须WMAppManifest.xaml启用ID_CAP_PROXIMITY);
}
其他
{
MessageBox.Show(ex.Message);
的Debug.WriteLine(ex.StackTrace);
的Debug.WriteLine(ex.HResult);
}
}
} 私人IBuffer GetBufferFromByteArray(字节[]封装)
{
使用(DataWriter DW =新DataWriter())
{
dw.WriteBytes(包);
返回dw.DetachBuffer();
}
}
私人无效ShowBluetoothcControlPanel()
{
ConnectionSettingsTask connectionSettingsTask =新ConnectionSettingsTask();
connectionSettingsTask.ConnectionSettingsType = ConnectionSettingsType.Bluetooth;
connectionSettingsTask.Show();
} 私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
this.Dispatcher.BeginInvoke(()=>
{
AppToDevice();
});
} }
}
多玩弄并没有成功重置手机恢复到原来的状态后。
我WMAppManifest.xml打勾ID_CAP_PROXIMITY,并开始马上工作!
它看起来像错误code我有ID_CAP_PROXIMITY也许是错的所以这里是code的更新加上一些更多的错误消息,我也碰到过,因为
我希望这可以帮助这是有一个类似的问题有人。
赶上(异常前)
{
如果((UINT)ex.HResult == 0x8007048F)
{
VAR的结果= MessageBox.Show(蓝牙关闭\\ n要看到当前的蓝牙设置点击OK。,蓝牙关,MessageBoxButton.OKCancel);
如果(结果== MessageBoxResult.OK)
{
ShowBluetoothcControlPanel();
}
}
否则,如果((UINT)ex.HResult == 0x8007271D)
{
// 0X80070005 - previous错误code,可能是错了吗?
的MessageBox.show(要运行这个程序,你必须WMAppManifest.xaml启用ID_CAP_PROXIMITY);
}
否则,如果((UINT)ex.HResult == 0x80072740)
{
的MessageBox.show(以下简称蓝牙端口已在使用。);
}
否则,如果((UINT)ex.HResult == 0x8007274C)
{
MessageBox.Show(无法连接到所选蓝牙设备\\ n请确保它打开。);
}
其他
{
//App.handleException(ex);
MessageBox.Show(ex.Message);
}
}
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.
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
The App finds the pair device and ouputs the printer name by the Debug command.
The code works up until the point:
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 **********
If a remove "await" before socket.ConnectAsync(selectedDevice.HostName, "1"); then the code will continue without any errors but no Bluetooth connection is made?
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.
Please does anybody have any idea's?
Full code:
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", "\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.
I ticked "ID_CAP_PROXIMITY" in WMAppManifest.xml and it started working straight away!
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.\nTo 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.\nPlease make sure it is switched on.");
}
else
{
//App.handleException(ex);
MessageBox.Show(ex.Message);
}
}
这篇关于的Windows Phone 8的蓝牙错误HRESULT:0x8007271D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!