[nRF52840] UART service advertising error.

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
Sunghwan_Chung
Posts: 5
Joined: Sat Feb 10, 2018 1:27 pm
Location: South Korea

[nRF52840] UART service advertising error.

Post by Sunghwan_Chung » Tue Mar 12, 2019 1:26 pm

Hello!

I try to connect through UART(Serial) with nRFConnect App.
Error found at the advertise function.

=====================================
>>>
>>> import spp
>>> sc = spp.SPP()
SoftDevice enabled
1. setName(name), default is nrf52_SPP .
2. start() : start service and advertise
>>> sc.start()
Traceback (most recent call last):
File "<stdin>", in <module>
File "spp.py", in start
OSError: Can not apply advertisment data. status: 0x09
>>>
=====================================

When use advertise() without params. It return no error and get any error during connect with iPhone App.
But, disconnected and connect continuously and unable transfer data. Code is below.


from board import LED
from ubluepy import Service, Characteristic, UUID, Peripheral, constants
import ble

class SPP:
deviceName = "nrf52_SPP"
conFlag = False
# start off with LED(1) off
led = LED(1)

# the Nordic UART Service.
uuid_spp_vender_specific = UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
uuid_spp_rx_characteristic = UUID("6E400002-B5A3-F393-E0A9-E50E24DCCA9E")
uuid_spp_tx_characteristic = UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E")

periph = Peripheral()
service_SPP = Service(uuid_spp_vender_specific)

SPP_rx_pros = Characteristic.PROP_WRITE
SPP_rx_attrs = Characteristic.ATTR_CCCD
SPP_rx_characteristic = Characteristic(uuid_spp_rx_characteristic, props = SPP_rx_pros, attrs = SPP_rx_attrs)
service_SPP.addCharacteristic(SPP_rx_characteristic)

SPP_tx_pros = Characteristic.PROP_NOTIFY
SPP_tx_attrs = Characteristic.ATTR_CCCD
SPP_tx_characteristic = Characteristic(uuid_spp_tx_characteristic, props = SPP_tx_pros, attrs = SPP_tx_attrs)
service_SPP.addCharacteristic(SPP_tx_characteristic)

receiveddata = None

def __init__(self):
ble.enable()
self.led.off()
self.conFlag = False
print('1. setName(name), default is ', self.deviceName, '.')
print('2. start() : start service and advertise')
return

def close(self):
pass

def start(self):
self.periph.addService(self.service_SPP)
self.periph.setConnectionHandler(self.event_handler)
self.periph.advertise(device_name=self.deviceName, services=[self.service_SPP])
return

def setName(self, data):
if len(data) > 0:
self.deviceName = data
return

def sendStr(self,strdata):
self.SPP_rx_characteristic.write(strdata.encode('utf-8'))
return

def getData(self):
if self.receiveddata != None:
strReceived = self.receiveddata.decode('utf-8')
print('Rcv:', strReceived)
return strReceived
return None

def event_handler(self, id, handle, data):
if id == constants.EVT_GAP_CONNECTED:
# indicated 'connected'
print('\nEVT_GAP_CONNECTED')
self.conFlag = True
self.led.on()
self.sendStr('hello.')

elif id == constants.EVT_GAP_DISCONNECTED:
# indicate 'disconnected'
print('\nEVT_GAP_DISCONNECTED')
self.conFlag = False
self.led.off()
# restart advertisment automatically
self.periph.advertise(device_name=self.deviceName, services=[self.service_SPP])

elif id == constants.EVT_GATTS_WRITE:
# write to this Characteristic is to CCCD
print('\nEVT_GATTS_WRITE:', data.decode('utf-8'))
self.receiveddata.append(data)

elif id == constants.EVT_GATTS_READ:
print('\nEVT_GATTS_READ:', data.decode('utf-8'))

else:
print('\nNot defined id :', id, '\ndata:', data)

Post Reply