Android Question Advice on learning programming with b4a

Woody

Member
Licensed User
Hi,

I toyed with b4a for some time now. I read the tutorials, am able to create a simple app, test it and get it in the app store. As long as it is a simple app with an activity, a couple of buttons and text fields, that is easy. But as soon as I need more 'control' I find myself drowning in OOP, libraries, classes etc.

As an example: At the moment I'm trying to make an app that talks to a PIC controlled Bluetooth device I create some years ago. Got all the BT examples, implemented Asyncstreams (changed the firmware in my Bluetooth device to support that) and managed to change the example BT 'chat' app to control my device and to show the data it sends. I now need to inspect, convert and use that data. There are libraries for that, but how do I use them? A statement like

B4X:
CallSub2(ChatActivity, "NewMessage", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))

has me more or less baffled.

It is not that I fail to understand what is needed. I'm able to do this is in plain C or assembler on the PIC controller at the other side of the BT connection. But I miss some connection with the way one writes code in an Android - B4A environment. It seems to need a different way of thinking about programming. Can anyone point me to sources to enlighten me? Or is the best way the way I use now: looking at examples and then trial-and-error until I have something that suddenly works?

Regards,

Paul
 

Sandman

Expert
Licensed User
Longtime User
Or is the best way the way I use now: looking at examples and then trial-and-error until I have something that suddenly works?

That's what the rest of us do. However, on top of my mind I have four more resources worth mentioning:
  1. B4X Video Tutorials by @Erel
  2. Documentation Booklets by @klaus
  3. B4A, B4i, B4J and B4r API documentation - B4X Object Browser by @Vader
  4. The forum itself. Make an effort on posting well written questions (post in correct subforum and use code tags for the love of everything) and you will very often get excellent help

And be prepared that you for a very long time will find yourself in the situation where you discover a post hidden deep in a subforum that really changes how you do some things. Almost always for the better, thankfully.
 
Upvote 0

Woody

Member
Licensed User
Thanks! Good to read that other people fly by the seat of their pants.

1, 2 and 3 are being used ATM. As for the forum, it is a brilliant source of info. Getting where I am with above mentioned app would have impossible without it.

Maybe I need to read up on OOP and/or Java to get a better feel for programming an Android - B4A app.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
My 2cents
Besides the above, and more specifically from a "RTOS-less C/Assembler to B4A transition" point of view, I'd say that the 3 main "concept differences" that I found, are:
  • B4A / Android code is a high-level event-driven language: these events can come from user-interface, peripherals (including timers) usually managed by libraries that receive lower-level events and output higher-level ones, other code, or the same OS (which at the same time manages the life-cycle). This is different to what most of (specially non-RTOS) C-code in microcontrollers does (*)--> perhaps a lot of State-Machines with manually shared time to simulate threading, and perhaps manually polling peripherals to check state transitions.
  • Modular structure: Activities, Services, classes and code modules --> even if it is explained in the tutorials and you have read it, worth to read it again (and again) until this structure is more than familiar to you. It can be a bit discouraging to find that there is not a unique approach to what you want to do (when to use one or more activities... there are different opinions depending on code complexity). Classes are a "commodity" to encapsulate things and behavior (simplifying a lot, they are similar to C-structs, that include their own managing functions) and user can again choose to use them or not (recommend to use them as much as possible). Also, language-specific functions to interact between Activities/Services (Callsub, CallSubDelayed need attention and reading well their differences)
  • Types: bytes are signed and pointers do not exist: instead there are variable instances that work 'equally' for primitive types, and a bit different (need to be initialized) for other data types. Can be confusing at the beginning specially if you send data back-and-forth and are used to pointers.

(*) Yes, it's also true that this 'higher level' structure can be 'approached' in embedded devices with C or Assembler, and ugly spaghetti code can also be produced with B4A. But each language 'eases' going into a direction depending on the facilities or restrictions that it offers --> Also, Arduino (and then B4R), as evolutions to the plain C, ease this 'modular' thinking (taking into account that there is not Android behind, but there is a life-cycle)

And be prepared that you for a very long time will find yourself in the situation where you discover a post hidden deep in a subforum that really changes how you do some things
I'm almost sure that someone hides them, and he/she is the same one who hides odd socks after the laundry :D
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Maybe I need to read up on OOP and/or Java

Considering your background I'd venture a guess it's more a case of un-learning your C and assembler ways of doing things. There are numerous users here with close to no programming background that manages to produce apps. It's not overly difficult to start from scratch, but it can be tricky if you come from a different programming background.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
the key in programming for bigger or complex projects are classes (OOP) because they bundle a functionality to a thing and make the live easier.
it did not mean that u must use the full spectrum for classes. if u make things complicated or messy / abnormal your app get buggy.
structures / struct in c++ / type in b4x / in java a class .. also used daily.

B4X:
CallSub2(ChatActivity, "NewMessage", BytesToString(Buffer, 0, Buffer.Length, "UTF8"))

would be more readable
B4X:
Dim Chat as ChatClass
Chat.Initialize

Chat.Add(Message)
 
Upvote 0

Woody

Member
Licensed User
Thanks for the info. The event driven-ness of B4A/Android I get. Very like Visual Basic. I will reread about the modular structure again cause I think that is where I miss out. And then most likely the best way to learn this is to soldier on by trial and error. More or less how I learned assembler and C way back when :)

Regards,

Paul
 
Upvote 0
Top