-1

I want to send messages from my Micro:Bit to a linked device over bluetooth. I have the following code for Micro:Bit:

#include "MicroBit.h"
#include "MicroBitUARTService.h"

MicroBitUARTService *uart;
MicroBit uBit;

uint8_t connected = 0;

void onConnect(MicroBitEvent)
{
    connected = 1;
    uBit.display.print("C");
}

void onDisconnect(MicroBitEvent)
{
    connected = 0;
    uBit.display.print("D");
}

void onButtonA(MicroBitEvent e)
{
    if (connected == 0) {
        uBit.display.print("X");
        return;
    }
    uart->send("Button A");
    uBit.display.print("A");
}

void onButtonB(MicroBitEvent e)
{
    if (connected == 0) {
        uBit.display.print("X");
        return;
    }
    uart->send("Button B");
    uBit.display.print("B");
}

int main()
{
    // Initialise the micro:bit runtime.
    uBit.init();

    uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnect);
    uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnect);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
    uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);

    uart = new MicroBitUARTService(*uBit.ble, 32, 32);
    uBit.display.print("S");

    release_fiber();
}

I'm able to pair it with my macbook using the following tool:

enter image description here

Once paired, I don't know how to read the messages sent over uart bluetooth.

Lechucico
  • 1,404
  • 4
  • 19
  • 43

1 Answers1

0

Don't let the term UART in the characteristic name confuse you, it's just a standard characteristic and has nothing to do with actual UART.

according to the documentation, indications are used with the UART TX characteristic so look at how to use Indications from your API .

https://lancaster-university.github.io/microbit-docs/resources/bluetooth/bluetooth_profile.html

And

https://lancaster-university.github.io/microbit-docs/ble/uart-service/#example-microbit-application-animal-vegetable-mineral-game for an Android example.

Martin

More....

Per the profile documentation for which I gave the link above, you can write to the RX characteristic but must subscribe to Indications to the TX characteristic. You cannot read it directly.

On a Raspberry Pi I would use the Noble node.hs module:

https://github.com/sandeepmistry/noble

For indications use

characteristic.subscribe([callback(error)]);

and

characteristic.on('data', callback(data, isNotification));

For writing use

characteristic.write(data, withoutResponse[, callback(error)]); // data is a buffer, withoutResponse is true|false

I know you are not interested in phones but the principle is exactly the same, whichever platform you are coding for and whichever API you use. You just need to know what operations each characteristic supports and then use your API accordingly.

  • But I can’t find how to read these messages on a raspberry pi, for example. I’m not interested on reading messages on phone. – Lechucico Feb 07 '18 at 09:07
  • I see that there's a characteristic for TX UART and RX UART with a UUID. But how I can use that? – Lechucico Feb 07 '18 at 10:32