Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What causes the "NotImplementedError" in my Python code when I try to start advertising? #160

Open
Desertgeek opened this issue Apr 12, 2022 · 4 comments

Comments

@Desertgeek
Copy link

I am working with Adafruit_CircuitPython_BLE on a MacBook running Monterey 12.3. The code below is an example from the Adafruit library (ble_advertising_simpletest.py).

`from adafruit_ble import BLERadio

from adafruit_ble.advertising import Advertisement

ble = BLERadio()
advertisement = Advertisement()
advertisement.short_name = "HELLO"
advertisement.connectable = True

while True:
print(advertisement)
ble.start_advertising(advertisement, scan_response=b"")
while not ble.connected:
pass
print("connected")
while ble.connected:
pass
`
When I get to the "ble.start_advertising" line I get the "NotImplementedError".
The MacBook should support BLE since it communicates with my iPhone, right?

@dhalbert
Copy link
Collaborator

dhalbert commented Apr 12, 2022

The underlying implementation for adafruit-blinka-bleio on a host computer uses bleak, which does not support peripheral mode at all. Your MacBook could act as a peripheral, but not via bleak. And when it talks to your phone, it is acting as a BLE central, almost certainly.

You can do scanning on the MacBook, so you could have the phone advertise, and find it via scanning.

@Desertgeek
Copy link
Author

Thank you Dan for responding. It is very helpful. I'm actually trying to communicate between my laptop and an Ardunio WiFi Rev2, not my iPhone. The Arduino, fitted with the CNC V3 shield, controls 2 stepper motors forming AZ & EL axes on a (home made) tracking mount. A USB camera is fixed on the mount and is plugged in to my laptop. A Python program running on the laptop uses OpenCV routines to report the centroid of a target object in the image from the camera. I want to pass the X,Y coordinates of this centroid from the laptop to the Arduino an command the mount to move to keep the target centered in the image.
Your suggestion makes sense - switching the roles and having the Arduino scan for the laptop instead of the other way around. I will work on that.
Thanks.
PS: I get a "404" error when I click on your link above.

@dhalbert
Copy link
Collaborator

Yes, switching roles sounds good. I fixed the link, sorry.

@Desertgeek
Copy link
Author

I'm still missing something. Following the suggestion given above, I wrote the following code for the ArduinoWiFi as a peripheral (modified from one of the examples):

`#include <ArduinoBLE.h>

BLEService uartService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy UART Service

// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLECharacteristic xCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", (0), BLERead | BLENotify, 2);
BLECharacteristic yCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", (0), BLERead | BLENotify, 2);

void setup() {
Serial.begin(115200);
while (!Serial);

// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}

// set advertised local name and service UUID:
BLE.setLocalName("ArduinoWiFi");
BLE.setAdvertisedService(uartService);

// add the characteristic to the service
uartService.addCharacteristic(xCharacteristic);
uartService.addCharacteristic(yCharacteristic);

// add service
BLE.addService(uartService);

// set the initial value for the characeristic:
xCharacteristic.writeValue(0);
yCharacteristic.writeValue(0);

// start advertising
BLE.advertise();

Serial.println("BLE Peripheral");
}

void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:`

When I run this on the Arduino and fire up "LightBlue" on my macBook I indeed "see" the Arduino in the list of peripherals:

image

The python code below (also adapted from an example) is running on the macBook:

from future import print_function
from adafruit_ble import BLERadio # BluetoothLE
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService

ArduinoUUID = "A615EABE-E0DC-2996-57CD-84C3E542499F"
sender = BLERadio()
service = UARTService

def main():
"""
Main method of the program
"""
uart_connection = None
if sender.connected:
for connection in sender.connections:
if UARTService in connection:
uart_connection = connection
print("got connection")
break

# See if any existing connections are providing UARTService.
while True:
    if not uart_connection:
        print("Scanning...")
        for adv in sender.start_scan(ProvideServicesAdvertisement,
                                     timeout=15):
            print(adv.address)
            if UARTService in adv.services:
                print("found a UARTService advertisement")
                uart_connection = sender.connect(adv)
                break
        # Stop scanning whether or not we are connected.
        sender.stop_scan()

When I run this (with Arduino running) I get the "Scanning" message in the while loop and also the UUID of the Arduino that is reported in the "print(adv.address)" in the for loop continuously scrolling down the output window. But it never makes the uart_connection! It is as if it doesn't recognize the UARTService.

Interestingly, if I run LightBlue while these two programs are running, the serial monitor running in the Arduino IDE shows that the Arduino "sees" the laptop as a central and reports it.

image

This does not appear in the serial monitor output unless LightBlue is running.
PS: I'm having a difficult time formatting the text in this post to distinguish text from code. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants