7 years, 5 months ago.

How to make it auto connected?

I write a simple code to turn on/off a rgb led. All work fine, but I want a direct connection and automatic but I don't know how to do. Someone can help me?

  1. include "mbed.h"
  2. include "ble/BLE.h"

PwmOut r(p1); PwmOut g(p25); PwmOut b(p24); uint16_t rgbUUID = 0xA000; uint16_t UUID = 0xA001;

const static char DEVICE_NAME[] = "TEST";

static uint8_t Value[10] = {0}; WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(Value)> Char(UUID, Value);

BLEDevice ble; GattCharacteristic *characteristics[] = {&Char}; GattService rgbService(rgbUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { ble.gap().startAdvertising(); r.write(0.0); g.write(0.0); b.write(0.0); }

void writeCharCallback(const GattWriteCallbackParams *params){ if(params->handle == Char.getValueHandle()) {

r = 1.0; g = 1.0; b = 1.0;

} }

int main(){

ble.init(); ble.gap().onDisconnection(disconnectionCallback); ble.gattServer().onDataWritten(writeCharCallback);

/* Setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); BLE only, no classic BT ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); advertising type ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingInterval(7500);

/* Add our custom service */ ble.addService(rgbService);

/* Start advertising */ ble.gap().startAdvertising();

BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); ble.init(bleInitComplete);

/* SpinWait for initialization to complete. This is necessary because the

  • BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ }

/* Infinite loop waiting for BLE interrupt events */ while (true) { ble.waitForEvent(); } }

Can you edit the post to use

<<code>>
your code here
<</code>>

so that the formatting is preserved and people can tell which bits are comments.

posted by Andy A 10 Jul 2017
Be the first to answer this question.