【问题标题】:Get Data from Bluetooth device in C#在 C# 中从蓝牙设备获取数据
【发布时间】:2014-05-05 07:44:19
【问题描述】:

我正在尝试从我已经有配对代码和通信协议的医疗 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 等等等等等等。

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

【问题讨论】:

  • 而不是 Encoding.ASCII,尝试使用 UTF8 或 Unicode.. 你知道你的源编码是什么吗?
  • 您好,我已经尝试过其他编码,但结果只得到其他符号。不,在我的通信协议中没有指定源编码。

标签: c# bluetooth networkstream 32feet


【解决方案1】:

您要替换以下内容:

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

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

【讨论】:

  • 谢谢!这是错误的。现在我可以从设备中读取字节了。但是我现在怎样才能将字节转换成更易读的东西呢?
  • @YiyiChen 好吧,这取决于您的医疗设备。它应该有一些关于协议/消息格式的文档。
  • 这是我第一次做这样的事情。我看到如果我在调试模式下为 (int i = 0; i dropbox.com/s/kmh627o53rzghwr/Communication%20protocol.pdf,你能告诉我它使用什么类型格式吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多