[英]Reading Data from a BLE device in C#
我必須編寫一個 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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.