Как получить данные с устройства Bluetooth в C# - PullRequest
1 голос
/ 14 апреля 2020

Я пытаюсь получить данные с устройства BT. Я знаю, что могу использовать библиотеку InTheHand для этого в C#, но я не могу подключиться к устройству и получить данные. Я использую сниффер и знаю, что устройство работает и отправляю данные. Может быть, кто-то знает, как работать с BT в C#? Мне нужны только данные из него, а не отправка или др. c.

У меня есть этот код:

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 BLE{
static class Program
{
    private static string sCode = "0000";
    // My BT notebook
    private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("50:76:AF:99:D3:87"), BluetoothService.BluetoothBase);
    private static BluetoothClient BC = new BluetoothClient(EP);

    // The BT device that would connect
    private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("34:29:F0:F4:49:C8"));

    private static NetworkStream stream = null;

    static void Main(string[] args)
    {

        if (BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, sCode))
        {
            Console.WriteLine("PairRequest: OK");

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

                BC.SetPin(sCode);

                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);

                    for (int i = 0; i < numberOfBytesRead; i++)
                        myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);
                }
                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();
        }
    }
}

}

В консоли я вижу «PairRequest: Нет» ... Можете ли вы помочь мне получить правильный результат

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...