strawberry-8
Posts: 4
Joined: Thu Jun 13, 2019 2:54 pm

BLE connection with an android

Thu Jun 13, 2019 3:06 pm

Hello everybody,

I am a student and I have a project to do on a Raspberry Pi zero W. I need to create an android application to connect to Bluetooth Low Energy (if possible otherwise normal bluetooth) in order to exchange data. But the Rpi must be a device (when the product is finished, there will be no access to the Rpi). The link must therefore be from the application. I’ve already made the app, but when it displays all the devices captured, there’s not my address's card.
I should mention that I installed bluez on my Rpi but I don’t know what else to do at all, I totally start on Raspberry. I have been looking for documentation but none of the solutions I have found work (I have only found one or two).
If anyone has any clues, I’ll take it.

Thank you

Strawberry-8

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Thu Jun 13, 2019 4:06 pm

I just posted some Python code on Github to make a Raspberry Pi GATT server (peripheral).

Clone the repo, read the README, and try the code.

https://github.com/Douglas6/cputemp.git
Last edited by Douglas6 on Fri Jun 14, 2019 3:06 pm, edited 1 time in total.

strawberry-8
Posts: 4
Joined: Thu Jun 13, 2019 2:54 pm

Re: BLE connection with an android

Fri Jun 14, 2019 8:25 am

Thank you Douglas

I'll try that and tell you if it work.

strawberry-8
Posts: 4
Joined: Thu Jun 13, 2019 2:54 pm

Re: BLE connection with an android

Fri Jun 14, 2019 1:12 pm

It works. You're the best douglas.

I just have to use the code in 'cputemp.py' for my project or the others are useful too?

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Fri Jun 14, 2019 7:34 pm

You'll need to import from advertisement.py and service.py (which import bletools.py), but you should only need to make modifications to cputemp.py.

strawberry-8
Posts: 4
Joined: Thu Jun 13, 2019 2:54 pm

Re: BLE connection with an android

Mon Jun 17, 2019 1:02 pm

Yes, I had not seen that the code "cputemp" used the other functions.
I have an other question :
Is there a predefined uuid address for all data or do I need to create a new address ?

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Mon Jun 17, 2019 2:40 pm

