Use the following CircuitPython code to make your board act as a peripheral device. The code will advertise the name of the device and services that are available on your board.
You may need to install the adafruit_ble library. The process of installing the BLE library is the same as the sensor libraries installed for Sensor Data Acquisition.
Copy the following code to the code.py in CIRCUITPY drive.
```python# Provide a remote sensing service over Bluetooth Low-Energy (BLE).# ----------------------------------------------------------------# Import the standard Python time functions.import timeimport digitalioimport board# Import the Adafruit Bluetooth library. Technical reference:# https://circuitpython.readthedocs.io/projects/ble/en/latest/api.htmlfrom adafruit_ble import BLERadiofrom adafruit_ble.advertising.standard import ProvideServicesAdvertisementfrom adafruit_ble.services.nordic import UARTService# ----------------------------------------------------------------# Initialize global variables for the main loop.ledpin= digitalio.DigitalInOut(board.BLUE_LED)ledpin.direction = digitalio.Direction.OUTPUTble =BLERadio()ble.name ="MyStudentID:XXXXX"uart =UARTService()advertisement =ProvideServicesAdvertisement(uart)# Flags for detecting state changes.advertised =Falseconnected =False# The sensor sampling rate is precisely regulated using the following timer variables.sampling_timer =0.0last_time = time.monotonic()sampling_interval =0.10# ----------------------------------------------------------------# Begin the main processing loop.whileTrue:# Read the accelerometer at regular intervals. Measure elapsed time and# wait until the update timer has elapsed. now = time.monotonic() interval = now - last_time last_time = now sampling_timer -= intervalif sampling_timer <0.0: sampling_timer += sampling_interval x, y, z = (10,20,30)else: x =Noneifnot advertised: ble.start_advertising(advertisement)print("Waiting for connection.") advertised =Trueifnot connected and ble.connected:print("Connection received.") connected =True ledpin.value =Trueif connected:ifnot ble.connected:print("Connection lost.") connected =False advertised =False ledpin.value =Falseelse:if x isnotNone: uart.write(b"%.3f,%.3f,%.3f\n"% (x, y, z))```
Start your Adafruit Feather nRF52840 board with code running from code.py. You must have the Bluetooth connectivity code running for this to work.
Next, start up the Bluefruit LE Connect app, and make sure it's in Central Mode (left button on the bottom). When you start the app, you should see the name of your device. If there's a long list of devices, you can shorten it by turning on the "Must have UART Service" switch.
To connect to the Adafruit Feather nRF52840, touch the Connect button. You should see "Connecting" and then "Discovering Services".
If you don't see your device right away, try pulling down to refresh. If that doesn't work, try turning Bluetooth off and back on on your phone or tablet and restarting the app.
Device Menu
After you connect, you'll see a menu of how you can interact with the device. Choose UART.
Data sent from the peripheral BLE device will appear in red.