BLE Peripheral App¶
Overview¶
Now that we’ve gone through how BLE Apps are contructed, how they function, and how all the parts fit together let’s try out a BLE Peripheral App to see how it all works.
Prerequisites¶
You should have a BLE Central App of some sort to connect with. On Mac OS or iOS, you can use LightBlue which is a free app to browse and connect to BLE Peripheral devices.
Create a New Target¶
You can create a new project instead, but this tutorial will simply use the previously created btshell project and add a new target for the BLE Peripheral
$ newt target create myperiph
Target targets/myperiph successfully created
$ newt target set myperiph bsp=@apache-mynewt-core/hw/bsp/nordic_pca10040
Target targets/myperiph successfully set target.bsp to @apache-mynewt-core/hw/bsp/nordic_pca10040
$ newt target set myperiph app=@apache-mynewt-nimble/apps/bleprph
Target targets/myperiph successfully set target.app to @apache-mynewt-nimble/apps/bleprph
$ newt target set myperiph build_profile=optimized
Target targets/myperiph successfully set target.build_profile to optimized
$ newt build myperiph
Building target targets/myperiph
...
Linking ~/dev/nrf52dk/bin/targets/myperiph/app/apps/bleprph/bleprph.elf
Target successfully built: targets/myperiph
$ newt create-image myperiph 1.0.0
App image succesfully generated: ~/dev/nrf52dk/bin/targets/myperiph/app/apps/bleprph/bleprph.img
$ newt load myperiph
Loading app image into slot 1
Now if you reset the board, and fire up your BLE Central App, you should see a new peripheral device called ‘nimble-bleprph’.
![LightBlue iOS App with nimble-bleprph device](../../../../_images/LightBlue-1.jpg)
Now that you can see the device, you can begin to interact with the advertised service.
Click on the device and you’ll establish a connection.
![LightBlue iOS App connected to the nimble-bleprph device](../../../../_images/LightBlue-2.jpg)
Now that you’re connected, you can see the Services that are being advertised.
Scroll to the bottom and you will see a Read Characteristic, and a Read/Write Characteristic.
![LightBlue iOS App connected to the nimble-bleprph device](../../../../_images/LightBlue-3.jpg)
Just click on the Read Write Characteristic and you will see the existing value.
![LightBlue iOS App with nimble-bleprph device Characteristic](../../../../_images/LightBlue-4.jpg)
Type in a new value.
![LightBlue iOS App Value Change](../../../../_images/LightBlue-5.jpg)
LightBlue¶
And you will see the new value reflected.
![LightBlue iOS App with nimble-bleprph new value](../../../../_images/LightBlue-6.jpg)
If you still have your console connected, you will be able to see the connection requests, and pairing, happen on the device as well.
258894:[ts=2022609336ssb, mod=64 level=1] connection established; status=0 handle=1 our_ota_addr_type=0 our_ota_addr=0a:0a:0a:0a:0a:0a our_id_addr_type=0 our_id_addr=0a:0a:0a:0a:0a:0a peer_ota_addr_type=1 peer_ota_addr=7f:be:d4:44:c0:d4 peer_id_addr_type=1 peer_id_addr=7f:be:d4:44:c0:d4 conn_itvl=24 conn_latency=0 supervision_timeout=72 encrypted=0 authenticated=0 bonded=0
258904:[ts=2022687456ssb, mod=64 level=1]
258917:[ts=2022789012ssb, mod=64 level=1] mtu update event; conn_handle=1 cid=4 mtu=185
258925:[ts=2022851508ssb, mod=64 level=1] subscribe event; conn_handle=1 attr_handle=14 reason=1 prevn=0 curn=0 previ=0 curi=1
261486:[ts=2042859320ssb, mod=64 level=1] encryption change event; status=0 handle=1 our_ota_addr_type=0 our_ota_addr=0a:0a:0a:0a:0a:0a our_id_addr_type=0 our_id_addr=0a:0a:0a:0a:0a:0a peer_ota_addr_type=1 peer_ota_addr=7f:be:d4:44:c0:d4 peer_id_addr_type=1 peer_id_addr=7f:be:d4:44:c0:d4 conn_itvl=24 conn_latency=0 supervision_timeout=72 encrypted=1 authenticated=0 bonded=1
261496:[ts=2042937440ssb, mod=64 level=1]
Congratulations! You’ve just built and connected your first BLE Peripheral device!