c# - 在 C# 中从 BLE 设备读取数据
问题描述
我必须编写一个 Windows 桌面应用程序来读取便携式医疗设备的日志。场景是设备进入维修站进行维护。该技术通过蓝牙将 PC 连接到设备并下载日志。经过一些维护后,设备会被运回给客户,并且可能几个月都不会再看到。
我已经尝试使用串行端口配置文件,但很快了解到 BLE 不支持它。另一家公司正在开发的设备仅限于 BLE,因此我无法使用 SPP。数据将在 10kb 到 100kb 大小范围内。
我已经研究过创建一个自定义服务来获取可用日志条目的数量以及可能设置获取日志的日期范围。这部分看起来很合理。
我不确定的是一旦我知道有多少要检索的日志,如何打开一个流来读取日志。每个日志条目都将作为字符串发送,Windows 代码将解析为单独的值以显示给技术人员。
我对 BLE 有点陌生,所以我不确定要通过哪种方式获取实际的日志条目。提前感谢您的指导。
更新:
做更多的调查,看起来对象传输协议可能是要走的路。快速计算每个日志记录的大小范围为 64 字节,或多或少。
我的理解是,OTP 允许我获取对象计数,在本例中为日志记录,并从设备中逐一请求它们。这种方法看起来合理吗?
解决方案
这是我几年前编写的一些代码,用于通过蓝牙与我的 LG G3 手机通话。我使用了 InTheHand 库。我不记得它是否支持BLE....
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Bluetooth.AttributeIds;
using InTheHand.Net.Sockets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BluetoothPrototype1
{
class Program
{
private const string DEVICE_NAME = "G3";
private static BluetoothDeviceInfo _device = null;
private static BluetoothWin32Events _bluetoothEvents = null;
static void Main(string[] args)
{
try
{
displayBluetoothRadio();
_bluetoothEvents = BluetoothWin32Events.GetInstance();
_bluetoothEvents.InRange += onInRange;
_bluetoothEvents.OutOfRange += onOutOfRange;
using (BluetoothClient client = new BluetoothClient())
{
BluetoothComponent component = new BluetoothComponent(client);
component.DiscoverDevicesProgress += onDiscoverDevicesProgress;
component.DiscoverDevicesComplete += onDiscoverDevicesComplete;
component.DiscoverDevicesAsync(255, true, false, false, false, null);
//BluetoothDeviceInfo[] peers = client.DiscoverDevices();
//device = peers.ToList().Where(p => p.DeviceName == DEVICE_NAME).FirstOrDefault();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.ReadKey();
}
}
static void onOutOfRange(object sender, BluetoothWin32RadioOutOfRangeEventArgs e)
{
Console.WriteLine(string.Format("Device {0} out of range", e.Device.DeviceName));
}
static void onInRange(object sender, BluetoothWin32RadioInRangeEventArgs e)
{
Console.WriteLine(string.Format("Device {0} in range. Connected:{1}", e.Device.DeviceName, e.Device.Connected));
}
static void onDiscoverDevicesProgress(object sender, DiscoverDevicesEventArgs e)
{
Console.WriteLine("Device discovery in progress");
foreach (var device in e.Devices)
{
Console.WriteLine(device.DeviceName);
}
Console.WriteLine();
}
static void onDiscoverDevicesComplete(object sender, DiscoverDevicesEventArgs e)
{
Console.WriteLine("Device discovery complete");
foreach (var device in e.Devices)
{
Console.WriteLine(device.DeviceName);
}
Console.WriteLine();
_device = e.Devices.ToList().Where(p => p.DeviceName == DEVICE_NAME).FirstOrDefault();
if (_device != null)
{
Console.WriteLine("Selected {0} device with address {1}", _device.DeviceName, _device.DeviceAddress);
using (BluetoothClient client = new BluetoothClient())
{
client.Connect(new BluetoothEndPoint(_device.DeviceAddress, BluetoothService.SerialPort));
Stream peerStream = client.GetStream();
for (int i = 0; i < 100; i++)
{
byte[] wb = Encoding.ASCII.GetBytes(string.Format("{0:X2} : This is a test : {1}{2}", i, DateTime.Now.ToString("o"), Environment.NewLine));
peerStream.Write(wb, 0, wb.Length);
}
byte [] buf = new byte[1024];
int readLength = peerStream.Read(buf, 0, buf.Length);
if (readLength > 0)
{
Console.WriteLine("Received {0} bytes", readLength);
}
else
{
Console.WriteLine("Connection is closed");
}
}
}
}
private static void displayBluetoothRadio()
{
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
if (myRadio == null)
{
Console.WriteLine("No radio hardware or unsupported software stack");
return;
}
RadioMode mode = myRadio.Mode;
// Warning: LocalAddress is null if the radio is powered-off.
Console.WriteLine("* Radio, address: {0:C}", myRadio.LocalAddress);
Console.WriteLine("Mode: " + mode.ToString());
Console.WriteLine("Name: " + myRadio.Name);
Console.WriteLine("HCI Version: " + myRadio.HciVersion
+ ", Revision: " + myRadio.HciRevision);
Console.WriteLine("LMP Version: " + myRadio.LmpVersion
+ ", Subversion: " + myRadio.LmpSubversion);
Console.WriteLine("ClassOfDevice: " + myRadio.ClassOfDevice.ToString()
+ ", device: " + myRadio.ClassOfDevice.Device.ToString()
+ " / service: " + myRadio.ClassOfDevice.Service.ToString());
//
//
// Enable discoverable mode
Console.WriteLine();
myRadio.Mode = RadioMode.Discoverable;
Console.WriteLine("Radio Mode now: " + myRadio.Mode.ToString());
Console.WriteLine();
}
}
}
推荐阅读
- safari - Hangouts 如何在 Safari 上获取桌面捕获?
- linux - XV6 os - Docker ubuntu 映像错误:“无可启动设备”/qemu-nox 命令
- kotlin - Kotlin 从通用实现中获取运行时类
- c++ - 解析字符串中的双精度和单词
- python - 使用 Django 发送电子邮件报告
- python - 使用pyspark查找每个对应列的两个数据帧上的值差异
- javascript - 如何找出 node/npm 包导出了哪些函数?
- docker - Docker 容器重启后 WebLogic 自动部署的耳朵消失了
- php - PHP scanf 与 %f 和 %lf 的行为是否不同?
- django - 我该如何划分这些字段并聚合