Little domotic project: Controlling RGB color's intensity of the JUMA card with an Android App. Here's the corresponding App's source code --> https://github.com/NordineKhelfi/Android_Bluetooth_Low_Energy

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
Super_Mario
Date:
Sun Apr 23 20:48:42 2017 +0000
Revision:
23:d63b6764b95d
Parent:
22:406127954d1f
Little domotic project: controlling RGB color intensity of the JUMA SMP Tag from an Android App'; ; Here's the matching App's source code --> https://github.com/NordineKhelfi/Android_Bluetooth_Low_Energy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 0:cd5b6733aeb1 1 #include "mbed.h"
andresag 19:477567297aac 2 #include "ble/BLE.h"
mbedAustin 1:94152e7d8b5c 3
andresag 19:477567297aac 4 DigitalOut led(LED1, 1);
Super_Mario 23:d63b6764b95d 5 Serial pc(P0_0, P0_1);
Super_Mario 23:d63b6764b95d 6
Super_Mario 23:d63b6764b95d 7 DigitalOut dSwitch(P0_4);
Super_Mario 23:d63b6764b95d 8
Super_Mario 23:d63b6764b95d 9 PwmOut ledB(P0_5);
Super_Mario 23:d63b6764b95d 10 PwmOut ledG(P0_6);
Super_Mario 23:d63b6764b95d 11 PwmOut ledR(P0_7);
Super_Mario 23:d63b6764b95d 12
Super_Mario 23:d63b6764b95d 13 uint16_t customServiceUUID = 0x1812;
Super_Mario 23:d63b6764b95d 14
mbedAustin 13:62b1d32745ac 15 uint16_t readCharUUID = 0xA001;
mbedAustin 9:b33f42191584 16 uint16_t writeCharUUID = 0xA002;
Super_Mario 23:d63b6764b95d 17 uint16_t writeRedCharUUID = 0xA003;
Super_Mario 23:d63b6764b95d 18 uint16_t writeGreenCharUUID = 0xA004;
mbedAustin 1:94152e7d8b5c 19
Super_Mario 23:d63b6764b95d 20
Super_Mario 23:d63b6764b95d 21 const static char DEVICE_NAME[] = "GATT Example"; // change this
mbedAustin 8:60ede963dfe2 22 static const uint16_t uuid16_list[] = {0xFFFF}; //Custom UUID, FFFF is reserved for development
mbedAustin 1:94152e7d8b5c 23
Super_Mario 23:d63b6764b95d 24
Super_Mario 23:d63b6764b95d 25
Super_Mario 23:d63b6764b95d 26 /* Set Up custom Characteristics -----------------------------------------------------------------------------------------------------*/
mbedAustin 9:b33f42191584 27 static uint8_t readValue[10] = {0};
mbedAustin 12:6d1f77d0cb37 28 ReadOnlyArrayGattCharacteristic<uint8_t, sizeof(readValue)> readChar(readCharUUID, readValue);
mbedAustin 2:e84c13abc479 29
mbedAustin 9:b33f42191584 30 static uint8_t writeValue[10] = {0};
mbedAustin 12:6d1f77d0cb37 31 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeValue)> writeChar(writeCharUUID, writeValue);
mbedAustin 2:e84c13abc479 32
Super_Mario 23:d63b6764b95d 33 //On construit un "WriteOnlyArrayGattCharacteristic" --> writeRedChar
Super_Mario 23:d63b6764b95d 34 static uint8_t writeRedValue[10] = {0};
Super_Mario 23:d63b6764b95d 35 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeRedValue)> writeRedChar(writeRedCharUUID, writeRedValue);
Super_Mario 23:d63b6764b95d 36
Super_Mario 23:d63b6764b95d 37 static uint8_t writeGreenValue[10] = {0};
Super_Mario 23:d63b6764b95d 38 WriteOnlyArrayGattCharacteristic<uint8_t, sizeof(writeGreenValue)> writeGreenChar(writeGreenCharUUID, writeGreenValue);
Super_Mario 23:d63b6764b95d 39
Super_Mario 23:d63b6764b95d 40
Super_Mario 23:d63b6764b95d 41 /* ---------------------------------------------------------------------------------------------------------------------*/
Super_Mario 23:d63b6764b95d 42
Super_Mario 23:d63b6764b95d 43
andresag 22:406127954d1f 44 /* Set up custom service */
Super_Mario 23:d63b6764b95d 45 GattCharacteristic *characteristics[] = {&readChar, &writeChar, &writeRedChar, &writeGreenChar};
mbedAustin 9:b33f42191584 46 GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
mbedAustin 2:e84c13abc479 47
mbedAustin 2:e84c13abc479 48
mbedAustin 8:60ede963dfe2 49 /*
mbedAustin 8:60ede963dfe2 50 * Restart advertising when phone app disconnects
andresag 19:477567297aac 51 */
andresag 19:477567297aac 52 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
mbedAustin 1:94152e7d8b5c 53 {
andresag 22:406127954d1f 54 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
mbedAustin 1:94152e7d8b5c 55 }
mbedAustin 0:cd5b6733aeb1 56
andresag 19:477567297aac 57 /*
andresag 22:406127954d1f 58 * Handle writes to writeCharacteristic
mbedAustin 8:60ede963dfe2 59 */
melmon 18:7d89c4fba362 60 void writeCharCallback(const GattWriteCallbackParams *params)
mbedAustin 3:0fb60f81f693 61 {
andresag 22:406127954d1f 62 /* Check to see what characteristic was written, by handle */
melmon 18:7d89c4fba362 63 if(params->handle == writeChar.getValueHandle()) {
Super_Mario 23:d63b6764b95d 64
Super_Mario 23:d63b6764b95d 65 float fPwm = (float) ( 100 - params->data[0] ) / 100.0;
Super_Mario 23:d63b6764b95d 66 pc.printf("fPwm --> %f\n", fPwm);
Super_Mario 23:d63b6764b95d 67 ledB.write(fPwm);
Super_Mario 23:d63b6764b95d 68
andresag 22:406127954d1f 69 /* Update the readChar with the value of writeChar */
andresag 22:406127954d1f 70 BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), params->data, params->len);
mbedAustin 8:60ede963dfe2 71 }
Super_Mario 23:d63b6764b95d 72 /* Check to see what characteristic was written, by handle */
Super_Mario 23:d63b6764b95d 73 else if(params->handle == writeRedChar.getValueHandle()) {
Super_Mario 23:d63b6764b95d 74
Super_Mario 23:d63b6764b95d 75 float fPwm = (float) ( 100 - params->data[0] ) / 100.0;
Super_Mario 23:d63b6764b95d 76 pc.printf("fPwm --> %f\n", fPwm);
Super_Mario 23:d63b6764b95d 77 ledR.write(fPwm);
Super_Mario 23:d63b6764b95d 78
Super_Mario 23:d63b6764b95d 79 if(fPwm >= 0.50)
Super_Mario 23:d63b6764b95d 80 dSwitch = true;
Super_Mario 23:d63b6764b95d 81 else
Super_Mario 23:d63b6764b95d 82 dSwitch = false;
Super_Mario 23:d63b6764b95d 83 }
Super_Mario 23:d63b6764b95d 84 /* Check to see what characteristic was written, by handle */
Super_Mario 23:d63b6764b95d 85 else if(params->handle == writeGreenChar.getValueHandle()) {
Super_Mario 23:d63b6764b95d 86
Super_Mario 23:d63b6764b95d 87 float fPwm = (float) ( 100 - params->data[0] ) / 100.0;
Super_Mario 23:d63b6764b95d 88 pc.printf("fPwm --> %f\n", fPwm);
Super_Mario 23:d63b6764b95d 89 ledG.write(fPwm);
Super_Mario 23:d63b6764b95d 90 }
mbedAustin 3:0fb60f81f693 91 }
andresag 22:406127954d1f 92 /*
andresag 22:406127954d1f 93 * Initialization callback
andresag 22:406127954d1f 94 */
andresag 22:406127954d1f 95 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 22:406127954d1f 96 {
andresag 22:406127954d1f 97 BLE &ble = params->ble;
andresag 22:406127954d1f 98 ble_error_t error = params->error;
Super_Mario 23:d63b6764b95d 99
andresag 22:406127954d1f 100 if (error != BLE_ERROR_NONE) {
andresag 22:406127954d1f 101 return;
andresag 22:406127954d1f 102 }
mbedAustin 0:cd5b6733aeb1 103
andresag 19:477567297aac 104 ble.gap().onDisconnection(disconnectionCallback);
andresag 19:477567297aac 105 ble.gattServer().onDataWritten(writeCharCallback);
mbedAustin 2:e84c13abc479 106
andresag 22:406127954d1f 107 /* Setup advertising */
andresag 19:477567297aac 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
andresag 19:477567297aac 109 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
andresag 19:477567297aac 110 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
andresag 19:477567297aac 111 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
andresag 19:477567297aac 112 ble.gap().setAdvertisingInterval(100); // 100ms.
mbedAustin 2:e84c13abc479 113
andresag 22:406127954d1f 114 /* Add our custom service */
mbedAustin 13:62b1d32745ac 115 ble.addService(customService);
mbedAustin 2:e84c13abc479 116
andresag 22:406127954d1f 117 /* Start advertising */
andresag 19:477567297aac 118 ble.gap().startAdvertising();
andresag 22:406127954d1f 119 }
andresag 19:477567297aac 120
andresag 22:406127954d1f 121 /*
andresag 22:406127954d1f 122 * Main loop
andresag 22:406127954d1f 123 */
andresag 22:406127954d1f 124 int main(void)
andresag 22:406127954d1f 125 {
andresag 22:406127954d1f 126 /* initialize stuff */
Super_Mario 23:d63b6764b95d 127 pc.printf("\n\r********* Starting Main Loop *********\n\r");
Super_Mario 23:d63b6764b95d 128
Super_Mario 23:d63b6764b95d 129 ledB = 1;
Super_Mario 23:d63b6764b95d 130 ledG = 1;
Super_Mario 23:d63b6764b95d 131 ledR = 1;
andresag 22:406127954d1f 132
Super_Mario 23:d63b6764b95d 133 dSwitch = false;
Super_Mario 23:d63b6764b95d 134
andresag 22:406127954d1f 135 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
andresag 22:406127954d1f 136 ble.init(bleInitComplete);
Super_Mario 23:d63b6764b95d 137
andresag 22:406127954d1f 138 /* SpinWait for initialization to complete. This is necessary because the
andresag 22:406127954d1f 139 * BLE object is used in the main loop below. */
Super_Mario 23:d63b6764b95d 140 while (ble.hasInitialized() == false) {
Super_Mario 23:d63b6764b95d 141 /* spin loop */
Super_Mario 23:d63b6764b95d 142 }
andresag 22:406127954d1f 143
andresag 22:406127954d1f 144 /* Infinite loop waiting for BLE interrupt events */
mbedAustin 2:e84c13abc479 145 while (true) {
andresag 22:406127954d1f 146 ble.waitForEvent(); /* Save power */
mbedAustin 2:e84c13abc479 147 }
andresag 20:fcc752d401ec 148 }