Windows Phone Silverlight 8.1使用蓝牙与Arduino通信

时间:2014-11-24 13:01:53

标签: c# silverlight bluetooth windows-phone-8.1 windows-phone

我使用蓝牙连接了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时,我收到Debugger.Break并继续,我得到异常"调用目标引发异常&#34 ;

我用过代码:

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

请帮助我。

1 个答案:

答案 0 :(得分: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;