[Video]: Bluetooth BLE and C++ classes

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Thu Jul 13, 2017 8:07 pm

Very informative, but unfortunately the error still persists. Here is main.cpp

Code: Select all

extern "C" {
	void app_main(void);
}

void SampleServer(void);
void Sample1(void);
void SampleRead(void);
void SampleWrite(void);
void SampleScan(void);
void SampleNotify(void);
void SampleClient(void);
void Sample_MLE_15(void);

void app_main(void) {
	//SampleServer();
	//Sample1();
	//SampleRead();
	//SampleWrite();
	SampleScan();
	//SampleNotify();
	//SampleClient();
	//Sample_MLE_15();
}
And here is the SampleScan() file:

Code: Select all

#include "BLE.h"
#include "BLEUtils.h"
#include "BLEScan.h"
#include <esp_log.h>
#include <string>

#include "BLEAdvertisedDeviceCallbacks.h"
#include "sdkconfig.h"

static const char LOG_TAG[] = "SampleScan";

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
	void onResult(BLEAdvertisedDevice *pAdvertisedDevice) {
		ESP_LOGD(LOG_TAG, "Advertised Device: %s", pAdvertisedDevice->toString().c_str());
	}
};

static void run() {
	ESP_LOGD(LOG_TAG, "Scanning sample starting");
	BLE::initClient();
	BLEScan* pBLEScan = BLE::getScan();
	pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
	pBLEScan->setActiveScan(true);
	std::vector<BLEAdvertisedDevice*> foundDevices = pBLEScan->start(30);
	ESP_LOGD(LOG_TAG, "We found %d devices", foundDevices.size());
	ESP_LOGD(LOG_TAG, "Scanning sample ended");
}

void SampleScan(void)
{
	run();
} // app_main

These were taken directly from your BLE Test in esp32-snippets github.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Thu Jul 13, 2017 9:12 pm

The next clue I have is that your main.cpp source file isn't being compiled/linked. Since main.cpp contains "app_main()" and it appears to be correctly declared as having "C" like exposure but yet the linker is saying "I'm sorry, I couldn't find a function called app_main()" ... one possibility is that we haven't presented "main.cpp" (which contains app_main()) to the compiler.

To test this theory, touch (modify) the source of main.cpp and recompile. See if a change of the source results in a recompilation. If it doesn't then that would indicate that we aren't actually compiling main.cpp ... and if we aren't compiling it, we aren't defining it and that would give us the "not found" error message.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Thu Jul 13, 2017 9:29 pm

It seems that you are right, I have put in garbage text at the top of the file and it did not respond to it:

Code: Select all

asDasdjasd
sad
!!

extern "C" {
	void app_main(void);
}

void SampleServer(void);
void Sample1(void);
void SampleRead(void);
void SampleWrite(void);
void SampleScan(void);
void SampleNotify(void);
void SampleClient(void);
void Sample_MLE_15(void);

void app_main(void) {
	//SampleServer();
	//Sample1();
	//SampleRead();
	//SampleWrite();
	SampleScan();
	//SampleNotify();
	//SampleClient();
	//Sample_MLE_15();
}

Why is my code not loading itself? I have done as I usually do with all my other programs. Here is the file structure:
http://imgur.com/dRCqQIr

Component only has the cpp_utils files.

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Thu Jul 13, 2017 9:43 pm

Ah! Oh no! I have forgot the component.mk in the main folder :lol:

It works now, thank you for putting up with my silliness. I will experiment with this and report back. Very nice work from what i'm seeing so far!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Thu Jul 13, 2017 9:47 pm

Delighted you got it going ... and don't feel bad. The only reason I was able to help is that I have done the same thing myself too many times to remember and the pattern of error was familiar to me. Glad you are liking what you see so far. Don't hesitate to post back if you have problems, puzzles, questions or enhancement requests. If it doesn't do what you want, my hope is that you will make that known so that I can either address or else forewarn someone else who might also need same.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Thu Jul 13, 2017 9:51 pm

Will do! Do you recommend any particular code to test out nRF Connect to make sure my phone is okay with the BLE?

The samples I have been testing give me the following error:

Code: Select all

E (730) BT: config_parse returned with err code: 2

E (780) BLEService: Handle is already set 28
and I can't connect to it using nRF Connect with the same log that I had given you previously
Last edited by frostyowned on Thu Jul 13, 2017 10:01 pm, edited 1 time in total.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Thu Jul 13, 2017 10:01 pm

Unfortunately there's no great quick answer to that. BLE is full of options and permutations, some of which are exclusive to each other.

I think what you are asking for is "Do you have any C++ BLE samples that I can use to see how things work and then use nRF Connect application on my phone as the partner BLE device?".

I think you have found the samples here ...

https://github.com/nkolban/esp32-snippe ... LE%20Tests

