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: 4672
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: 4672
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: 4672
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: 4672
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: 4672
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 106 times
Thanks | Regards,
Dipen

Return to “Networking and servers”