Android Question How to transfer image as byte array with bluetooth

DickD

Active Member
Licensed User
I am using Bluetooth to send/receive info in a SQLite database. I have done this successfully with text data thanks to the Serial example provided in the tutorial on this topic. However, I don't see how to transfer an image in a byte array. I'm sure I need to use ReadBytes and WriteBytes to do this but I just can't get the syntax of defining, initilializing and use these correctly. I've tried a dozen combinations without success. The working program summary I'm using successfully for text is shown below. What is the byte array equivalent? I have the image in 'buffer1' as extracted from an SQLite BLOB.

B4X:
Dim Serial1 As Serial
Dim TextReader1 As TextReader
Dim TextWriter1 As TextWriter
Dim buffer1() as byte
Dim thisText as String
...
Serial1.Initialize("Serial1")
TextReader1.Initialize(Serial1.InputStream)
TextWriter1.Initialize(Serial1.OutputStream)
...
TextWriter1.WriteLine("Some text here")
thisText = TextReader1.ReadLine
 

DickD

Active Member
Licensed User
You should never use TextReader / TextWriter with network or Bluetooth streams.

You should use AsyncStreams.

Code to convert bitmaps to bytes: https://www.b4x.com/android/forum/threads/b4x-bytes-to-file.70111/#post-445167

Thanks. I read the AsyncStreams tutorial, downloaded the sample program and ran it successfully. I also read your Networker's Guide to the Galaxy carefully. That answered a lot of questions I had. But I'm still not clear as to which network solution would be right for my particular app. I want to automatically connect to other nearby phones/tablets running my software with as little user intervention as possible. The apps would handshake, notify the user when a matching app was found and exchange some information upon the user's request. I realize this may not be completely possible for obvious security reasons but I'd still like to limit user input in making the connection as much as possible. The data needs are not great, a picture (under 2Mb) and a few profile text fields.

I'd appreciate your suggestion as to the best network took to use.

Thanks
 
Upvote 0
Top