Bluetooth simple connection & parsing of results

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
jimmo
Posts: 2262
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Bluetooth simple connection & parsing of results

Post by jimmo » Thu Feb 11, 2021 5:12 am

mflmartin wrote:
Tue Feb 09, 2021 10:50 am
Hummm: I get Status: 258, but the READ_RESULT never fires.
FWIW I see status=258 if i try to issue a read to a characteristic that doesn't support reading (i.e. doesn't set the read flag).

Which would make sense, 258 in NimBLE that would be BLE_HS_ATT_ERR(BLE_ATT_ERR_READ_NOT_PERMITTED) (i.e. 0x100 + 0x02).

You might also see this if the remote device requires pairing before reading.

mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: Bluetooth simple connection & parsing of results

Post by mflmartin » Thu Feb 11, 2021 10:30 pm

Ahhhhhhhhmigo. I understand. Makes sense. I will do further testing! Thanks!

mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: Bluetooth simple connection & parsing of results

Post by mflmartin » Thu Mar 04, 2021 7:14 pm

The device did require pairing, actually, with SRP protocol. So I would need to send data first.

What I don't understand fully is the gattc_write, in order to send data to the server. Where do I get the conn_handle and value_handle from?

Thanks,


ble.gattc_write(conn_handle, value_handle, data, mode=1)


What I want is to send an specific HEX string, which I suposse I have to encode to bytes, and read the response. Do I have to call an specific read function or if I have:

ble.gattc_write(self._conn_handle, value_handle, data, mode=1)

# Explicitly issue reads, using "print" as the callback.
while central.is_connected():
central.read(callback=print)
time.sleep_ms(3000)

I can get the results in the IRQ events function?

Thanks,

mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: Bluetooth simple connection & parsing of results

Post by mflmartin » Thu Mar 04, 2021 9:29 pm

I guess that I have to use the write_handle that was calculated previously for the write characteristic. But for the value_handle? Where do I get that? Is there any place with extended documentation on what does what in order to find more info about this?

self._ble.gattc_write(self._write_handle, self._value_handle, data, 1)


Also, if the data that I want to send is an HEX string:

data = '7e00822c012ca118d436fc5fb42bf18a67d8041001710ecf8bb898ab462b0ffcd753f8702d22873ff88e82599b6bb0d29d53615596847eaeb6beda6a00f557afa65e120d3d44249fd9920f1c4d762283c4a92d981d9d7c85fc3b8a397af938e1a3f0697972ff6db62527d5e003500a6225a6c0a72451b40a0a4ea6360599ea82a91bdff045b6'

What is the best way to prepare it for the self._ble.gattc_write function? Using unhexlify?
data = binascii.unhexlify(data)

Thanks,

mflmartin
Posts: 43
Joined: Sat Jul 23, 2016 7:30 pm

Re: Bluetooth simple connection & parsing of results

Post by mflmartin » Thu Mar 04, 2021 11:31 pm

UPDATE:

I guessed, from another example, that I had to use the write_handle that was calculated previously for the write characteristic. And for the value handle, I used the value of the handle for the write_service.

self._ble.gattc_write(self.conn_handle, self.write_handle, data, 1)


And now, I am getting:

_IRQ_GATTC_WRITE_DONE-> 0 13

from this:

Code: Select all

		elif event == _IRQ_GATTC_WRITE_DONE:
			# Write completed (no-op).
			conn_handle, value_handle, status = data
			print('_IRQ_GATTC_WRITE_DONE-> ', status, value_handle)
But I expected a response from the server, with data. How can I get the data that I receive back? With the value handle? I am expecting a hex string of data. How can I extract it now?

----

Also, if the data that I want to send is an HEX string:

data = '7e00822c012ca118d436fc5fb42bf18a67d8041001710ecf8bb898ab462b0ffcd753f8702d22873ff88e82599b6bb0d29d53615596847eaeb6beda6a00f557afa65e120d3d44249fd9920f1c4d762283c4a92d981d9d7c85fc3b8a397af938e1a3f0697972ff6db62527d5e003500a6225a6c0a72451b40a0a4ea6360599ea82a91bdff045b6'

What is the best way to prepare it for the self._ble.gattc_write function? Using unhexlify?
data = binascii.unhexlify(data)

I tried to send it like that, directly, and with conversion and I also got status:0, value_handle:13.
Where can I get a list of all the status and their meanings, @jimmo? I am a bit lost, to be honest.

Thanks!

Post Reply