Reputation: 83
I want to have my ESP32 device always waiting to listen for data from my BT bathroom scale.
When I look at the device in an Android app, it lists it as "non-connectable". So I guess that's a thing. But all the sample code I've seen does a "connect" of some sort. And when I try to connect, it just hangs there in my code.
Can I get data from a characteristic or whatever without first connecting?
Or any thoughts on what I'm doing wrong? I'm happy to share my code or screenshots of my phone app or whatever might be helpful. Thanks
Upvotes: 2
Views: 1084
Reputation: 83
In case others have this same issue. I've found an answer.
The data is being passed in the last few bytes of the Manufacturer Data field.
This code is able to read it.
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
/**
Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice)
{
if ( strcmp( "ed:67:37:5e:9c:e3", advertisedDevice.getAddress().toString().c_str() ) == 0 ) {
int slen = 11;
int dg =
advertisedDevice.getManufacturerData().c_str()[slen - 1] << 8 |
advertisedDevice.getManufacturerData().c_str()[slen - 2];
int stability = (advertisedDevice.getManufacturerData().c_str()[slen - 3] & 0x01);
dbgprintf("Scale: %.1f lbs %s\n", (float) dg / 100.0 * 2.205, (stability) ? "(stable)" : "" );
advertisedDevice.getScan()->stop();
foundMeasurement = true;
}
} // onResult
};
void startScanning()
{
// Retrieve a Scanner and set the callback we want to use to be informed when we
// have detected a new device. Specify that we want active scanning and start the
// scan to run for 30 seconds.
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
}
Hope it helps someone else get on the right track.
Upvotes: 1