Skip to content

BLE Central review (Arduino style) #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions libraries/BLE/examples/central/central.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

#include "ArduinoBLE.h"
#include "BLEAttribute.h"
#include "BLECharacteristicImp.h"
#include "BLEProfileManager.h"

// LED pin
#define LED_PIN 13

void setup() {
Serial1.begin(115200);
Serial1.println("test---");

// set LED pin to output mode
pinMode(LED_PIN, OUTPUT);

// begin initialization
BLE.begin();
Serial1.println(BLE.address());

BLE.startScanning("LED");
}

void controlLed(BLEDevice &peripheral)
{
static bool discovered = false;
// connect to the peripheral
Serial1.print("Connecting ... ");
Serial1.println(peripheral.address());

if (peripheral.connect())
{
Serial1.print("Connected: ");
Serial1.println(peripheral.address());
}
else
{
Serial1.println("Failed to connect!");
return;
}

peripheral.discoverAttributes();

BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10101-e8f2-537e-4f6c-d104768a1214");

if (!ledCharacteristic)
{
//peripheral.disconnect();
while(1)
{
Serial1.println("Peripheral does not have LED characteristic!");
delay(5000);
}
return;
}


unsigned char ledstate = 0;

discovered = false;
while (peripheral.connected())
{
if (ledstate == 1)
{
ledstate = 0;
}
else
{
ledstate = 1;
}
ledCharacteristic.write(&ledstate, sizeof(ledstate));
delay(5000);
}
Serial1.print("Disconnected");
Serial1.println(peripheral.address());
}

void loop() {
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
BLEDevice peripheral = BLE.available();
//pr_debug(LOG_MODULE_BLE, "%s-%d",__FUNCTION__, __LINE__);
if (peripheral)
{
Serial1.println(peripheral.address());
BLE.stopScanning();
delay (1000);
// central connected to peripheral
controlLed(peripheral);
delay (4000);
BLE.startScanning("LED");
}
}


74 changes: 74 additions & 0 deletions libraries/BLE/examples/test/test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

#include "ArduinoBLE.h"
#include "BLEAttribute.h"
#include "BLECharacteristicImp.h"
#include "BLEProfileManager.h"

// LED pin
#define LED_PIN 13

// create service
BLEService ledService("19b10100e8f2537e4f6cd104768a1214");

BLECharacteristic switchCharacteristic("19b10101e8f2537e4f6cd104768a1214", BLERead | BLEWrite | BLENotify, 1);

BLEDescriptor switchDescriptor("2901", "switch");

void setup() {
Serial1.begin(115200);
Serial1.println("test---");

// set LED pin to output mode
pinMode(LED_PIN, OUTPUT);

// begin initialization
BLE.begin();
Serial1.println(BLE.address());

// set advertised local name and service UUID
BLE.setLocalName("LED");
BLE.setAdvertisedServiceUuid(ledService.uuid());

// add service and characteristic
BLE.addService(ledService);
ledService.addCharacteristic(switchCharacteristic);
switchCharacteristic.addDescriptor(switchDescriptor);
unsigned char test = 1;
switchCharacteristic.writeValue(&test,1);
BLE.startAdvertising();
}

void loop() {
static int i = 0;
BLEDevice central = BLE.central();
bool temp = central;
i++;
if (temp) {
// central connected to peripheral
Serial1.print(i);
Serial1.print(F("Connected to central: "));
Serial1.println(central.address());

Serial1.print(temp);

while (central.connected()) {
// central still connected to peripheral
if (switchCharacteristic.written()) {
char ledValue = *switchCharacteristic.value();
// central wrote new value to characteristic, update LED
if (ledValue) {
Serial1.println(F("LED on"));
digitalWrite(LED_PIN, HIGH);
} else {
Serial1.println(F("LED off"));
digitalWrite(LED_PIN, LOW);
}
}
}

// central disconnected
Serial1.print(F("Disconnected from central: "));
Serial1.println(central.address());
}
//delay (1000);
}
9 changes: 9 additions & 0 deletions libraries/BLE/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=BLE
version=0.0
author=Lianggao
maintainer=Lianggao <[email protected]>
sentence=Library to manage the Bluetooth Low Energy module with Curie Core boards.
paragraph=Using this library, it is possible to use BLE features to communicate and interact with other devices like smartphones and tablets. This library enables multiple types of functionalities through a number of different classes.
category=Communication
url=
architectures=arc32
42 changes: 42 additions & 0 deletions libraries/BLE/src/ArduinoBLE.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
BLE API
Copyright (c) 2016 Arduino LLC. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef ARDUINO_BLE_H
#define ARDUINO_BLE_H

