App Inventor 和 FireBeetle 进行蓝牙通讯
AppInventor 内置了蓝牙控件,但是经过我的实验内置的无法工作,感觉是对应的 SDK太老,导致在我的华为 P30 以及谷歌的Pixel5 上都无法运行。经过研究找到一种可以解决问题的方法,在 FireBeetle ESP32 上测试成功。1.在 https://mit-cml.github.io/extensions/上下载 BluetoothLE.aix2.打开AppInventor,我使用的是“广州市电化教育馆服务器”版本的:http://app.gzjkw.net/login/3.Import上面的BluetoothLE.aix文件 4.界面从上到下分别放置“列表显示框”“标签”“水平布局”“文本输入框”“按钮”和“BlueToothLE”(最后这个就是我们导入的控件): 列表显示框:适用于显示App 扫描得到的蓝牙设备的标签:用于显示App 收到的FireBeetle通过蓝牙发送过来的数据文本输入框:用于输入App发送给FireBeetle的数据按钮:用于触发发送上面提到的文本输入框的内容 5.接下来进行逻辑设计5.1 首先是当屏幕初始化时开启BLE的扫描功能 5.2扫描结果显示在列表中 5.3用于选择列表中的某一个设备之后,就用蓝牙进行连接 5.4 上述选择之后,App会通过蓝牙连接设备,连上之后隐藏列表框(太占地方),然后注册接收6E400001多字节数值服务 ,6E400003属性的数据(具体数值是在FireBeetle的Arduino 代码中定义的)。然后在标签上显示当前连接设备的名称: 5.5当6E400003收到数值后,会跳转到下面这里执行,我们只是将收到内容显示在标签上 5.6 按下按钮后,使用6E400002 属性将文本输入框的内容发送到FireBeetle 上 6.FireBeetle 代码:/*Video: https://www.youtube.com/watch?v=oCMOYS71NIU
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
Create a BLE server that, once we receive a connection, will send periodic notifications.
The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with"NOTIFY"
The design of creating the BLE server is:
1. Create a BLE Server
2. Create a BLE Service
3. Create a BLE Characteristic on the Service
4. Create a BLE Descriptor on the characteristic
5. Start the service.
6. Start advertising.
In this example rxValue is the data received (only accessible inside that function).
And txValue is the data to be sent, in this example just a byte incremented every second.
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
uint8_t txStr={0,0};
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("UART Service");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_READ|BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
if (deviceConnected) {
String datastring = "ESP32: ";
txStr++;
txStr=random(255);
Serial.printf("*** Sent ValueX: [%d] [%d] ***\n",txStr,txStr);
pCharacteristic->setValue(txStr,2);
pCharacteristic->notify();
}
delay(2000);
}
代码会发送 2字节长度的txStr[]数组到App 中,其中txStr从0-255不停增加,txStr存放一个随机生成的数值。AppInventor程序 Arduino 程序
页:
[1]