Android Question Bluetooth receiving data

bogdanc

Active Member
Licensed User
Longtime User
Hello:)
That is my firs post here.
So welcome everybody:)

I have problem with communication via Bluetooth.

I wrote small app on my phone. I connected to PC.
From PC i sending via Bleutooth from Putty terminal some data.

I have declared in Sub Globals:
B4X:
Dim Stream As AsyncStreams
Dim Data_receive as String
Dim Label1 as Label

Also I have:
B4X:
Sub Stream_NewData (Buffer() As Byte)
Data_receive = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Label1.Text=Data_receive

If Data_receive.lenght=7 Then
If Data_receive.EndsWith( Chr(13)) Then
Try
            ' Do something
            Catch
            End Try
        End If
      Data_receive =""
End If
End Sub

I displaying on Label1 all incoming data from Bluetooth and I don't receiving all data.

What Am I doing wrong?
 

walterf25

Expert
Licensed User
Longtime User
Hello:)
That is my firs post here.
So welcome everybody:)

I have problem with communication via Bluetooth.

I wrote small app on my phone. I connected to PC.
From PC i sending via Bleutooth from Putty terminal some data.

I have declared in Sub Globals:
B4X:
Dim Stream As AsyncStreams
Dim Data_receive as String
Dim Label1 as Label

Also I have:
B4X:
Sub Stream_NewData (Buffer() As Byte)
Data_receive = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Label1.Text=Data_receive

If Data_receive.lenght=7 Then
If Data_receive.EndsWith( Chr(13)) Then
Try
            ' Do something
            Catch
            End Try
        End If
      Data_receive =""
End If
End Sub

I displaying on Label1 all incoming data from Bluetooth and I don't receiving all data.

What Am I doing wrong?
Are you using prefix mode?
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
Are you using prefix mode?

I just trying to send from PC or any my device via Bluetooth simple string value
example: 2201100\n and I receiving random value of string.
sometimes full(what i wanted) string and many times just part of it.
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User
Any idea what am I doing wrong?

How to init prefix or non prefix mode on Bluetooth AsyncStream?
 
Upvote 0

bogdanc

Active Member
Licensed User
Longtime User


Thank You Erel for Your respond.
I saw this tutorial.

I still have problem and I cant resolve it.

Sending works fine, but receiving is not :(


I use a non prefix mode.
My init code:

B4X:
Dim Adapter As Serial
  
Sub Adapter_Connected (Flag As Boolean)
    'IF connected - init a Stream
    If Powodzenie=True Then
        Stream.Initialize(Adapter.InputStream, Adapter.OutputStream, "Stream")
    End If
End Sub



From my app I sending to microcontroller a query via Bluetooth.
B4X:
?

On Every query I send a string from microcontroller to my phone:
B4X:
123456\r


And on Stream_NewData I chcecikng incoming data.

B4X:
Sub Stream_NewData (Buffer() As Byte)
Data_receive = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Label1.Text=Data_receive

Label2.Text=Buffer.Length

If Data_receive.lenght=7 Then
If Data_receive.EndsWith( Chr(13)) Then
Try
            ' Do something
i=i+1
Label3.Text=i

            Catch
            End Try
        End If
      Data_receive =""
End If
End Sub


And every time when my Data_recive has lenght a 7 char's and ended ASCII char code 13 I increment variable i to see how many goods I revived.

And is only 30% of goods
 
Upvote 0
Top