These are all sample ESP32 C++ BLE applications that perform different tests such as being a server, scanning, being a client, sending notifications etc etc.

Thankfully, because of the intent of the project, the samples are hopefully short enough to comprehend. Combine that with the first pass for a programmers guide found here:

https://github.com/nkolban/esp32-snippe ... umentation

... my hope is that this *might* be enough to get us started.

The nRF Connect tool can be either a BLE client (to, presumably, your ESP32 being a BLE server) OR it can be a BLE Server (to presumably, your ESP32 being a BLE client).

Also don't forget that the nRF Connect tool is just that, a tool for development purposes. You can also use Web Bluetooth and other BLE APIs on devices/platforms other than the ESP32 as your target endpoints. If you are have a Linux environment, I have had success using tools such as bluetoothctl and gatttool using a Bluetooth v4 dongle plugged into my PC. However, my tool of choice remains the nRF Connect on my cell phone. I haven't come across a PC desktop GUI equivalent to that yet.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Fri Jul 14, 2017 1:01 pm

I have learned a lot from that guide! I have a couple of more questions if you don't mind.

1) How do I decide what ServiceUUID to use?
2) Is there a list of services somewhere I can look at?
3) What are the different events that may be triggered in BLE? I can use onRead and onWrite but do I have to parse what was written to know what is triggered?
4) If I have made a request to read data, how would I send out multiple streams if the desired data is too much for BLE?
5) The android phone (4.4) is failing to get the services because it is sent too fast after connect, is it possible to delay the service information so that we can get it properly?
6) Is it possible to make my own Services based off a mobile app I develop my self on android ( I am using the nrf connect for now, but I am unsure if I have chosen the correct UUID )

I am honestly unsure why Samsung Galaxy S5 does not want to connect to the BLE. I have tried again and it works fine on nexus, nRF Connect can actually obtain services and etc. On Galaxy S5 it still disconnects itself with this set:

Code: Select all

Connected to MACADDRESS
[Broadcast] Action received: android.bluetooth.device.action.ACL_CONNECTED
Discovering services...
gatt.discoverServices()
[Callback] Services discovered with status: 129
Error 129 ( 0x81 ): GATT INTERNAL ERROR
[Callback] Connection state changed with status: 133 and new state: DISCONNECTED (0)
Error 133 (0x85): GATT ERROR
Disconnected
[Broadcast] Action received: android.bluetooth.device.action.ACL_DISCONNECTED
and I have used this code:

Code: Select all

#include "BLE.h"
#include "BLEUtils.h"
#include "BLEServer.h"
#include <esp_log.h>
#include <string>

#include "sdkconfig.h"

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

static void run() {
	BLE::initServer("MYDEVICE");

	BLEServer *pServer = new BLEServer();

	BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));

	BLECharacteristic *pCharacteristic = pService->createCharacteristic(
		BLEUUID(CHARACTERISTIC_UUID),
		BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
	);

	pCharacteristic->setValue("Hello World says Neil");

	pService->start();

	BLEAdvertising *pAdvertising = pServer->getAdvertising();
	pAdvertising->start();
}

void Sample1(void)
{
	run();
} // app_main
Last edited by frostyowned on Fri Jul 14, 2017 2:01 pm, edited 1 time in total.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video]: Bluetooth BLE and C++ classes

Postby kolban » Fri Jul 14, 2017 2:00 pm

Howdy,
The notion of working with the BLE C++ classes are to make BLE API programming easier ... but unfortunately, they aren't a substitute for the underlying BLE high level knowledge. For example, although the classes make BLE Characteristic usage a lot easier, they aren't meant as a substitute for hitting the books and understand "what" a BLE Characteristic actually is. One can hit the Internet or get some books:

https://www.amazon.com/Bluetooth-Low-En ... 013288836X

http://shop.oreilly.com/product/0636920033011.do

While the above will cover everything you need to know, lets try this set of about half a dozen questions:

https://learn.adafruit.com/introduction ... low-energy

Read the pages at the link above (a couple of times) and then come back to your questions posted here and see how many of them were answered. For the ones that weren't I and the rest of the community will have a bash or provide more specialized reading links.

Your one question that I don't think will be covered is:
The android phone (4.4) is failing to get the services because it is sent too fast after connect, is it possible to delay the service information so that we can get it properly?
and that one is important to me. It is possible that there is a bug in the C++ code. Can you explain in more detail the story/scenario that results in the error. The C++ classes deliberately hide callback processing that you would otherwise have to be responsible to implement. I want to determine if these are correct for the scenario you describe and, if not, we'll get them fixed.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: [Video]: Bluetooth BLE and C++ classes

Postby frostyowned » Fri Jul 14, 2017 2:02 pm

I have edited the post with the nRF Logger and the code I used. I will read those resources and try to get a better grasp

Who is online

Users browsing this forum: cdollar and 100 guests