Skip to content

BLE: combination of 337, 342 & xieqi's commit #343

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 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
19a5634
Arduino BLE new library init version
sgbihu Oct 17, 2016
0b47a91
Add BLE library.properties file
sgbihu Oct 18, 2016
9d3bab2
Implement central feature
sgbihu Oct 21, 2016
35027d3
Add central example sketch
sgbihu Oct 21, 2016
521d1db
Support Notify/Indicate feature
sgbihu Oct 24, 2016
30b88ef
Fix the callback issue and the Klock scan issues
sgbihu Oct 25, 2016
5fc779b
Add BLEPeripheral library back compatible features
sgbihu Oct 27, 2016
5b69b5a
Fix the review issues and dismiss some TODOs
sgbihu Oct 31, 2016
e916136
Fix discover potential bugs
sgbihu Nov 2, 2016
1836856
Enable some confirmed APIs
sgbihu Nov 7, 2016
bf78754
Update the code based on review
sgbihu Nov 7, 2016
fd4e43a
Update code based on code review
sgbihu Nov 7, 2016
31d1838
Fix Jira 747 BLE Central Mode peripheral.discoverAtrtributes() Hangs
sgbihu Nov 7, 2016
c8c36a3
Fix Jira 738 BLE Verify that BT Address in OTP is the same as adverti…
sgbihu Nov 8, 2016
19baf30
Update examples and change code based on code review
sgbihu Nov 8, 2016
619599a
Use our own version of copy.exe so that we can copy without cmd.exe
calvinatintel Oct 27, 2016
297728d
Recompile libarc32drv_arduino101.a
eriknyquist Nov 8, 2016
ca22565
advertisedServiceUuid: force caller to provide storage
eriknyquist Nov 8, 2016
6d15bef
BLEDeviceManager.cpp: fix typo in advertisedServicesUuid()
eriknyquist Nov 9, 2016
6a4d381
Fix *.address() and *.localName methods
eriknyquist Nov 9, 2016
25eb59f
fix KW issue for class BLEDescriptor, BLECharacteristic and port.c
Nov 9, 2016
5fa6a98
Recompile libarc32drv_arduino101.a
eriknyquist Nov 10, 2016
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
133 changes: 133 additions & 0 deletions libraries/BLE/examples/central/led_control/led_control.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Arduino BLE Central LED Control example
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
*/


#include <ArduinoBLE.h>

// variables for button
const int buttonPin = 2;
int oldButtonState = LOW;

char addr_buf[BT_ADDR_STR_LEN];
char name_buf[BLE_MAX_ADV_SIZE];
char uuid_buf[70];

void setup() {
Serial.begin(9600);

// configure the button pin as input
pinMode(buttonPin, INPUT);

// initialize the BLE hardware
BLE.begin();

Serial.println("BLE Central - LED control");

// start scanning for peripherals
BLE.startScanning();
}

void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
peripheral.advertisedServiceUuid(uuid_buf);
peripheral.address(addr_buf);
peripheral.localName(name_buf);

Serial.print("Found ");
Serial.print(addr_buf);
Serial.print(" '");
Serial.print(name_buf);
Serial.print("' ");
Serial.println(uuid_buf);

// see if peripheral is advertising the LED service
if (String(uuid_buf) == String("19b10000-e8f2-537e-4f6c-d104768a1214")) {
// stop scanning
BLE.stopScanning();

controlLed(peripheral);

// peripheral disconnected, start scanning again
BLE.startScanning();
}
}
}

void controlLed(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");

if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}

// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}

// retrieve the LED characteristic
BLECharacteristic ledCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

if (!ledCharacteristic) {
Serial.println("Peripheral does not have LED characteristic!");
peripheral.disconnect();
return;
} else if (!ledCharacteristic.canWrite()) {
Serial.println("Peripheral does not have a writable LED characteristic!");
peripheral.disconnect();
return;
}

