在C#中获取蓝牙设备数据

6

我正在尝试从一个已经有配对码和通讯协议的医疗BT设备中获取数据。

在寻找代码时,我找到了以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InTheHand.Net.Sockets;
using InTheHand.Net;
using InTheHand.Net.Bluetooth;
using InTheHand.Windows.Forms;
using System.Net.Sockets;
using System.Diagnostics;
using System.Threading;

namespace dConsoleApp
{
    static class Program
    {
        // My BT USB adapter
        private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("00:02:72:CD:9A:33"), BluetoothService.BluetoothBase);
        private static BluetoothClient BC = new BluetoothClient(EP);

        // The BT device that would connect
        private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("94:21:97:60:07:C0"));

        private static NetworkStream stream = null;

        static void Main(string[] args)
        {
            if (BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, MY_PAIRING_CODE))
            {
                Console.WriteLine("PairRequest: OK");

                if (BTDevice.Authenticated)
                {
                    Console.WriteLine("Authenticated: OK");

                    BC.SetPin(MY_PAIRING_CODE);

                    BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
                }
                else
                {
                    Console.WriteLine("Authenticated: No");
                }
            }
            else
            {
                Console.WriteLine("PairRequest: No");
            }

            Console.ReadLine();
        }

    private static void Connect(IAsyncResult result)
    {
        if (result.IsCompleted)
        {
            // client is connected now :)
            Console.WriteLine(BC.Connected);
            stream = BC.GetStream();

            if (stream.CanRead)
            {
                byte[] myReadBuffer = new byte[1024];
                StringBuilder myCompleteMessage = new StringBuilder();
                int numberOfBytesRead = 0;

                // Incoming message may be larger than the buffer size. 
                do
                {
                    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);

                    myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
                }
                while (stream.DataAvailable);

                // Print out the received message to the console.
                Console.WriteLine("You received the following message : " + myCompleteMessage);
            }
            else
            {
                Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
            }

            Console.ReadLine();       
        }
    }
}

这是控制台结果:

Result

然而我期望得到类似于 0XA5 0x05 0x04 0x01 的结果等等。

请问您能帮我得到正确的结果吗?


1
尝试使用UTF8或Unicode而不是Encoding.ASCII。你知道你的源编码是什么吗? - Raja Nadar
你好,我已经尝试了其他编码,但结果只有其他符号。不,我使用的通信协议中没有指定源编码。 - chenny
1个回答

8
您想替换以下内容:
myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

使用

for (int i = 0; i < numberOfBytesRead; i++)
    myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);

谢谢!这就是错误所在。现在我可以从设备中读取字节了。但是我该如何将这些字节转换为更易读的内容? - chenny
@YiyiChen 嗯,这取决于你的医疗设备。它应该有一些关于协议/消息格式的文档。 - Ulugbek Umirov
这是我第一次做这样的事情。我在调试模式下为(int i = 0; i < numberOfBytesRead; i++)设置了断点,我可以看到myReadBuffer变量中的数据。这是通信协议:https://www.dropbox.com/s/kmh627o53rzghwr/Communication%20protocol.pdf,你能告诉我它使用的是什么类型格式吗? - chenny

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接