Android Question binary data with bluetooth

Discussion in 'Android Questions' started by kenad, Jun 4, 2015.

  1. kenad

    kenad Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Peter Simpson likes this.
  3. kenad

    kenad Member Licensed 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)
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    You shouldn't use such code. It will not be reliable.

    You should use AsyncStreams and never try to block the main thread with such loops. It will not work.
     
    Peter Simpson likes this.
  5. kenad

    kenad Member Licensed 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.
     
  6. kenad

    kenad Member Licensed 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
     

    Attached Files:

  7. Erel

    Erel Administrator Staff Member Licensed User

    The Bluetooth example based on AsyncStreams should work on all Android devices running Android 2.0+.

    For the example to work you need to run it on two devices and connect them.
     
    Peter Simpson likes this.
Loading...