Get Data from Bluetooth device in C#
Asked Answered
B

1

6

I'm trying to get data from a medical BT device that I already have pairing code and communication protocol.

Looking for some code I've got this code:

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

}

And this is the console result:

Result

While I expect something like 0XA5 0x05 0x04 0x01 etc. etc.

Can you help me to get the correct result?

Boldfaced answered 26/3, 2014 at 17:38 Comment(2)
instead of Encoding.ASCII, try using UTF8 or Unicode.. do you know what is your source encoding?Valletta
Hello, I've already tried other Encoding but I get only other symbols as result. No, in the communication protocol that I've there is not specified the source encoding.Boldfaced
R
8

You want to replace the following:

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

with

for (int i = 0; i < numberOfBytesRead; i++)
    myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);
Rapids answered 26/3, 2014 at 18:8 Comment(3)
Thank you! This was the mistake. Now I can read the bytes from the device. But how can I now convert the bytes in something more readable?Boldfaced
@YiyiChen Well, it depends on your medical device. It should have some documentation with it about protocol/messages format.Rapids
It's the first time that I do something like this. I saw if I put a break point on for (int i = 0; i < numberOfBytesRead; i++) in the debug mode I can see the data in the myReadBuffer variable. This is the communication protocol: dropbox.com/s/kmh627o53rzghwr/Communication%20protocol.pdf , can you tell me what type format it uses?Boldfaced

© 2022 - 2024 — McMap. All rights reserved.