我用蓝牙连接了Windows Phone 8和arudino,如这里的例子所示
http://developer.nokia.com/community/wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth#Complete_example
这对于Windows Phone 8来说工作正常,但当我将应用程序重定向到Windows Phone Silverlight 8.1时,我得到了调试器。中断并继续,我得到异常“异常已被调用的目标抛出”。
我使用了代码:
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
Debug.WriteLine("No paired devices were found.");
}
else
{
foreach (var pairedDevice in pairedDevices)
{
if (pairedDevice.DisplayName == DeviceName.Text)
{
connectionManager.Connect(pairedDevice.HostName);
ConnectAppToDeviceButton.Content = "Connected";
DeviceName.IsReadOnly = true;
ConnectAppToDeviceButton.IsEnabled = false;
continue;
}
}
}
其中连接函数定义为:
public async void Connect(HostName deviceHostName)
{
if (socket != null)
{
await socket.ConnectAsync(deviceHostName, "1");
dataReader = new DataReader(socket.InputStream);
dataReadWorker.RunWorkerAsync();
dataWriter = new DataWriter(socket.OutputStream);
}
}
请帮帮我。
您是否尝试过使用Windows. Devices. Bluetooth. Rfcomm命名空间?它几乎是为Windows 8.1蓝牙通信而设计的。
设置设备功能。
<m2:DeviceCapability Name="bluetooth.rfcomm">
<m2:Device Id="any">
<m2:Function Type="serviceId:00001101-0000-1000-8000-00805F9B34FB"/>
</m2:Device>
</m2:DeviceCapability>
选择设备:这是您必须解析您正在使用的设备的Guid的地方。之后,您使用解析后的Guid来查找提供该服务的每个设备。(我使用了SerialPort Guid)
Guid RfcommChatServiceUuid = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");
await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(RfcommChatServiceUuid)));
Connecting: DeviceInformation返回设备信息列表。使用chatserviceInfo。您可以创建一个新的RfcommDeviceService。(在这种情况下,它被称为“服务”)
StreamSocket _socket;
RfcommDeviceService service = await RfcommDeviceService.FromIdAsync(chatserviceInfo1._id);
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
断开连接:
this._socket.Dispose();
this._socket = null;