void blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_evt_msg *event_data, void *param) { BLEPeripheral* p = (BLEPeripheral*)param; p->handleGattsEvent(event, event_data); }
void setup() { wait_for_serial(); // set the local name peripheral advertises blePeripheral.setLocalName("Initial"); // set the UUID for the service this peripheral advertises: blePeripheral.setAdvertisedServiceUuid(whistlepunkService.uuid()); // add service and characteristics blePeripheral.addAttribute(whistlepunkService); blePeripheral.addAttribute(valueCharacteristic); blePeripheral.addAttribute(configCharacteristic); blePeripheral.addAttribute(versionCharacteristic); versionCharacteristic.setValue(version); // assign event handlers for connected, disconnected to peripheral blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler); blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler); valueCharacteristic.setEventHandler(BLESubscribed, bleNotificationSubscribeHandler); valueCharacteristic.setEventHandler(BLEUnsubscribed, bleNotificationUnsubscribeHandler); configCharacteristic.setEventHandler(BLEWritten, bleConfigChangeHandler); ble_addr_t _local_bda; char _device_name[BLE_MAX_DEVICE_NAME+1]; ble_client_get_factory_config(&_local_bda, _device_name); sprintf(BleLongName, "Sci%02x%02x", _local_bda.addr[0], _local_bda.addr[1]); DEBUG_PRINT("Address is: "); DEBUG_PRINTLN(BleLongName); blePeripheral.setLocalName(BleLongName); // advertise the service blePeripheral.begin(); }
void loop() { // listen for BLE peripherals to connect: BLECentral central = blePeripheral.central(); // if a central is connected to peripheral: if (central) { Serial.print("Connected to central: "); // print the central's MAC address: Serial.println(central.address()); // turn on the LED to indicate the connection: digitalWrite(13, HIGH); // check the heart rate measurement every 200ms // as long as the central is still connected: while (central.connected()) { long currentMillis = millis(); // if 200ms have passed, check the heart rate measurement: if (currentMillis - previousMillis >= 200) { previousMillis = currentMillis; updateHeartRate(); } } // when the central disconnects, turn off the LED: digitalWrite(13, LOW); Serial.print("Disconnected from central: "); Serial.println(central.address()); } }
void loop() { if (Serial) { if (!serialConnected) { serialConnected = true; DEBUG_PRINT(F("LongName: ")); DEBUG_PRINTLN(BleLongName); } } else { if (serialConnected) serialConnected = false; } // poll peripheral blePeripheral.poll(); if (valueCharacteristic.subscribed()) { int sensorValue = 0; if (pin_type == ANALOG) { sensorValue = analogRead(pin); } else if (pin_type == DIGITAL) { sensorValue = digitalRead(pin); } else { sensorValue = 666; } send_data(valueCharacteristic, millis(), sensorValue); } #ifdef GOOSCI_DEVELOPER_MODE heartbeat(); #endif }
void setup() { Serial.begin(9600); // initialize serial communication pinMode(13, OUTPUT); // initialize the LED on pin 13 to indicate when a central is connected /* Set a local name for the BLE device This name will appear in advertising packets and can be used by remote devices to identify this BLE device The name can be changed but maybe be truncated based on space left in advertisement packet */ blePeripheral.setLocalName("HeartRateSketch"); blePeripheral.setAdvertisedServiceUuid(heartRateService.uuid()); // add the service UUID blePeripheral.addAttribute(heartRateService); // Add the BLE Heart Rate service blePeripheral.addAttribute(heartRateChar); // add the Heart Rate Measurement characteristic /* Now activate the BLE device. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices until it receives a new connection */ blePeripheral.begin(); Serial.println("Bluetooth device active, waiting for connections..."); }