Android Question binary data with bluetooth

kenad

Member
Licensed User
Longtime User
Hi Erel
Your serial example works well.
I want to send and receive binary data , not text
Instead of using textwriter , and textreader to read the serial buffers

TextReader1.Initialize(Serial1.InputStream)
TextWriter1.Initialize(Serial1.OutputStream)


How do you get the data into a byte array?
I need to be able to send bytes with values from 0-0xff and the textreader/writer gets in the way

The system will be getting 34 bytes every 500 msec

Is there a length command or some way of telling how many bytes the buffer has in it?
In VB6 I would do something like this


Dim datain as variant
Dim dataout(31) as byte


If MSComm1.InBufferCount >=34 then
Datain = MSComm1.Input


I then process the data put it in the dataout buffer and return it to blue tooth

MSComm1.output = dataout


What would be a similar way in B4A?

Thanks for your help
Ken
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

kenad

Member
Licensed User
Longtime User
Ok Thanks
I did try AsyncStreams but the program stopped as soon as I tried to connect to a bluetooth connection.
I will probably investigate the program again.

as my project totally revolves around the blue tooth , I did the following with the serial example, and if works for my requirements.

'Activity module
Sub Process_Globals
Dim Serial1 As Serial
Dim tmpin As InputStream
Dim tmpout As OutputStream
Dim bufin(1023),bufout(1023) AsByte

End Sub

Sub Serial1_Connected (Success AsBoolean)

If Success Then
tmpin=Serial1.InputStream 'connect streams to input and output streams in program
tmpout=Serial1.OutputStream
connected = True

' this section writes bytes to bluetooth
tmpout.WriteBytes(bufout,0,7)

this section reads from bluetooth but waits using doevents

'main loop
DoWhileconnected = True

'first wait for serial data

DoWhile bytenum = 0
bytenum= tmpin.BytesAvailable
DoEvents
Loop

Timer1.Enabled=True 'wait 20 ms , and read buffer
DoWhileTimer1.Enabled=True
DoEvents
Loop
'then read serial info
byteread=tmpin.ReadBytes(bufin,0,bufin.length)
 
Upvote 0

kenad

Member
Licensed User
Longtime User
Is there a sample program that works on android 4.4.2
I tried the bluetooth sample , but it crashes about 1 sec after searching for devices, there is only one device available.
does it require more than one?
I tried the compiled program on 2 different tablets to see if it made a difference, but they both reacted the same way.
the serialexample sample file compiles and works on both test tablets , so I know the b4a is compiling properly, and that the test units connect properly.
 
Upvote 0

kenad

Member
Licensed User
Longtime User
this is a log from the bluetooth program , I don't know enough about Java to know why this program fails, perhaps you could let me know what is wrong?
I haven't made any changes to the sample file.
Thanks for your time
Ken
 

Attachments

  • bluetooth dropout log.txt
    2.5 KB · Views: 255
Upvote 0
Top