You can use one of a number of predefined UUIDs for services (https://www.bluetooth.com/specifications/gatt/services/) or characteristics (https://www.bluetooth.com/specification ... teristics/). Or you can use your own random 128-bit UUID. I got mine from https://www.uuidgenerator.net/ and then just zeroed out the first few bytes and numbered them sequentially. Not required but good form.

dipen
Posts: 17
Joined: Wed Aug 03, 2016 6:41 am

Re: BLE connection with an android

Thu Jul 11, 2019 2:40 pm

Namaste Douglas6,

I too am grateful to you for this simple yet powerful application for BLE communication. Over the last week, I was only going crazy figuring out, how and where to begin for the Python based BLE development on Pi 0 W board.

Happy to inform that I can exchange strings between my Pi 0 W board and the 'nRF Connect' app on my Android phone.
Thanks | Regards,
Dipen

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Fri Jul 12, 2019 1:15 am

सुखिनोऽभव

dipen
Posts: 17
Joined: Wed Aug 03, 2016 6:41 am

Re: BLE connection with an android

Tue Jul 16, 2019 10:18 am

Namaste Douglas6,

When running the cputemp.py, the Service and Characteristic are discovered by the nRF Connect mobile App as 'Unknown Service' and 'Unknown Characteristic'.

Kindly guide me how and where to set these values in the Python code.

As an example, for cputemp.py, they could be
Service: Thermometer
Characteristic 1 - CPU Temperature
Characteristic 2 - Temperature Unit


In this case, I realise this would be same as the description of the 'Characteristic User Description' provided. But hope it makes sense that the 'Services' and 'Characteristics' must have some description for user interpretation, once the BLE device is connected.
Thanks | Regards,
Dipen

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Tue Jul 16, 2019 12:44 pm

I believe that nRF Connect labels services using the local name included in the advertising packet. I think local name is not working in BlueZ 5.43, so you may be out of luck. If you have upgraded BlueZ or are using Buster, you can try setting the local_name on the Advertisement.

I don't know what nRF Connect uses to label the characteristics, or if it uses anything at all in the case of non predefined characteristics. I was hoping it would be the CUD, but it's not.

dipen
Posts: 17
Joined: Wed Aug 03, 2016 6:41 am

Re: BLE connection with an android

Tue Jul 16, 2019 1:52 pm

Namaste Douglas6,

Below is the snapshot of what I see on the nRF Mobile App.
nRF_mobileApp_detection.JPEG
nRF_mobileApp_detection.JPEG (64 KiB) Viewed 2535 times
Thanks | Regards,
Dipen

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Fri Apr 10, 2020 12:55 pm

Hello all, i have a similar requirement with the raspberry pi and android. Till now i am able to follow all the steps in this post till connecting the pi to android service thermometer. But i am unable to transfer strings from raspberry pi to android. Please let me know how to go about it.

deepo
Posts: 548
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: BLE connection with an android

Fri Apr 10, 2020 3:08 pm

Just a note on how to make it run on Buster Lite on a Raspberry Pi 3B+:

sudo apt-get install git
git clone https://github.com/douglas6/cputemp.git

sudo apt-get install python3-dbus
sudo apt-get install python3-gpiozero

cd cputemp
sudo python3 cputemp.py

/Mogens

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Sat Apr 11, 2020 10:03 pm

Hi thanks for your reply i have pi zero w board but i am unable to exchange strings.
The board is geting connected with nrf connect app but no exchange of strings. is any additional step required for string exchange.

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Sat Apr 11, 2020 10:51 pm

tshivam wrote:
Sat Apr 11, 2020 10:03 pm
Any additional step required for string exchange.
That's proprietary and almost completely up to the manufacturer. Generally they don't publish their protocols. You should be able to see what characteristics are available by opening the device with nRF Control, and there may be CUDs (Characteristic User Descriptions) that give some hints. It will require some reverse engineering. A BLE sniffing device (Adafruit has one) can be very helpful.

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Sun Apr 12, 2020 5:49 am

Okay so the code cputemp.py has any command for sending data to android mobile nrfconnect app or i have to put some code.

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Sun Apr 12, 2020 11:47 pm

Cputemp.py (referenced above) acts as a BLE server. That means it responds to requests from from a client (say, nRF Connect on your phone). If the client requests a characteristic, cputemp.py will respond with the value of that characteristic. The server can also be instructed to periodically send the characteristic. it's not clear to me what you are trying to do, but some more research into BLE and nRF Connect will probably help.

petzval
Posts: 25
Joined: Sat Aug 10, 2013 12:15 pm

Re: BLE connection with an android

Mon Apr 13, 2020 10:30 am

Have a look at the Apr 11 post here:

viewtopic.php?f=29&t=237178

It is matched Android and Pi code that will exchange strings over regular Bluetooth (not BLE).

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Wed Apr 15, 2020 10:49 am

Hi @douglas i am trying to send a .txt file from raspberry pi zero w to android mobile application BLE Terminal or my custom application. There are two ways of doing the same i.e complete file transfer or file read any of the method will be helpfull for me.
Please guide me. I have succesfully done it via arduino to android but i want to do it by raspberry pi.

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Thu May 28, 2020 5:50 am

Hi douglas,
Many thanks for your support. I am able to exchange strings over ble from raspberry pi to my custom android application.
But my requirement is to continously send string. I need to transfer txt files.

User avatar
Douglas6
Posts: 4850
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: BLE connection with an android

Thu May 28, 2020 3:58 pm

If using BLE, you'll need to break up the text file into small pieces (18 - 20 bytes) to transmit, and then assemble them on the other end.

deepo
Posts: 548
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: BLE connection with an android

Thu May 28, 2020 7:14 pm

tshivam wrote:
Wed Apr 15, 2020 10:49 am
Hi @douglas i am trying to send a .txt file from raspberry pi zero w to android mobile application BLE Terminal or my custom application. There are two ways of doing the same i.e complete file transfer or file read any of the method will be helpfull for me.
Please guide me. I have succesfully done it via arduino to android but i want to do it by raspberry pi.
Were you able to transfer files from Arduino to Android via BLE?
How?

Do you know for a fact that you can transfer files over BLE, or do you wish that you could do so?

And why go through files?
Why not just send advertisement messages with the temperature or whatever it is you want to send?

/Mogens

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Fri May 29, 2020 11:17 am

No i did not transfer files from arduino to android. I was reading a txt file byte by byte and transmitting over ble.
Okay so how can i change the cputemp code so that i can continously send strings without selecting a characterstic every time.

tshivam
Posts: 11
Joined: Wed Mar 18, 2020 4:39 pm

Re: BLE connection with an android

Fri May 29, 2020 11:38 am

Hi @ Douglas thanks for the suggestion, but it would be okay even if i don't break the file and transmit (18-20 bytes) data sequentially in active mode as ble has active and sleep mode.
I have done the same with Arduino and android.
It would be helpful for me if you can let me know where to tweak in the code.

Return to “Networking and servers”