Bluetooth on Android

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Bluetooth on Android

Post by mnf »

A question (only partly Flowcode based)

I want to connect Bluetooth (hc-06 but have tried hc-05 too) to an Android phone - it is set on an Arduino and will connect and transmit to a PC perfectly.

It will pair to an Android phone (Android 10) - and connect to a serial terminal (the snappily named Serial Terminal Bluetooth) program on the phone..

But - I cannot get it to connect to my own program. I've tried Java (using the google bluetooth example), Kotlin, and in desperation Fusion 2.5. Even App inventor from MIT (though struggled with that!)

Does anyone have any working code - or pointers to an open source serial program... It feels as though it should be straightforward - but all my attempts return an error on connect...


Martin

stefan.erni
Valued Contributor
Valued Contributor
Posts: 654
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 179 times
Contact:

Re: Bluetooth on Android

Post by stefan.erni »

Hi Martin

I used the terminal program from here and it's working fine to connect to my hardware and transmit simple commands to start and stop my recorder
I think Hi also provides the source code.

http://www.kai-morich.de/android/

regards

Stefan

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thanks Stefan,

Source (at least for one version) - seems to be available on github - I'll take a look after work..

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

Hi Martin,
I have created a few Android apps for communicating via Bluetooth.
This is one of them.
The app sends data to the PIC (MP3 player), but the PIC also sends data back you you know commands have taken place.
I have also created a more complex one that converts incoming UART into different arrays.
It's more complex as App inventor MIT did not have arrays.
Not sure about now as its been fair while since used it.
If you let me what what you want teh app and PIC to do, I can create something for you.
I can also send you the source code so you can learn how it works.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thanks Martin,and

Looks ideal!

If I can get the Bluetooth to connect I'll be happy... What I want to do is have a transmitter in the loft that signals when a water container is full (or better a water level) - phone will display a level indicator or sound an alarm (only had one flood so far)
I got to the stage of reading the paired devices and got the mac address. Unfortunately that's as far as if goes - connect always fails... (I can however get two android devices to connect and communicate - just not the hc-05/6) The 'connect' option in bluetooth settings on the phone also doesn't connect (but serial terminal can :-( aarrgghhh)
The Arduino side is currently just sending a string (works well - can receive this on PC or in Bluetooth terminal on phone...)

I didn't get very far with app inventor - my old phone refused to play ball (android 6) - now I've upgraded to 10 things might go more smoothly?


Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

I find Thunkable classic worked really well when App inventor was playing up.
To give you and idea how good the data can work between the HC05/06 and phone.
Here is the data sent:

Code: Select all

>80 378 D:13 0 15 10 659 454 4512 0 0 0 10110001 0 0 23.0
R21:17:38(MT) Mon02/11/2020 Boost_En:05:00-22:00
Here is the mobile receiving data:
Receieved Data.jpg
Receieved Data.jpg (27.44 KiB) Viewed 14580 times
As there is lots of data to decode and display, the app scrolls vertically so button is not really cut off.
The greyed out buttons are only greyed out in normal mode.
The app detects when in installer node so then appear bold.
I will need a sample of the data string being sent.
I will also like to know what you want the app to look like if you could put together a drawing?
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thanks - in the first instance just a 'Connect' button (change to 'Disconnect' when connected)

A text box to show data received..

Maybe a button to send a value to Arduino?

Doesn't need anything too fancy yet... I'd like to understand why the connection isn't happening first...

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thunkable - a new one to me will have to take a look. But tomorrow - 'cos today was horrendous busy at work :-(

Feeling slightly (read very) jaded...

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

No problem.
Probably the way to go for now is with App inventor 2 as Thunkable classic is no longer supported.
When I get a chance, I will create a simple window displaying Value from ADC.
Also I will a a single button that will send Hello World string.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Managed to get a connection with app inventor 2...

Can't see why my other efforts refuse to connect - but - we can use app inventor if we must....

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

That's A start lol.
Just creating a Flowchart that sends two ADC values.
What to want to happen when you push a button on the phone?
LED light on the Arduino for a couple of seconds for example.
The issue with Arduino displaying received txt via UART is, the UART is already being used to transmit to Bluetooth, which is 3V3.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Should be okay on the arduino side - I've used a software port for the Bluetooth and it works perfectly...

Struggling a bit with the app inventor side of things though - for example how to add a string to a text box - or connect on start-up (got round this with a button;) )

Have to work through a few of the tutorials I think..

That's pretty cool - I like the way the app updates instantly on the phone when change something (Android Studio doesn't do that!)

Got a recursive Fibonacci procedure up and running.... A bit slow to use - will take a bit of getting used to how to click the blocks together...



Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

