简体   繁体   English

通用应用程序+蓝牙+ SPP

[英]Universal Application + Bluetooth + SPP

I am currently trying to build a library for using the Serial Port Profile (SPP) on an Universal App. 我目前正在尝试构建一个在通用应用程序上使用串行端口配置文件(SPP)的库。

As far as I have discovered, SPP runs on top of RFCOMM. 据我发现,SPP在RFCOMM之上运行。 The basics for RFCOMM are outlined in the MSDN which is fine. 在MSDN中概述了RFCOMM的基础知识,这很好。 I was also able to "find" my device and "connect" to it. 我还能够“找到”我的设备并“连接”到它。 I was also able to create a StreamReader and StreamWriter for the RFCOMM. 我还能够为RFCOMM创建StreamReader和StreamWriter。

Now the troubles arise. 现在麻烦出现了。 I understand that RFCOMM provides some kind of channels for various features/tasks, one of them being probably SPP (I know the device features SPP and it even works when done via a "normal" serial connection). 我知道RFCOMM为各种功能/任务提供了某种渠道,其中一个可能是SPP(我知道设备具有SPP,甚至在通过“正常”串行连接完成后也可以工作)。

I would like to know if there was an example which bytes I have to send through that channel to get a single byte output on the other side. 我想知道是否有一个示例,我必须通过该通道发送哪些字节才能在另一端获得单字节输出。 Is there some kind of connection setup required (bi-directional exchange)? 是否需要某种连接设置(双向交换)? Are there examples for these data packets, what are their names and is there a specific specification for it. 是否有这些数据包的示例,它们的名称是什么,是否有特定的规范。 I think I would be happy even with some of the correct terms to search for. 我想即使要搜索一些正确的词,我也会很高兴。

Alright, what I assumed was basically wrong. 好吧,我认为基本上是错误的。 Here is the minimalistic code for a very simple (no error handling, no tasks, ...) communication. 这是用于非常简单(无错误处理,无任务等)通信的简约代码。

This goes into the Package.appxmanifest: 这进入Package.appxmanifest中:

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

And this to a method of your choice (make sure your Bluetooth device has been paired, my device has the name "HC-06"). 这是您选择的方法(请确保您的蓝牙设备已配对,我的设备名称为“ HC-06”)。

// Find the device
var bluetoothDevicesSpp = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var bluetoothDeviceHc06 = bluetoothDevicesSpp.SingleOrDefault(d => d.Name == "HC-06");
var serviceRfcomm = await RfcommDeviceService.FromIdAsync(bluetoothDeviceHc06.Id);
StreamSocket socket = new StreamSocket();
await socket.ConnectAsync(serviceRfcomm.ConnectionHostName, serviceRfcomm.ConnectionServiceName, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
DataWriter writer = new DataWriter(socket.OutputStream);
DataReader reader = new DataReader(socket.InputStream);

To read, use this: 要阅读,请使用以下命令:

await reader.LoadAsync(1);
byte b = reader.ReadByte();
Debug.WriteLine((char)b);

To write, use this: 要编写,请使用以下命令:

writer.WriteString("MaM\r\n");
writer.StoreAsync();

The bytes will be transferred as they are, no additional protocol or similar is necessary. 字节将按原样传输,无需其他协议或类似协议。 Enjoy. 请享用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM