c# - C# 从蓝牙设备获取数据

标签 c# bluetooth networkstream 32feet

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

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

最佳答案

您要替换以下内容:

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

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

关于c# - C# 从蓝牙设备获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668912/

相关文章:

c# - 全尺寸 Silverlight 应用程序尺寸

c# - IEnumerable<T> 以某种方式转换为 EntitySet<T>

c# - 如何在 Devexpress TextEdit 框中设置最大数值?

android - 低功耗蓝牙外设模式

ios - 使用 Swift iOS 8/9 以编程方式挂断/断开连接/结束通话

c# - BeginRead 的缓冲区何时填充?

c# NetworkStream 的 write() 和 read()

c# - 带参数的 ASP.NET Web Api HttpClient.GetAsync

android - 以编程方式绑定(bind)到 Android 上的 BLE 设备

c# - 如何判断XmlTextReader已经读取了多少数据?