Hi Martin,
Something for you to play with to give you an idea.
Attachments
WaterLevelSensor1.zip
(165.49 KiB) Downloaded 227 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thanks Martin,

I think it could work...

An Arduino with Bluetooth.

Two (bare) wires on a plastic strip attached to the side of the 'tank' - one connected to 5v and one to an analog pin (with a resistor (200k at present) pulling to ground)

A brief test - the analog voltage measure does vary depending on the depth of water. I suspect that using wire with slightly higher resistance will work better still - however for testing purposes I'm using tap water which possibly has better conductivity than 'dehumidifier' water (will have to test this)

I'm tempted to add a second analog wire at 'max fill' level to act as a failsafe alarm.

One challenge will be getting power consumption low enough for battery operation (it doesn't however, need to run continuously - once a day would be sufficient)

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

I created the App to work as an array.
So by just altering array numbers you could have say 10 different analogue/digital inputs and each status will show on the mobile.
Can you see the values on your mobile with 2 ADC converters?
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

Hi Martin,
attached will work much better and faster.
No flickering either.
I have changed the array number to 10 which will allow from 1 to 10 individual inputs to be displayed on the app.
Have you got it to work okay with app displaying both ADC values?

You can do all sorts with the App, e.g have a slider to control the brightness or flash rate of LED.
Attachments
WaterLevelSensor2.zip
(162.53 KiB) Downloaded 196 times
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Hi Martin,

Thanks - haven't had much time to play - so yesterday was simply getting the hardware to work (and I used a bluetooth connection on the PC for this) However took a good look at the app inventor code - and things are starting to make sense there too :-)

Hopefully get it up and running this evening.

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

No problem Martin.
What I do to live test i.e not install apk on the mobile.
Install then run on mobile MIT App Inventor 2.
I find it better to tick use Legacy Connection.
On the PC browser connect to App inventor 2, then select Connect, AI Companion.
A Qcode will pop up.
On your mobile, Select Scan QR code.
After a short time, you should see image from Designer appear on your phone.
Any adjustments made to blocks should be updated live on your mobile
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

The instant update is brilliant...

I got a bit bogged down on adding a gauge to my software... Okay bogged down on designing a gauge. But now have something I rather like - so should go from 'green' (empty) to 'red' (eeek)

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

Sounds really good.
Does it work as expected?
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Yes - and no :-(

Got a nice gauge into a canvas and then failed to make a pointer 'sprite' rotate to point at values.
Tried drawing a line instead...

Finding the inventor interface a touch frustrating at present (a search function would help) - but I seem to disconnect blocks easily and then struggle to get them to join together again - I'll persevere, as I'm sure a bit of familiarity will help, but if I could get the Bluetooth working in another language I'd be using that right now (and I'm thinking a try with B4A might be happening next...)

Tried - setting HC-05 (changed from an 06 to see if it helped) to a different name, to master mode, different baud / stop bits etc. Nothing seemed to help :-( Can connect to terminal program (or app inventor) and nowhere else.

I need to do some nice relaxing programming that doesn't involve graphics (or Bluetooth) - to unwind :)

Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Found a BLE device and that talks nicely to B4A - data transferring from FC no problems....

Might be a route to take (and good for battery life too!)

Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Bluetooth on Android

Post by medelec35 »

I don't have any issues with HC05 or HC06 as created a flowchart that can program both.
It can set the baud, the pin and the name with a number after, with is automatically incremented.
This is in case you have a fair few that require programming.
I Could adapt it for an Arduino if you are interested?

Glad you are getting further.
Have you got link to the BLE version that works please?
As for the App inventor 2.
For the indicator you could create two colour rectangles, one red and one green.
The Red can be a fixed size in width
You can the the app to control the width of the green which is exactly in the same position as the red i.e laying over it.
I.e the green gets narrower as the water level gets lower.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Bluetooth on Android

Post by mnf »

Thanks,

Yes the FC side is okay - though in the end I used a FTDI device to test and reprogram the Bluetooth adapters.

The BLE version works 'as is' - ie FC can just send data to the adapter and it works transparently (although the default BAUD seems to be 115200) - the Android side is very different though, no pairing.

I'm impressed with B4A (and it's now free! - lot's of downloading to start with) - I happened on a BLE example first but the HC-05/6 should be usable easily too.

I like the 'rectangles' idea for the gauge - mine was a bit ambitious for a first attempt...

Martin

airfoxnine
Posts: 1
Joined: Fri May 28, 2021 8:49 am
Location: Neavada
Contact:

Re: Bluetooth on Android

Post by airfoxnine »

Should be okay on the arduino side, I've used a software port for the Bluetooth and it works perfectly.

Post Reply