#define ARDUINO_BLE_API_VERSION 10000 // version 1.0.0

class BLEDevice;
class BLECharacteristic;
class BLEDescriptor;
class BLEService;
class BLECharacteristicImp;

#include "BLECommon.h"

#include "BLEDevice.h"

#include "BLECharacteristic.h"
#include "BLEDescriptor.h"
#include "BLEService.h"


extern BLEDevice BLE;

#endif
64 changes: 64 additions & 0 deletions libraries/BLE/src/BLEAttribute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2015 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.

* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#include "ArduinoBLE.h"
#include "BLEAttribute.h"

#include "BLEUtils.h"


BLEAttribute::BLEAttribute(const char* uuid, BLEAttributeType type) :
_type(type)
{
memset(&_uuid, 0, sizeof (_uuid));
BLEUtils::uuidString2BT(uuid, (bt_uuid_t*)&_uuid);
}

BLEAttribute::BLEAttribute(const bt_uuid_t* uuid, BLEAttributeType type) :
_type(type)
{
memcpy(&_uuid, uuid, sizeof (_uuid));
}

const bt_uuid_t *BLEAttribute::bt_uuid(void)
{
return (bt_uuid_t *)&_uuid;
}


BLEAttributeType
BLEAttribute::type() const {
return this->_type;
}

bool BLEAttribute::compareUuid(const bt_uuid_t* uuid)
{
int cmpresult = 0;
cmpresult = bt_uuid_cmp(uuid, (const bt_uuid_t*)&_uuid);
return (cmpresult == 0);

}

bool BLEAttribute::compareUuid(const char* uuid)
{
bt_uuid_128_t temp;
BLEUtils::uuidString2BT(uuid,(bt_uuid_t *)&temp);
return compareUuid((bt_uuid_t *)&temp);
}

73 changes: 73 additions & 0 deletions libraries/BLE/src/BLEAttribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2015 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.

* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/

#ifndef _BLE_ATTRIBUTE_H_INCLUDED
#define _BLE_ATTRIBUTE_H_INCLUDED

#include "BLECommon.h"

/// BLE attribute tyep enum
typedef enum {
BLETypeService = 0x2800, ///< the service type
BLETypeCharacteristic = 0x2803, ///< the characteristic type
BLETypeDescriptor = 0x2900 ///< the descriptor type
}BLEAttributeType;


class BLEAttribute {
public:
/**
* @brief Get the UUID raw data
*
* @param none
*
* @return bt_uuid_t* The pointer of UUID
*
* @note none
*/
const bt_uuid_t *bt_uuid(void);

/**
* @brief Compare the UUID with the paramater data
*
* @param[in] data The pointer of data
*
* @param[in] uuidsize The max size of UUID
*
* @return bool true - UUID is the same with data
* false- UUID is not the same with data
*
* @note none
*/
bool compareUuid(const char* uuid);
bool compareUuid(const bt_uuid_t* uuid);

BLEAttributeType type(void) const;

protected:
BLEAttribute(const char* uuid, BLEAttributeType type);
BLEAttribute(const bt_uuid_t* uuid, BLEAttributeType type);
private:
bt_uuid_128_t _uuid;

BLEAttributeType _type;

};

#endif // _BLE_ATTRIBUTE_H_INCLUDED
Loading