我在 DFRobot 的 FireBeetle 上实验了如下代码,非常好用: - /**
- * This example turns the ESP32 into a Bluetooth LE mouse that continuously moves the mouse.
- */
- #include <BleMouse.h>
-
- BleMouse bleMouse;
-
- void setup() {
- Serial.begin(115200);
- Serial.println("Starting BLE work!");
- bleMouse.begin();
- }
-
- void loop() {
- if(bleMouse.isConnected()) {
-
- while (Serial.available())
- {
- switch (Serial.read()) {
- case 'l':
- Serial.println("Left click");
- bleMouse.click(MOUSE_LEFT);
- break;
- case 'r':
- Serial.println("Right click");
- bleMouse.click(MOUSE_RIGHT);
- break;
- case 'c':
- Serial.println("move a rectangle");
- bleMouse.move(127,0,0,0);
- delay(200);
- bleMouse.move(0,127,0,0);
- delay(200);
- bleMouse.move(-127,0,0,0);
- delay(200);
- bleMouse.move(0,-127,0,0);
- delay(200);
- break;
- }
- }
- }
- }
复制代码
上电之后,打开本机的蓝牙,搜索设备,可以看到 “ESP32 Bluetooth Mouse”,连接上即可:
之后,串口输入 “l” 是点击左键,“r”是点击右键,“c” 会在屏幕上按照矩形路径移动鼠标。
ESP32-BLE-Mouse-master.zip
(8.14 KB, 下载次数: 60)
|