1529浏览
查看: 1529|回复: 0

[ESP8266/ESP32] App Inventor 和 FireBeetle 进行蓝牙通讯

[复制链接]
AppInventor 内置了蓝牙控件,但是经过我的实验内置的无法工作,感觉是对应的 SDK太老,导致在我的华为 P30 以及谷歌的Pixel5 上都无法运行。经过研究找到一种可以解决问题的方法,在 FireBeetle ESP32 上测试成功。
1.在 https://mit-cml.github.io/extensions/上下载         BluetoothLE.aix下载附件BluetoothLE.zip
2.打开AppInventor,我使用的是“广州市电化教育馆服务器”版本的:http://app.gzjkw.net/login/
3.Import上面的BluetoothLE.aix文件
App Inventor 和 FireBeetle 进行蓝牙通讯图1
                              
4.界面从上到下分别放置“列表显示框”“标签”“水平布局”“文本输入框”“按钮”和“BlueToothLE”(最后这个就是我们导入的控件):
App Inventor 和 FireBeetle 进行蓝牙通讯图2
列表显示框:适用于显示App 扫描得到的蓝牙设备的
标签:用于显示App 收到的FireBeetle通过蓝牙发送过来的数据
文本输入框:用于输入App发送给FireBeetle的数据
按钮:用于触发发送上面提到的文本输入框的内容
App Inventor 和 FireBeetle 进行蓝牙通讯图3
5.接下来进行逻辑设计
5.1 首先是当屏幕初始化时开启BLE的扫描功能
App Inventor 和 FireBeetle 进行蓝牙通讯图4
5.2扫描结果显示在列表中
App Inventor 和 FireBeetle 进行蓝牙通讯图5
5.3用于选择列表中的某一个设备之后,就用蓝牙进行连接
App Inventor 和 FireBeetle 进行蓝牙通讯图6
5.4 上述选择之后,App会通过蓝牙连接设备,连上之后隐藏列表框(太占地方),然后注册接收6E400001多字节数值服务 ,6E400003属性的数据(具体数值是在FireBeetle的Arduino 代码中定义的)。然后在标签上显示当前连接设备的名称:
App Inventor 和 FireBeetle 进行蓝牙通讯图7
5.5当6E400003收到数值后,会跳转到下面这里执行,我们只是将收到内容显示在标签上
App Inventor 和 FireBeetle 进行蓝牙通讯图8
5.6 按下按钮后,使用6E400002 属性将文本输入框的内容发送到FireBeetle 上
App Inventor 和 FireBeetle 进行蓝牙通讯图9
6.FireBeetle 代码:
  1. /*
  2.     Video: https://www.youtube.com/watch?v=oCMOYS71NIU
  3.     Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
  4.     Ported to Arduino ESP32 by Evandro Copercini
  5.    Create a BLE server that, once we receive a connection, will send periodic notifications.
  6.    The service advertises itself as: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
  7.    Has a characteristic of: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E - used for receiving data with "WRITE"
  8.    Has a characteristic of: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E - used to send data with  "NOTIFY"
  9.    The design of creating the BLE server is:
  10.    1. Create a BLE Server
  11.    2. Create a BLE Service
  12.    3. Create a BLE Characteristic on the Service
  13.    4. Create a BLE Descriptor on the characteristic
  14.    5. Start the service.
  15.    6. Start advertising.
  16.    In this example rxValue is the data received (only accessible inside that function).
  17.    And txValue is the data to be sent, in this example just a byte incremented every second.
  18. */
  19. #include <BLEDevice.h>
  20. #include <BLEServer.h>
  21. #include <BLEUtils.h>
  22. #include <BLE2902.h>
  23. BLECharacteristic *pCharacteristic;
  24. bool deviceConnected = false;
  25. uint8_t txStr[2]={0,0};
  26. // See the following for generating UUIDs:
  27. // https://www.uuidgenerator.net/
  28. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
  29. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  30. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  31. class MyServerCallbacks: public BLEServerCallbacks {
  32.     void onConnect(BLEServer* pServer) {
  33.       deviceConnected = true;
  34.     };
  35.     void onDisconnect(BLEServer* pServer) {
  36.       deviceConnected = false;
  37.     }
  38. };
  39. class MyCallbacks: public BLECharacteristicCallbacks {
  40.     void onWrite(BLECharacteristic *pCharacteristic) {
  41.       std::string rxValue = pCharacteristic->getValue();
  42.       if (rxValue.length() > 0) {
  43.         Serial.println("*********");
  44.         Serial.print("Received Value: ");
  45.         for (int i = 0; i < rxValue.length(); i++)
  46.           Serial.print(rxValue[i]);
  47.         Serial.println();
  48.         Serial.println("*********");
  49.       }
  50.     }
  51. };
  52. void setup() {
  53.   Serial.begin(115200);
  54.   // Create the BLE Device
  55.   BLEDevice::init("UART Service");
  56.   // Create the BLE Server
  57.   BLEServer *pServer = BLEDevice::createServer();
  58.   pServer->setCallbacks(new MyServerCallbacks());
  59.   // Create the BLE Service
  60.   BLEService *pService = pServer->createService(SERVICE_UUID);
  61.   // Create a BLE Characteristic
  62.   pCharacteristic = pService->createCharacteristic(
  63.                       CHARACTERISTIC_UUID_TX,
  64.                       BLECharacteristic::PROPERTY_READ|  BLECharacteristic::PROPERTY_NOTIFY
  65.                     );
  66.                      
  67.   pCharacteristic->addDescriptor(new BLE2902());
  68.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  69.                                          CHARACTERISTIC_UUID_RX,
  70.                                          BLECharacteristic::PROPERTY_WRITE
  71.                                        );
  72.   pCharacteristic->setCallbacks(new MyCallbacks());
  73.   // Start the service
  74.   pService->start();
  75.   // Start advertising
  76.   pServer->getAdvertising()->start();
  77.   Serial.println("Waiting a client connection to notify...");
  78. }
  79. void loop() {
  80.   if (deviceConnected) {
  81.     String datastring = "ESP32: ";
  82.     txStr[0]++;
  83.     txStr[1]=random(255);
  84.     Serial.printf("*** Sent ValueX: [%d] [%d] ***\n",txStr[0],txStr[1]);
  85.     pCharacteristic->setValue(txStr,2);
  86.     pCharacteristic->notify();
  87.    
  88.   }
  89.   delay(2000);
  90. }
复制代码

代码会发送 2字节长度的txStr[]数组到App 中,其中txStr[0]从0-255不停增加,txStr[1]存放一个随机生成的数值。
AppInventor程序  
Arduino 程序

App Inventor 和 FireBeetle 进行蓝牙通讯图12
  

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail