我正在尝试连接打印机(通过MAC地址)并打印。我遇到了几个问题,我试图克服几个小时而没有成功。我尝试了几种不同的方法:
方法1:使用PeerFinder.AlternateIdentities [“Bluetooth:SDP”] =“{00001101-0000-1000-8000-00805F9B34FB}”首次尝试连接。当我尝试在关闭连接后重新打开连接时,它无法找到任何配对设备。当我退出应用程序并重新运行时,我得到以下异常:“找不到元素。(来自HRRESULT的异常:0x80070490)在socket.ConnectAsync()
//Search for devices based on service
PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{00001101-0000-1000-8000-00805F9B34FB}";
//bluetooth device to connect to
var m_bluetoothDevice;
//paired bluetooth device list
var m_pairedDevices = await PeerFinder.FindAllPeersAsync();
if (m_pairedDevices.Count > 0)
{
foreach (PeerInformation device in m_pairedDevices)
{
//Find the device with matching bluetooth mac address (eg. "00:AA:BB:CC:DD:EE")
String macAddr = device.HostName.RawName;
if (macAddr.Contains(m_bluetoothAddress.ToUpper()))
{
m_bluetoothDevice = device;
break;
}
}
}
//Device not paired
if (m_bluetoothDevice == null)
throw new Exception("Device was not paired with system.");
StreamSocket socket = new StreamSocket();
if (socket != null)
{
//Connect to device. Run this task synchronously
IAsyncAction taskConnect = socket.ConnectAsync(m_bluetoothDevice.HostName, m_bluetoothDevice.ServiceName);
taskConnect.AsTask().Wait();
if (taskConnect.ErrorCode != null)
{
throw (taskConnect.ErrorCode);
}
}
方法2:使用PeerFinder.Alternative [“蓝牙:已配对”=“”,我能够连接一次。关闭连接并重新打开连接后,我在socket.ConnectAsync()中收到以下异常“找不到元素。(HRRESULT异常:0x80070490)。
//Search for devices based on service
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
//bluetooth device to connect to
var m_bluetoothDevice;
//paired bluetooth device list
var m_pairedDevices = await PeerFinder.FindAllPeersAsync();
if (m_pairedDevices.Count > 0)
{
foreach (PeerInformation device in m_pairedDevices)
{
//Find the device with matching bluetooth mac address (eg. "00:AA:BB:CC:DD:EE")
String macAddr = device.HostName.RawName;
if (macAddr.Contains(m_bluetoothAddress.ToUpper()))
{
m_bluetoothDevice = device;
break;
}
}
}
//Device not paired
if (m_bluetoothDevice == null)
throw new Exception("Device was not paired with system.");
StreamSocket socket = new StreamSocket();
if (socket != null)
{
//Connect to device. Run this task synchronously
IAsyncAction taskConnect = socket.ConnectAsync(m_bluetoothDevice.HostName, "{00001101-0000-1000-8000-00805F9B34FB}");
taskConnect.AsTask().Wait();
if (taskConnect.ErrorCode != null)
{
throw (taskConnect.ErrorCode);
}
}
遇到同样问题的人?我做错了什么?
提前致谢