while (peripheral.connected()) {
// while the peripheral is connection

// read the button pin
int buttonState = digitalRead(buttonPin);

if (oldButtonState != buttonState) {
// button changed
oldButtonState = buttonState;

if (buttonState) {
Serial.println("button pressed");

// button is pressed, write 0x01 to turn the LED on
ledCharacteristic.writeByte(0x01);
} else {
Serial.println("button released");

// button is released, write 0x00 to turn the LED of
ledCharacteristic.writeByte(0x00);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
Arduino BLE Central peripheral explorer example
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
*/

#include <ArduinoBLE.h>

char addr_buf[BT_ADDR_STR_LEN];
char name_buf[BLE_MAX_ADV_SIZE];
char uuid_buf[70];

void setup() {
Serial.begin(9600);

// initialize the BLE hardware
BLE.begin();

Serial.println("BLE Central - Peripheral Explorer");

// start scanning for peripherals
BLE.startScanning();
}

void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

if (peripheral) {
// discovered a peripheral, print out address, local name, and advertised service
peripheral.advertisedServiceUuid(uuid_buf);
peripheral.address(addr_buf);
peripheral.localName(name_buf);

Serial.print("Found ");
Serial.print(addr_buf);
Serial.print(" '");
Serial.print(name_buf);
Serial.print("' ");
Serial.println(uuid_buf);

// see if peripheral is a SensorTag
if (String(name_buf) == "SensorTag") {
// stop scanning
BLE.stopScanning();

explorerPeripheral(peripheral);

// peripheral disconnected, we are done
while (1) {
// do nothing
}
}
}
}

void explorerPeripheral(BLEDevice peripheral) {
// connect to the peripheral
Serial.println("Connecting ...");

if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}

// discover peripheral attributes
Serial.println("Discovering attributes ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}

// read and print device name of peripheral
Serial.println();
Serial.print("Device name: ");
Serial.println(peripheral.deviceName());

// read and print appearance of peripheral
Serial.print("Appearance: ");
Serial.println(peripheral.appearance());
Serial.println();

// loop the services of the peripheral and explore each
for (int i = 0; i < peripheral.serviceCount(); i++) {
BLEService service = peripheral.service(i);

exploreService(service);
}

Serial.println();

// we are done exploring, disconnect
Serial.println("Disconnecting ...");
peripheral.disconnect();
Serial.println("Disconnected");
}

void exploreService(BLEService service) {
// print the UUID of the service
Serial.print("Service ");
Serial.println(service.uuid());

// loop the characteristics of the service and explore each
for (int i = 0; i < service.characteristicCount(); i++) {
BLECharacteristic characteristic = service.characteristic(i);

exploreCharacteristic(characteristic);
}
}

void exploreCharacteristic(BLECharacteristic characteristic) {
// print the UUID and properies of the characteristic
Serial.print("\tCharacteristic ");
Serial.print(characteristic.uuid());
Serial.print(", properties 0x");
Serial.print(characteristic.properties());

// check if the characteristic is readable
if (characteristic.canRead()) {
// read the characteristic value
characteristic.read();

// print out the value of the characteristic
Serial.print(", value 0x");
printData(characteristic.value(), characteristic.valueLength());
}

Serial.println();

// loop the descriptors of the characteristic and explore each
for (int i = 0; i < characteristic.descriptorCount(); i++) {
BLEDescriptor descriptor = characteristic.descriptor(i);

exploreDescriptor(descriptor);
}
}

void exploreDescriptor(BLEDescriptor descriptor) {
// print the UUID of the descriptor
Serial.print("\t\tDescriptor ");
Serial.print(descriptor.uuid());

// read the descriptor value
descriptor.read();

// print out the value of the descriptor
Serial.print(", value 0x");
printData(descriptor.value(), descriptor.valueLength());

Serial.println();
}

void printData(const unsigned char data[], int length) {
for (int i = 0; i < length; i++) {
unsigned char b = data[i];

if (b < 16) {
Serial.print("0");
}

Serial.print(b, HEX);
}
}

74 changes: 74 additions & 0 deletions libraries/BLE/examples/central/scan/scan.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Arduino BLE Central scan example
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
*/

#include <ArduinoBLE.h>

char addr_buf[BT_ADDR_STR_LEN];
char name_buf[BLE_MAX_ADV_SIZE];
char uuid_buf[70];

void setup() {
Serial.begin(9600);

// initialize the BLE hardware
BLE.begin();

Serial.println("BLE Central scan");

// start scanning for peripheral
BLE.startScanning();
}

void loop() {
// check if a peripheral has been discovered
BLEDevice peripheral = BLE.available();

if (peripheral) {
// discovered a peripheral
Serial.println("Discovered a peripheral");
Serial.println("-----------------------");

// print address
peripheral.address(addr_buf);
Serial.println("Address: " + String(addr_buf));

// print the local name, if present
if (peripheral.hasLocalName()) {
peripheral.localName(name_buf);
Serial.println("Local Name: " + String(name_buf));
}

// print the advertised service UUID's, if present
if (peripheral.hasAdvertisedServiceUuid()) {
Serial.print("Service UUID's: ");
for (int i = 0; i < peripheral.advertisedServiceUuidCount(); i++) {
peripheral.advertisedServiceUuid(i, uuid_buf);
Serial.print(String(uuid_buf) + " ");
}
Serial.println();
}

// print the RSSI
Serial.print("RSSI: ");
Serial.println(peripheral.rssi());

Serial.println();
}
}

Loading