[英]Windows Phone Silverlight 8.1 Bluetooth app to device element not found exception
[英]Windows Phone Silverlight 8.1 communicating with Arduino using Bluetooth
我使用此處示例中提供的藍牙將Windows Phone 8與arudino連接起來
這對於Windows Phone 8來說還可以,但是當我將應用程序重新定位到Windows Phone Silverlight 8.1時,我得到了Debugger.Break,然后繼續,我得到了異常“調用目標拋出了異常”。
我使用了代碼:
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;
}
}
}
其中connect函數定義為:
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)));
連接:DeviceInformation返回DeviceInformation的列表。 使用chatserviceInfo.Id可以創建一個新的RfcommDeviceService。 (在這種情況下,它稱為“服務”)
StreamSocket _socket;
RfcommDeviceService service = await RfcommDeviceService.FromIdAsync(chatserviceInfo1._id);
await _socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
斷開:
this._socket.Dispose();
this._socket = null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.