【发布时间】:2015-08-08 14:10:57
【问题描述】:
我正在尝试使用 Visual Studio 2013、Windows Phone 应用程序通过蓝牙连接到 Arduino...
当我使用以下代码时,我可以找到没有任何问题的设备,但出现“找不到元素”的错误:
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
代码失败,因为 MakeBlock.Id = ""
有什么建议吗?
【问题讨论】:
标签: c# visual-studio-2013 bluetooth arduino windows-phone-8.1