Android Question Receive data Bluetooth

red30

Well-Known Member
Licensed User
Longtime User
I managed to get the data but when i send data from the terminal, nothing comes. But if i send data from micricontroller via bluetooth, the data comes with this error. Moreover, [00 46 00 0b ff ff 00 00] must come but only [46 00 0b ff ff 00 00] comes. Where is the first bite?
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
    Dim AStream As AsyncStreams
    Dim bc As ByteConverter
End Sub

Sub Globals
    Dim readings As EditText
    Dim label1 As Label


End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    readings.Color = Colors.black
    If AStream.IsInitialized = False Then
        'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
        AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
    End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    readings.Text=Buffer(0)
    label1.Text=("Data: " & bc.HexFromBytes(Buffer))
End Sub

Sub AStream_Error
    ToastMessageShow("Connection is broken.", True)
End Sub

Sub AStream_Terminated
    AStream_Error
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        AStream.Close
    End If
End Sub

Sub LightOn_Click
    AStream.Write(Array As Byte(0x00,0x05,0x3f))
End Sub
 

Attachments

  • Screenshot_2016-09-20-13-14-59.png
    Screenshot_2016-09-20-13-14-59.png
    93.8 KB · Views: 276

red30

Well-Known Member
Licensed User
Longtime User
When I press send: Size:1
You receive an error when click "yes" then: Size:7
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Show your code from the new data sub please.

You are accessing an array there but use the wrong index. Note the index starts with 0 and not 1
A size of 3 have the indexes 0,1 and 2... But you are accessing index 3
A size of 1 have the index 0... But you are accessing index 1

Check and fix your code
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
B4X:
#Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
    Dim AStream As AsyncStreams
    Dim bc As ByteConverter
End Sub

Sub Globals
    Dim readings As EditText
    Dim label1 As Label


End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("2")
    readings.Color = Colors.black
    If AStream.IsInitialized = False Then
        'AStream.InitializePrefix(Main.serial1.InputStream, True, Main.serial1.OutputStream, "AStream")
        AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams")
    End If
End Sub

Sub AStreams_NewData (Buffer() As Byte)
    Log("Size: " & Buffer.Length)
    'readings.Text=Buffer(0)
    'label1.Text=("Data: " & bc.HexFromBytes(Buffer))
     If (Buffer(1)=0x46) Then 'Инициализация
     label1.Text="Ок"
     End If
End Sub

Sub AStream_Error
    ToastMessageShow("Connection is broken.", True)
End Sub

Sub AStream_Terminated
    AStream_Error
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        AStream.Close
    End If
End Sub

Sub LightOn_Click
    AStream.Write(Array As Byte(0x00,0x05,0x3f))
End Sub

I transmit: [00 46 00 0b ff ff 00 00]
 

Attachments

  • Screenshot_2016-09-20-13-14-59.png
    Screenshot_2016-09-20-13-14-59.png
    93.8 KB · Views: 220
Last edited:
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
Are you sure all of the message has been received when NewData is called the first time, perhaps the rest is received a short time later and NewData is called again.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
Are you sure all of the message has been received when NewData is called the first time, perhaps the rest is received a short time later and NewData is called again.

I'm not sure, if you're right how can I correct it?
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
You probably need to build up another array until you have all of the message. In my case i was looking for CR (13) and i was receiving characters as bytes so it was useful to append the received bytes onto a string and check for CR.

B4X:
Sub HandleAStream_NewData (Buffer() As Byte)
    For i =  0 To Buffer.Length-1
        aStreamRxString.Append(Chr(Buffer(i)))
        If Buffer(i) = 13 Then

'      do something with the string
'      then initialise aStreamRxString and carry on receiving
        End If
    Next

If i did not receive the CR in this call the NewData routine exits and gets called again when more data has arrived. I seem to remember this code was based on an example from B4A.

If you are receiving just bytes then perhaps use another Byte array (something like rxBuffer(8) which is long enough for the message - you may also want to use a fill pointer) and copy the newly received bytes from Buffer onto the end of any data you have already received and put in rxBuffer. When rxBuffer is holding the correct number of bytes for a message you can analyse it.

Notice that you should not try and read past the end of the Buffer() parameter and you may want to timeout the receiving process in case the sender stops sending.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
SteveTerrell, thank you very much, I nearly understand you.
I don't understand, why, when I send 8 bytes from the microcontroller via bluetooth, I get these 8 bytes piece by piece? (Ex.i send 8 bytes , I receive 1 , then 7 or 2 then 6.)
I used AStreams from Erel's example about USB, and everything comes in one piece. What's wrong with Bluetooth?
Notable, that when I send any message terminal via Bluetooth, I receive nothing... The programme works only from microcontroller.
 
Upvote 0

SteveTerrell

Active Member
Licensed User
Longtime User
SteveTerrell, thank you very much, I nearly understand you.
I don't understand, why, when I send 8 bytes from the microcontroller via bluetooth, I get these 8 bytes piece by piece? (Ex.i send 8 bytes , I receive 1 , then 7 or 2 then 6.)
I used AStreams from Erel's example about USB, and everything comes in one piece. What's wrong with Bluetooth?
Notable, that when I send any message terminal via Bluetooth, I receive nothing... The programme works only from microcontroller.

Remember that the Bluetooth system does not know how many bytes you are going to send so it cannot wait and send the complete message as a single block. There are many factors that can affect how many pieces are sent including how fast the transmitting end is given the bytes, what the Bluetooth system is doing at the time as well as what the receiving end is doing.

You might want to search the forum for AsyncStreamsText and AsyncStreams in "prefix" mode to get some more ideas about what is happening.
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I did something like this:
B4X:
Sub btnOpen_Click
    AStream.Write(Array As Byte(addr,0x05,0x3f)) '(addr,0x05,0xfb))
End Sub

Sub AStreams_NewData (Buffer() As Byte)   
    For i =  0 To Buffer.Length-1
        New123(j)=Buffer(i)
        j=j+1
        If j = 7 Then
            j=0
            Label1.Text=("Data: " & bc.HexFromBytes(New123))
            If (New123(1)=0x46) Then 'Инициализация
                Label2.Text="Пришёл "&New123(1)
            End If
        End If
    Next
Every time forward to "Send" - comes:

01 46 00 0c ff ff 00 00
bb 01 46 00 0c ff ff 00
00 bb 01 46 00 0c ff 00
ff 00 bb 01 46 00 0c 00
ff ff 00 bb 01 46 00 00
0c ff ff 00 bb 01 46 00
46 00 0c ff ff 00 bb 00
01 46 00 0c ff ff 00 00

A must always come: 01 46 00 0c ff ff 00 bb

Why is that?
What's my mistake?
 
Upvote 0

red30

Well-Known Member
Licensed User
Longtime User
I figured out! Everything is working! Error that bluetooth late sending adds 0.
B4X:
Sub btnOpen_Click
    AStream.Write(Array As Byte(addr,0x05,0x3f)) '(addr,0x05,0xfb))
End Sub

Sub AStreams_NewData (Buffer() As Byte)  
    For i =  0 To Buffer.Length-1
        New123(j)=Buffer(i)
        j=j+1
        If j = 8 Then
            j=0
            Label1.Text=("Data: " & bc.HexFromBytes(New123))
            If (New123(1)=0x46) Then 'Инициализация
                Label2.Text="Пришёл "&New123(1)
            End If
        End If
    Next

When I post 01 46 00 0c ff ff 00 bb, comes 01 46 00 0c ff ff 00 bb 00

But still do not understand why the messages come through bluetooth parts?
 
Upvote 0
Top