【问题标题】:Reading data from a non-connectable(?) Bluetooth device on ESP32从 ESP32 上的不可连接(?)蓝牙设备读取数据
【发布时间】:2020-02-04 14:51:57
【问题描述】:
我想让我的 ESP32 设备始终等待收听来自我的 BT 浴室秤的数据。
当我在 Android 应用中查看设备时,它会将其列为“不可连接”。所以我想这是一回事。但是我看到的所有示例代码都做了某种“连接”。当我尝试连接时,它只是挂在我的代码中。
我可以在不先连接的情况下从特征或其他内容中获取数据吗?
或者对我做错了什么有任何想法?我很高兴分享我的代码或我的手机应用程序的屏幕截图或任何可能有帮助的内容。谢谢
【问题讨论】:
标签:
bluetooth
bluetooth-lowenergy
esp32
【解决方案1】:
如果其他人有同样的问题。我找到了答案。
数据在制造商数据字段的最后几个字节中传递。
这段代码可以读取。
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);
}
希望它可以帮助其他人走上正轨。