1

我正在 Windows 8.1 和 Windows Phone 8.1 平台上实现一个通用应用程序,它应该通过蓝牙技术与体重或血压监测器等设备连接。不幸的是,我在发现过程中遇到了困难,因此我没有机会配对和连接到设备。

我找到了关于这个问题的线程,但不幸的是它与 Android 相关联。

从上面的帖子:

描述医疗设备:该设备正在使用服务发现协议 (SDP) 和串行端口配置文件 (SPP)。它启动查询程序以发现(最多 10 个)具有匹配 COD 过滤器和服务名称的周围接入点。然后它通过检查 PIN 顺序地与接入点建立连接(使用 Page Procedure)。匹配 PIN 后,将上传数据。上传数据后,设备等待确认。设备是主设备并启动通信。

我也从Microsoft Bluetooth Rfcomm Sample开始。

我的问题:

  1. 我对我的标准UUID编号 00001101-0000-1000-8000-00805F9B34FB 持怀疑态度。在这种情况下合适吗?

    // The Id of the Service Name SDP attribute
    protected const UInt16 SdpServiceNameAttributeId = 0x100;
    
    // The SDP Type of the Service Name SDP attribute.
    // The first byte in the SDP Attribute encodes the SDP Attribute Type as follows :
    //    -  the Attribute Type size in the least significant 3 bits,
    //    -  the SDP Attribute Type value in the most significant 5 bits.
    protected const byte SdpServiceNameAttributeType = (4 << 3) | 5;
    
    public async Task Start()
    {
        try
        {
            if (rfcommProvider == null)
                rfcommProvider = await RfcommServiceProvider.CreateAsync(
                    RfcommServiceId.FromUuid(BluetoothServiceUuid));
    
            if (rfcommProvider != null)
            {
                if (socketListener != null)
                {
                    socketListener.Dispose();
                    socketListener = null;
                }
    
                // Create a listener for this service and start listening
                socketListener = new StreamSocketListener();
                socketListener.ConnectionReceived += OnConnectionReceived;
    
                await socketListener.BindServiceNameAsync(rfcommProvider.ServiceId.AsString(),
                    SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
    
                // Set the SDP attributes and start Bluetooth advertising
                DataWriter sdpWriter = new DataWriter();
    
                // Write the Service Name Attribute.
                sdpWriter.WriteByte(SdpServiceNameAttributeType);
    
                // The length of the UTF-8 encoded Service Name SDP Attribute.
                sdpWriter.WriteByte((byte)BluetoothServiceDisplayName.Length);
    
                // The UTF-8 encoded Service Name value.
                sdpWriter.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                sdpWriter.WriteString(BluetoothServiceDisplayName);
    
                // Set the SDP Attribute on the RFCOMM Service Provider.
                if (rfcommProvider.SdpRawAttributes.ContainsKey(SdpServiceNameAttributeId))
                    rfcommProvider.SdpRawAttributes.Remove(SdpServiceNameAttributeId);
                rfcommProvider.SdpRawAttributes.Add(SdpServiceNameAttributeId, sdpWriter.DetachBuffer());
                // Start Bluetooth advertising
                //SetState(BluetoothServerState.Started);
                rfcommProvider.StartAdvertising(socketListener);
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine( "ERROR: " + e.Message);
        }
    }
    
  2. 我不确定是否有更好的方法来设置服务名称?也许我做错了?我认为这是一个主要问题。

  3. 那么COD 过滤器呢(在规范中有一个注释,它应该等于 [00000000] 以进行正确的通信?有没有使用 RfcommServiceProvider 设置它的方法?也许我还应该手动设置 PIN 码,或者仅在配对过程中需要它?

  4. 我注意到,在 microsoft 示例中 - 服务器仅支持 Windows 平台。是否也可以为 WP 实现服务器功能?- 将从医疗设备接收数据的设备应该是从设备。

我也读过 Gatt 协议,但它没有为我需要的所有设备提供配置文件。

在此先感谢您的帮助。

4

2 回答 2

1

解决我的问题的关键是在应用程序清单文件中声明串行端口连接,而不是通过指定的 UUID 号声明连接:

<Capabilities>
    <DeviceCapability Name="proximity" />
    <m2:DeviceCapability Name="bluetooth.rfcomm">
      <m2:Device Id="any">
        <m2:Function Type="name:serialPort" />
      </m2:Device>
    </m2:DeviceCapability>
  </Capabilities>

并使用RfcommServiceId.SerialPort作为 RfcommServiceProvider 的参数。

于 2014-08-21T20:37:26.397 回答
0

我在一条非常相似的船上;我正在尝试使用用于 RT 设备的 WinJS/HTML 连接到医疗设备,我发现这篇文章非常好:

http://blogs.windows.com/windows/b/buildingapps/archive/2014/05/07/talking-to-robots-or-other-devices-using-bluetooth-from-a-windows-runtime-app。 aspx

看起来您使用的是 c#,但您可以从文章中的 javascript 中得到这个想法。创建一个查找串行类型设备的选择器,然后查询所有这些设备的列表。这是文章中的一些javascript来说明这个想法

var selector = Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService.getDeviceSelector(Windows.Devices.Bluetooth.Rfcomm.RfcommServiceId.serialPort);

var devices = Windows.Devices.Enumeration.DeviceInformation.findAllAsync(selector, null);

变种设备=设备[0];

返回 Windows.Devices.Bluetooth.Rfcomm.RfcommDeviceService.fromIdAsync(device.id);

我仍在努力解析从设备获得的字节,但是使用上面的代码,WinRT 要求连接到设备,并且我的设备开始流式传输数据,这是最大的障碍。祝你好运!

于 2014-07-29T17:05:02.233 回答