Here is the full code listing:
<
#include <ArduinoBLE.h>
// create a service to expose our service to BLE Central Devices
BLEService garageDoorService("fa01");
BLEUnsignedCharCharacteristic buttonCharacteristic("2102", BLERead | BLEWrite | BLENotify);
byte buttonValue = 0x00;
int commandIterations = 0;
#define BUTTONSWITCHPIN 10
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(BUTTONSWITCHPIN, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
if (!BLE.begin())
{
Serial.println("starting BLE failed!");
while (1);
}
String address = BLE.address();
Serial.println("Our address is [" + address + "]");
BLE.setDeviceName("IBM Garage Opener"); // this sets Characteristic 0x2a00 of Service 0x1800
// Service 0x1800 is the Generic Access Profile
// Characteristic 0x2a00 is the Device Name
// Characteristic 0x2a01 is the "Appearance"
BLE.setAppearance(384); // BLE_APPEARANCE_GENERIC_REMOTE_CONTROL
BLE.setLocalName("BLE Garage Opener"); // this sets the local name for the advertising data
// tell the world about us
BLE.setAdvertisedService(garageDoorService);
garageDoorService.addCharacteristic(buttonCharacteristic);
BLE.addService(garageDoorService);
buttonCharacteristic.writeValue(buttonValue); // start with a zero
// advertise to the world so we can be found
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
// register new connection handler
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
// registeer disconnect handler
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// handle Characteristic Written Handler
buttonCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
}
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, see if care, etc.
Serial.print("Characteristic event, written: ");
Serial.println(characteristic.uuid());
// see if this is the Button Characteristic
if (characteristic.uuid() == "2102")
{
characteristic.readValue(buttonValue);
Serial.print("new value ");
Serial.println(buttonValue);
if (buttonValue == 1)
{
digitalWrite(BUTTONSWITCHPIN, HIGH);
delay(500);
commandIterations++;
buttonCharacteristic.writeValue(1); // <<<<<<< here is what i think should be sending the value 1 to the mobile??????
}
digitalWrite(BUTTONSWITCHPIN, LOW);
}
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, HIGH); // indicate that we have a connection
digitalWrite(BUTTONSWITCHPIN, LOW); // make sure our button is NOT pressed
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
digitalWrite(LED_BUILTIN, LOW); // indicate that we no longer have a connection
digitalWrite(BUTTONSWITCHPIN, LOW); // make sure our button is NOT pressed
}
void loop()
{
BLEDevice central = BLE.central();
if (central)
{
while (central.connected()) {
if (buttonCharacteristic.written()) {
Serial.println("changed");
}
if (scaledValueChar.valueUpdated())
{
Serial.println("changed");
}
delay(200);
}
}
}
/>