Bluetooth Low Energy- bluetooth on Raspberry pi 3 — (third part)

Fares Othmane Mokhtari
Dev TEch
Published in
5 min readAug 29, 2018

in this blog I will assume that you know what is bluetooth low energy (you can check my first blog about it if you don’t ) and you have an idea about its stack (you can check my second blog if you don’t) and you haven’t used golang before so I will start step by step by how to install it in your raspberry pi how to configure its paths and how we can use it to make a iBeacon central and GATT peripheral so lets start

what is golang :

golang is a multi platform open source programming language created by Google in 2009

you can check this link to get a deep idea about.

install go on the raspberry :

go has a version for ARM microprocessor so we need just to go to this link and download the zip file for ARM.

after that you have to extract your you file I assume that you have extracted it in your home work (/home/pi)

tar -C ~ -xzf go$VERSION.$OS-$ARCH.tar.gz

now we need to create gopath folder I assume that you have created it in your home work as I did (/home/pi/gopath)

inside you gopath folder you must create three folders bin,pkg,src

cd ~mkdir gopathcd gopathmkdir src bin pkg

since we have extracted our go and created our folders we have to edit our our .bashrc

export PATH=$PATH:/home/pi/go/bin
export GOROOT=/home/pi/go
export GOPATH=/home/pi/gopath

congratulation your go environment is waiting for you

the last step that we need to do before starting coding is to add GATT package write this command in your terminal

go get github.com/paypal/gatt

now we have to check if everything is alright for us, so close the current terminal and open new-one and write these commands :

cd /home/pi/gopath/src/github.com/paypal/gatt
go build examples/server.go
sudo ./server

you must see something like this:

now every is alright lets start coding

Central side (client) :

in this part of my post we will scan for a BLE IBeacon peripheral from our raspberry , so before staring let’s see what is IBeacon

IBeacon:

IBeacon has been created 5 years ago by Apple and it is very simple to implement, it defines a single message format with 31 bytes as shown in the figure bellow

each IBeacon packet consists of three important parts:

UUID: see my second blog in which I talk about the ble Architecture.

Major Number(16 bits) : the major number is used to identify major groups of beacons owned by one entity .

Minor Number: the minor number is used to identify the lowest level of the hierarchy within a group of beacon (one beacon of the same group).

now we are ready so lets start practicing .

let’s go inside our gopath/src and create a central folder, and inside the central folder we should create two file let’s call them central.go and iBeacon.go so the hierarchy of our gopath folder is shown in the figure below :

let’s start by the Central.go

to be able to target iBeacon packets we need to parse information that is why we have to create a structure and make a test for the length of the data because if the length is less then 25 b we are not dealing with iBeacon peripheral .

so let’s pass to iBeacon.go

now let’s build our project

go build
sudo ./central

the result :

peripheral side(server):

as explained in the previous blog the GATT server is built from services and each service is stack of characteristics, the figure bellow give an idea about the hierarchy of our folder and files

so we have to :

  1. initialize our service :

the code below has to be placed withing a file named test.go (you can choose whatever you want as name for the file) under the Service directory of the GATT examples “gopath/src/github.com/paypal/gatt/examples/service/

this code has one service and two characteristics the first characteristic is readable characteristic that we can read from it the value “123456” when we request reading from the characteristic we will receive “read request” as message on our terminal, and the second characteristic is writable characteristic , when we write a value in this characteristic we receive a message on the terminal (“write request”)

2. adding our service to our server :

For this purpose, we can start editing the server.go source file for the server example in the “/home/pi/gopath/src/github.com/paypal/gatt/examples” path. we only need to add three lines of code in the function definition for onStateChanged in between other service definitions. In the following content, note that the count service and battery service already exist. We only need to add our service:
// A simple count service for demo.

Additionally, in the same file, change the line where new services are advertised to the following code in order to advertise the new service as well

tasting our pizza:

to test our code I recommend you to use nrf connect it is an Android application made by Nordic Semiconductor engineer team and it works well you can download from Android play store

the both messages have been written on our terminal

the result of our read request (123456) is shown the screen shot of the nrf connect.

--

--