C# 用C语言从蓝牙设备获取数据#

C# 用C语言从蓝牙设备获取数据#,c#,bluetooth,networkstream,32feet,C#,Bluetooth,Networkstream,32feet,我正在尝试从一个医疗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; usin

我正在尝试从一个医疗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();       
        }
    }
}
}

这是控制台结果:

而我期望类似于
0XA5
0x05
0x04
0x01
等的东西


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

您想替换以下内容:

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

for(int i=0;i
请尝试使用UTF8或Unicode,而不是Encoding.ASCII。。你们知道你们的源代码编码是什么吗?您好,我已经尝试过其他编码,但结果只有其他符号。不,在我的通信协议中没有指定源代码。谢谢!这是个错误。现在我可以从设备读取字节了。但是我现在怎么才能把字节转换成更可读的格式呢?@YiyiChen嗯,这取决于你的医疗设备。它应该有一些关于协议/消息格式的文档。这是我第一次这样做。我看到如果在调试模式下为(inti=0;Ifor (int i = 0; i < numberOfBytesRead; i++) myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);