Android Question How to parse incoming bluetooth data

Beja

Expert
Licensed User
Longtime User
Hi all!
Terminal App is receiving the data as per attached screenshot. But I couldn't receive it in B4A app.
The numbers in green color are the ones I want to parse.
Any help appreciated.

Screenshot_20250106_023244_Serial Bluetooth Terminal.jpg
 

emexes

Expert
Licensed User
Once you've connected your Android device to the Bluetooth "Classic" serial port, and you're receiving data via AsyncStream.NewData events, get back to us.

https://www.b4x.com/android/help/serial.html#serial_getpaireddevices

https://www.b4x.com/android/forum/threads/android-bluetooth-bluetoothadmin-tutorial.14768/

Note that the tutorial uses AsyncStream Prefix mode, which is a B4X special that works great for communicating with other B4X programs but not so great with Bluetooth devices generally. You should initialize your AsyncStream using plain old boring .Initialize rather than .InitializePrefix

1736152052001.png
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
If all you're receiving is a stream of decimal numbers per your example, I'd just go with something like:

B4X:
Sub ProcessGlobals
    Dim NumberBuffer As String    'global variable so that data not lost when split across multiple calls to astream_NewData
End Sub

Sub astream_NewData(Buffer() As Byte)

    For Each B As Byte In Buffer
        If "0123456789".Contains(Chr(B)) Then    'if B is an ASCII digit '0' to '9'
            NumberBuffer = NumberBuffer & Chr(B)    'append it to the buffer
        Else    'anything other than a digit is regarded as a delimiter
            If NumberBuffer.Length <> 0 Then    'ignore empty lines
                HandleNewNumber(NumberBuffer)
                NumberBuffer = ""
            End If
        End If
    Next
 
End Sub

Sub HandleNewNumber(NewNumber As Int)

    Log(NewNumber)
 
End Sub
 
Last edited:
Upvote 0

f0raster0

Well-Known Member
Licensed User
Longtime User
Note: I understand this is a very old example, and the recommendation is to use the updated example shared above.

However, I had some free time and decided to power up my 20-year-old Arduino and Bluetooth module.
To my surprise, I was still able to make it work using this classic example:


In the Arduino code, I set it up to send dummy temperature data, I can read that and also control ON/OFF the led-Arduino
Aruino-Bluetooth1.jpg
 
Upvote 0
Top