2014-11-24 72 views
0

我連接了Windows Phone 8與arudino使用藍牙作爲樣本這裏的Windows Phone的Silverlight 8.1與Arduino的通信使用藍牙

http://developer.nokia.com/community/wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth#Complete_example

這是工作好了的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; 
       } 
      } 
     } 

當連接函數定義爲:

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); 
     } 
    } 

請幫助我。

回答

1

您是否嘗試過使用Windows.Devices.Bluetooth.Rfcomm命名空間?它非常適合Windows 8.1藍牙通信。

Setting device capabilities.

<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; 
+0

我設置設備capabilities.I有仍然很少的問題嗎?我們可以隨機提供服務ID嗎?如果不知道如何知道服務ID?什麼是_id和ConnectionServiceName? – 2014-11-24 16:04:52

+0

似乎我犯了一個小錯誤,讓我們改變它吧! – ggg 2014-11-24 16:36:31

+0

什麼是chatserviceInfo1._id以及如何獲取它?我們也應該聲明服務爲var? – 2014-11-25 08:36:26

相關問題