Skip to content

Commit 6090e42

Browse files
sgbihueriknyquist
authored andcommitted
Arduino BLE new library init version
Know issues: 1. Memory issue. Some times the memcmp not work correct. 2. Central not complete. But can do scan.
1 parent 41f98f9 commit 6090e42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+7323
-14
lines changed

libraries/BLE/examples/test/test.ino

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
#include "ArduinoBLE.h"
3+
#include "BLEAttribute.h"
4+
#include "BLECharacteristicImp.h"
5+
#include "BLEProfileManager.h"
6+
7+
// LED pin
8+
#define LED_PIN 13
9+
10+
// create service
11+
BLEService ledService("19b10100e8f2537e4f6cd104768a1214");
12+
13+
BLECharacteristic switchCharacteristic("19b10101e8f2537e4f6cd104768a1214", BLERead | BLEWrite | BLENotify, 1);
14+
15+
BLEDescriptor switchDescriptor("2901", "switch");
16+
17+
void setup() {
18+
Serial1.begin(115200);
19+
Serial1.println("test---");
20+
21+
// set LED pin to output mode
22+
pinMode(LED_PIN, OUTPUT);
23+
24+
// begin initialization
25+
BLE.begin();
26+
Serial1.println(BLE.address());
27+
28+
// set advertised local name and service UUID
29+
BLE.setLocalName("LED");
30+
BLE.setAdvertisedServiceUuid(ledService.uuid());
31+
32+
// add service and characteristic
33+
BLE.addService(ledService);
34+
ledService.addCharacteristic(switchCharacteristic);
35+
switchCharacteristic.addDescriptor(switchDescriptor);
36+
unsigned char test = 1;
37+
switchCharacteristic.writeValue(&test,1);
38+
BLE.startAdvertising();
39+
}
40+
41+
void loop() {
42+
static int i = 0;
43+
BLEDevice central = BLE.central();
44+
bool temp = central;
45+
i++;
46+
if (temp) {
47+
// central connected to peripheral
48+
Serial1.print(i);
49+
Serial1.print(F("Connected to central: "));
50+
Serial1.println(central.address());
51+
52+
Serial1.print(temp);
53+
54+
while (central.connected()) {
55+
// central still connected to peripheral
56+
if (switchCharacteristic.written()) {
57+
char ledValue = *switchCharacteristic.value();
58+
// central wrote new value to characteristic, update LED
59+
if (ledValue) {
60+
Serial1.println(F("LED on"));
61+
digitalWrite(LED_PIN, HIGH);
62+
} else {
63+
Serial1.println(F("LED off"));
64+
digitalWrite(LED_PIN, LOW);
65+
}
66+
}
67+
}
68+
69+
// central disconnected
70+
Serial1.print(F("Disconnected from central: "));
71+
Serial1.println(central.address());
72+
}
73+
//delay (1000);
74+
}

libraries/BLE/src/ArduinoBLE.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
BLE API
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef ARDUINO_BLE_H
21+
#define ARDUINO_BLE_H
22+
23+
#define ARDUINO_BLE_API_VERSION 10000 // version 1.0.0
24+
25+
class BLEDevice;
26+
class BLECharacteristic;
27+
class BLEDescriptor;
28+
class BLEService;
29+
class BLECharacteristicImp;
30+
31+
#include "BLECommon.h"
32+
33+
#include "BLEDevice.h"
34+
35+
#include "BLECharacteristic.h"
36+
#include "BLEDescriptor.h"
37+
#include "BLEService.h"
38+
39+
40+
extern BLEDevice BLE;
41+
42+
#endif

libraries/BLE/src/BLEAttribute.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
*/
19+
20+
#include "ArduinoBLE.h"
21+
#include "BLEAttribute.h"
22+
23+
#include "BLEUtils.h"
24+
25+
26+
BLEAttribute::BLEAttribute(const char* uuid, BLEAttributeType type) :
27+
_type(type)
28+
{
29+
memset(&_uuid, 0, sizeof (_uuid));
30+
BLEUtils::uuidString2BT(uuid, (bt_uuid_t*)&_uuid);
31+
}
32+
33+
BLEAttribute::BLEAttribute(const bt_uuid_t* uuid, BLEAttributeType type) :
34+
_type(type)
35+
{
36+
memcpy(&_uuid, uuid, sizeof (_uuid));
37+
}
38+
39+
const bt_uuid_t *BLEAttribute::bt_uuid(void)
40+
{
41+
return (bt_uuid_t *)&_uuid;
42+
}
43+
44+
45+
BLEAttributeType
46+
BLEAttribute::type() const {
47+
return this->_type;
48+
}
49+
50+
bool BLEAttribute::compareUuid(const bt_uuid_t* uuid)
51+
{
52+
int cmpresult = 0;
53+
cmpresult = bt_uuid_cmp(uuid, (const bt_uuid_t*)&_uuid);
54+
return (cmpresult == 0);
55+
56+
}
57+
58+
bool BLEAttribute::compareUuid(const char* uuid)
59+
{
60+
bt_uuid_128_t temp;
61+
BLEUtils::uuidString2BT(uuid,(bt_uuid_t *)&temp);
62+
return compareUuid((bt_uuid_t *)&temp);
63+
}
64+

