1

I have a Bluetooth application that accepts connections from other Bluetooth devices and receives a Stream object from them. I need help getting the data as a string so I can display in a TextView object I have in my app.I tried the method below from Microsoft documentations but the target variable that should hold the string from the Stream is of type int and so am unsure if doing textView.Text=data.ToString() on an int object will display the data from the devices.

BluetoothSocket source_device = mysocket.Accept();
// get the input and output streams
output = source_device.OutputStream;
input = source_device.InputStream;

// check if data is available and write it to our UI object
if (input.IsDataAvailable())
{
    // way of reading the data from stream on Microsoft documentation
    byte[] bytes= new byte[input.Length];
    int bytesToRead = (int)input.Length;
    int numBytesRead = 0;
    do
    {
        // Read may return anything from 0 to 10.
        int n = input.Read(bytes, numBytesRead, 10);
        numBytesRead += n;
        bytesToRead -= n;
    } while (bytesToRead>0);

    // am unsure if this will display the data I need as it is just an int
    _data.Text = numBytesRead.ToString();
Palle Due
  • 5,929
  • 4
  • 17
  • 32
TechGeek
  • 316
  • 1
  • 11
  • 3
    you surely want to convert the array of bytes to string, not the number of read bytes. See https://stackoverflow.com/questions/1003275/how-to-convert-utf-8-byte-to-string for further help. – MakePeaceGreatAgain Jun 09 '22 at 09:45
  • 2
    In addition to @MakePeaceGreatAgain comment, you need to know, which Encoding was used to create the byte array – TheTanic Jun 09 '22 at 09:46
  • which array of bytes, the `numBytesRead` is the variable that holds the data read from the input stream? – TechGeek Jun 09 '22 at 09:47
  • `numBytesRead.ToString()`would be just the default `ToString()` implementation from `object` not sure what format the data has you may need to cast it with the `binaryformatter` or read it from the bytes as the right kind of string encoding e.g. `UTF8` – Mucksh Jun 09 '22 at 09:48
  • @MakePeaceGreatAgain, understood, Thank You. The `numBytesRead` is for the `do while loop` – TechGeek Jun 09 '22 at 09:48
  • I have a way with `System.Text.Encoding.ASCII.GetString(byte[])` Thanks for your contributions guys – TechGeek Jun 09 '22 at 09:49
  • 1
    `bytes` contains the information. `Read` returns the amount of readbytes. This is meta information for you, as well as the accumulated `numBytesRead`. – Mong Zhu Jun 09 '22 at 09:50
  • 2
    Using bytesToRead>0 is wrong. A lot of code uses this and it is not correct. You could be reading faster than the data is being sent and underrun the buffer and exit before all the data is received. Text data should be sent using a terminating character and you should read until the terminated is found. With binary (or text) precede the data with a byte count and read until all the bytes are received. – jdweng Jun 09 '22 at 09:52
  • 1
    Be aware that ```System.Text.Encoding.ASCII``` is a 7-bit encoding that only supports English letters. There's not even support for other languages using Latin characters such as French or Spanish. If you have text entered by a user, the ASCII encoding is most likely not the right choice. – Christof Wollenhaupt Jun 09 '22 at 09:55
  • "Bluetooth devices and receives a Stream object from them." do you have the specs for the data communication protocol? does it use a termination character at the end? Devices can be tricky to talk to. Your code might work 40 times but on the 41.st the device might decide to be slower than your reading procedure. – Mong Zhu Jun 09 '22 at 09:55
  • @ChristofWollenhaupt, the text is just `Say Hello` – TechGeek Jun 09 '22 at 09:57
  • @TechGeek Is that all that is ever going to be transmitted? If you have a limited and predefined set of strings that could be sent, then ASCII is fine. However, if ```Say Hello``` is merely your test string and eventually users are able to enter text themselves, then ASCII might not be sufficient. – Christof Wollenhaupt Jun 09 '22 at 09:59
  • @ChristofWollenhaupt, it is all that the client app will send and its been hard coded into it. – TechGeek Jun 09 '22 at 10:01
  • 1
    anyreason your not using a stream reader? https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=net-6.0 – MikeT Jun 09 '22 at 11:12
  • Use `StreamReader.ReadToEnd()`. Also you need `using` on most of those objects – Charlieface Jun 09 '22 at 12:38
  • @MikeT, that is actually a better idea – TechGeek Jun 09 '22 at 13:19

0 Answers0