C# 找不到蓝牙设备的ID
我正在尝试使用visual studio 2013、windows phone应用程序通过蓝牙连接到Arduino 我可以找到没有任何问题的设备。当我使用以下代码时,会出现一个错误,显示“Element not found”:C# 找不到蓝牙设备的ID,c#,visual-studio-2013,bluetooth,arduino,windows-phone-8.1,C#,Visual Studio 2013,Bluetooth,Arduino,Windows Phone 8.1,我正在尝试使用visual studio 2013、windows phone应用程序通过蓝牙连接到Arduino 我可以找到没有任何问题的设备。当我使用以下代码时,会出现一个错误,显示“Element not found”: await socket.ConnectAsync(MakeBlock.HostName, "5", SocketProtectionLevel.BluetoothEncryptionAllowNullAuthenticat
await socket.ConnectAsync(MakeBlock.HostName, "5",
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
我试图修改代码以使用RfcommDeviceService获取服务名称,但PeerFinder对象的Id为“”,无法设置connectService
connectService = RfcommDeviceService.FromIdAsync(MakeBlock.Id);
这是我尝试连接的完整代码:
#region App to Device....
PeerFinder.AlternateIdentities["Bluetooth:SDP"] = "{00001101-0000-1000-8000-00805F9B34FB}";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
tbLogger.Text = "Seaching for Connections...";
if (pairedDevices.Count == 0)
{
tbLogger.Text = "Makeblock is not found...";
}
else
{
tbLogger.Text = pairedDevices.Count.ToString() + " connections found!";
for (int i = 0; i < pairedDevices.Count; i++)
{
PeerInformation selectedPeer = pairedDevices[i];
tbLogger.Text = tbLogger.Text + "\r\n" + selectedPeer.DisplayName;
if (selectedPeer.DisplayName == "Makeblock")
{
MakeBlock = pairedDevices[i];
}
}
tbLogger.Text = tbLogger.Text + "\r\n" + "---------------------------";
try
{
StreamSocket socket = new StreamSocket();
IAsyncOperation<RfcommDeviceService> connectService;
connectService = RfcommDeviceService.FromIdAsync(MakeBlock.Id);
RfcommDeviceService rfcommService = await connectService;
await socket.ConnectAsync(rfcommService.ConnectionHostName, rfcommService.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
tbLogger.Text = tbLogger.Text + "\r\n" + "Connection to MakeBlock has been made...";
}
catch (Exception ex)
{
tbLogger.Text = tbLogger.Text + "\r\n" + "Could not connect to " + MakeBlock.DisplayName;
tbLogger.Text = tbLogger.Text + "\r\n" + ex.Message;
}
}
#endregion
#地区应用程序到设备。。。。
PeerFinder.alternativeEntities[“Bluetooth:SDP”]=“{00001101-0000-1000-8000-00805F9B34FB}”;
var pairedDevices=await PeerFinder.FindAllPeersAsync();
tbLogger.Text=“搜索连接…”;
如果(pairedDevices.Count==0)
{
tbLogger.Text=“找不到Makeblock…”;
}
其他的
{
tbLogger.Text=pairedDevices.Count.ToString()+“找到连接!”;
for(int i=0;i
代码失败,因为MakeBlock.Id=“”
有什么建议吗?好的。。。我想出来了 我只是直接将GUID添加到serviceName中
StreamSocket socket = new StreamSocket();
await socket.ConnectAsync(MakeBlock.HostName, "{00001101-0000-1000-8000-00805F9B34FB}",
SocketProtectionLevel.PlainSocket);
tbLogger.Text = tbLogger.Text + "\r\n" + "Connection to MakeBlock has been made...";
这个有用!!不允许发送消息来控制机器人。这个答案有效。。。我忘了换另一部手机时必须配对。