libraries/BLE/src/BLEAttribute.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2015 Intel Corporation. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
*/
19+
20+
#ifndef _BLE_ATTRIBUTE_H_INCLUDED
21+
#define _BLE_ATTRIBUTE_H_INCLUDED
22+
23+
#include "BLECommon.h"
24+
25+
/// BLE attribute tyep enum
26+
typedef enum {
27+
BLETypeService = 0x2800, ///< the service type
28+
BLETypeCharacteristic = 0x2803, ///< the characteristic type
29+
BLETypeDescriptor = 0x2900 ///< the descriptor type
30+
}BLEAttributeType;
31+
32+
33+
class BLEAttribute {
34+
public:
35+
/**
36+
* @brief Get the UUID raw data
37+
*
38+
* @param none
39+
*
40+
* @return bt_uuid_t* The pointer of UUID
41+
*
42+
* @note none
43+
*/
44+
const bt_uuid_t *bt_uuid(void);
45+
46+
/**
47+
* @brief Compare the UUID with the paramater data
48+
*
49+
* @param[in] data The pointer of data
50+
*
51+
* @param[in] uuidsize The max size of UUID
52+
*
53+
* @return bool true - UUID is the same with data
54+
* false- UUID is not the same with data
55+
*
56+
* @note none
57+
*/
58+
bool compareUuid(const char* uuid);
59+
bool compareUuid(const bt_uuid_t* uuid);
60+
61+
BLEAttributeType type(void) const;
62+
63+
protected:
64+
BLEAttribute(const char* uuid, BLEAttributeType type);
65+
BLEAttribute(const bt_uuid_t* uuid, BLEAttributeType type);
66+
private:
67+
bt_uuid_128_t _uuid;
68+
69+
BLEAttributeType _type;
70+
71+
};
72+
73+
#endif // _BLE_ATTRIBUTE_H_INCLUDED
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
BLE Attribute with value API
3+
Copyright (c) 2016 Arduino LLC. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef ARDUINO_BLE_ATTRIBUTE_WITH_VALUE__H
21+
#define ARDUINO_BLE_ATTRIBUTE_WITH_VALUE__H
22+
23+
class BLEAttributeWithValue
24+
{
25+
public:
26+
BLEAttributeWithValue();
27+
28+
virtual int valueLength() const; // returns the length of the attribute value
29+
virtual const byte* value() const; // returns the value of the attribute array
30+
virtual byte operator[] (int offset) const; // access an attribute value at the specified offset
31+
virtual bool writeValue(const byte value[], int length);
32+
33+
// intepret the value of the attribute with the specified type
34+
String stringValue() const;
35+
char charValue() const;
36+
unsigned char unsignedCharValue() const;
37+
byte byteValue() const;
38+
short shortValue() const;
39+
unsigned short unsignedShortValue() const;
40+
int intValue() const;
41+
unsigned int unsignedIntValue() const;
42+
long longValue() const;
43+
unsigned long unsignedLongValue() const;
44+
float floatValue() const;
45+
double doubleValue() const;
46+
47+
// write the value of the attribute with the specified type
48+
bool writeString(const String& s);
49+
bool writeString(const char* s);
50+
bool writeChar(char c);
51+
bool writeUnsignedChar(unsigned char c);
52+
bool writeByte(byte b);
53+
bool writeShort(short s);
54+
bool writeUnsignedShort(unsigned short s);
55+
bool writeInt(int i);
56+
bool writeUnsignedInt(unsigned int i);
57+
bool writeLong(long l);
58+
bool writeUnsignedLong(unsigned int l);
59+
bool writeFloat(float f);
60+
bool writeDouble(double d);
61+
};
62+
63+
#endif

0 commit comments

Comments
 (0)