I am trying to connect to a printer(by MAC address) and print. I am encountering several issues that I tried to overcome for hours with no success. I tried several different Methods:
Method 1: Using PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{00001101-0000-1000-8000-00805F9B34FB}" works on first attempt to connect. When I try to reopen the connection after closing connection, it was not able to find any paired devices. When I exit application and rerun, I get the following exception: "Element not found. (Exception from HRRESULT:0x80070490) at 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);
}
}
Method 2: Using PeerFinder.Alternative["Bluetooth:Paired" = "", I am able to connect once. After closing connection and reopening connection, I get the following Exception "Element not found. (Exception from HRRESULT:0x80070490) at socket.ConnectAsync().
//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);
}
}
Anyone experiencing the same issue? What am I doing wrong?
Thanks in advance
User contributions licensed under CC BY-SA 3.0