It would be great if there was a tray containing Bluetooth related blocks.
In this tray, there should/could be a block per Bluetooth "service".
Dragging a Bluetooth service block into a PXT app would result in the corresponding Bluetooth being included in the generated code.
This issue requests a block for the micro:bit Bluetooth UART service.
Note that the UART service is a little more complicated than the other services which generally only require a single line of code for them to be included in a build. The UART service needs to be instantiated in the same way but also needs user code for either the production of data to transmit to a connected client and / or user code to receive and process data received from a connected client. At least one of these is required for use of this service to make sense.
It would generally make sense to implement producer/consumer code with reference to the state of the Bluetooth connection, for which events are available. i.e. there's no sense trying to transmit data over UART if you're not in a connection.
Note also that when creating an instance of the UART service, a buffer size must be supplied for the underlying serial data buffer.
The Lancaster University documentation covers this well: http://lancaster-university.github.io/microbit-docs/ble/uart-service/
I've repeated an example application here to show what the possible structure of an application which creates the UART service and then both receives data from it and transmits data using it might look like.
#include "MicroBit.h"
#include "MicroBitSamples.h"
#include "MicroBitUARTService.h"
MicroBit uBit;
MicroBitUARTService *uart;
int connected = 0;
void onConnected(MicroBitEvent e)
{
uBit.display.scroll("C");
connected = 1;
// my client will send ASCII strings terminated with the colon character
ManagedString eom(":");
while(1) {
ManagedString msg = uart->readUntil(eom);
uBit.display.scroll(msg);
}
}
void onDisconnected(MicroBitEvent e)
{
uBit.display.scroll("D");
connected = 0;
}
void onButtonA(MicroBitEvent e)
{
if (connected == 0) {
uBit.display.scroll("NC");
return;
}
uart->send("YES");
uBit.display.scroll("YES");
}
void onButtonB(MicroBitEvent e)
{
if (connected == 0) {
uBit.display.scroll("NC");
return;
}
uart->send("NO");
uBit.display.scroll("NO");
}
void onButtonAB(MicroBitEvent e)
{
if (connected == 0) {
uBit.display.scroll("NC");
return;
}
uart->send("GOT IT!!");
uBit.display.scroll("GOT IT!!");
}
int main()
{
// Initialise the micro:bit runtime.
uBit.init();
// listen for Bluetooth connection state changes
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);
// listen for user button interactions
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
uart = new MicroBitUARTService(*uBit.ble, 32, 32);
uBit.display.scroll("AVM");
release_fiber();
}
Note that inclusion of Bluetooth services has consequences for memory configuration on the micro:bit, specifically with respect to GATT table size. I've suggested an approach to addressing this to Joe Finney of Lancaster University and we're evaluating. Will update issue once this discussion has progressed further.
It would be great if there was a tray containing Bluetooth related blocks.
In this tray, there should/could be a block per Bluetooth "service".
Dragging a Bluetooth service block into a PXT app would result in the corresponding Bluetooth being included in the generated code.
This issue requests a block for the micro:bit Bluetooth UART service.
Note that the UART service is a little more complicated than the other services which generally only require a single line of code for them to be included in a build. The UART service needs to be instantiated in the same way but also needs user code for either the production of data to transmit to a connected client and / or user code to receive and process data received from a connected client. At least one of these is required for use of this service to make sense.
It would generally make sense to implement producer/consumer code with reference to the state of the Bluetooth connection, for which events are available. i.e. there's no sense trying to transmit data over UART if you're not in a connection.
Note also that when creating an instance of the UART service, a buffer size must be supplied for the underlying serial data buffer.
The Lancaster University documentation covers this well: http://lancaster-university.github.io/microbit-docs/ble/uart-service/
I've repeated an example application here to show what the possible structure of an application which creates the UART service and then both receives data from it and transmits data using it might look like.
Note that inclusion of Bluetooth services has consequences for memory configuration on the micro:bit, specifically with respect to GATT table size. I've suggested an approach to addressing this to Joe Finney of Lancaster University and we're evaluating. Will update issue once this discussion has progressed further.