0

A micro-service software is running in my raspberry pi. The software receives raw data from various paired and connected BLE sensors, analyzes it and sends the result to cloud server. I want the analyzed data to be sent to the nearby Anroid phone too over the BLE. For this, I want the Raspberry pi to act as GATT server and Android phone as GATT client, wherein the GATT server will advertise using its custom service_uuid and characteristics_uuid. The GATT client (Android phone) scans for the advertised packet and connects to the GATT server, later reads the data that is sent from the GATT server (Raspberry pi). For this, I've written the following script:

from bluepy.btle import Peripheral, UUID, DefaultDelegate
from bluepy import btle
import struct
import time
import uuid

# Define the UUID of the service and the characteristic
SERVICE_UUID = uuid.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
CHARACTERISTIC_UUID = uuid.UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")

# Define the advertisement data
AD_TYPE_16_BIT_UUID = 0x03
AD_TYPE_SERVICE_DATA = 0x16
ADVERTISING_DATA = struct.pack("<BBHHB", AD_TYPE_16_BIT_UUID, 2, 0xAAAA, 0xAAAA, AD_TYPE_SERVICE_DATA) + bytes.fromhex("")

# Define the callback function for handling GATT requests
class MyDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print("Received data:", data)

# Define the function to write data to the characteristic
def write_data(char, data):
    char.write(bytes(data))
    print("Wrote data:", data)

# Start the BLE advertisement
try:
    peripheral = Peripheral()
    peripheral.setAdvertisingData(ADVERTISING_DATA)
    peripheral.setAdvertisingInterval(1000)
    peripheral.startAdvertising()
except:
    pass

# Define the characteristic class
class MyCharacteristic(btle.Characteristic):
    def __init__(self, service):
        btle.Characteristic.__init__(
                self, CHARACTERISTIC_UUID, ["read", "write"], service)
        self.data = []

    def read(self):
        return bytes(self.data)

    def write(self, value):
        self.data = value
        btle.handle_request(CHARACTERISTIC_UUID, value)


class MyService(btle.Service):
    def __init__(self, peripheral, uuid):
        btle.Service.__init__(self, peripheral, uuid, 0x01, 0x0f)
        self.addCharacteristic(MyCharacteristic(self)) ## <--ISSUE HERE


# Create the service and set up the peripheral
peripheral = Peripheral()
service = MyService(peripheral, SERVICE_UUID)
peripheral.setMTU(512)
peripheral.setServices(service)

# Start the main event loop
try:
    while True:
        peripheral.waitForNotifications(1.0)
except KeyboardInterrupt:
    peripheral.disconnect()

# Write data to the characteristic
char = peripheral.getCharacteristics(uuid=CHARACTERISTIC_UUID)[0]
write_data(char, "Hello, Android!")

This code gives me the following error:

self.addCharacteristic(MyCharacteristic(self))
AttributeError: 'MyService' object has no attribute 'addCharacteristic'

I get the similar error if I use:

 self.add_Characteristic(MyCharacteristic(self))

How can I solve it?

2

0