设备
https://item.taobao.com/item.htm?spm=a1z09.2.0.0.50122e8dJH3bCJ&id=547198899364&_u=51qf7bf5dd52
安装库
https://github.com/T-vK/ESP32-BLE-Mouse
下载zip
软件添加库
烧录
选择版型
选择程序例子
烧录时候 需要按着boot按键,结束后松开,按下reset按键
电脑或者手机连接蓝牙
串口输出,电脑跟着改变
单纯蓝牙控制例子
#include <Arduino.h> #include <BluetoothSerial.h> BluetoothSerial SerialBT; const int ledPin1 = 2; // LED1连接的引脚 const int ledPin2 = 4; // LED2连接的引脚 void setup() { Serial.begin(115200); SerialBT.begin("ESP32_LED"); // 蓝牙设备名称 pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); Serial.println("蓝牙已初始化,可以通过蓝牙控制LED"); } void loop() { if (SerialBT.available()) { char command = SerialBT.read(); // 读取蓝牙数据 switch (command) { case '1': digitalWrite(ledPin1, HIGH); // 点亮LED1 SerialBT.println("LED1已点亮"); // 反馈信息 break; case '2': digitalWrite(ledPin1, LOW); // 熄灭LED1 SerialBT.println("LED1已熄灭"); // 反馈信息 break; case '3': digitalWrite(ledPin2, HIGH); // 点亮LED2 SerialBT.println("LED2已点亮"); // 反馈信息 break; case '4': digitalWrite(ledPin2, LOW); // 熄灭LED2 SerialBT.println("LED2已熄灭"); // 反馈信息 break; } } }
上下左右切换视频滑动
鼠标初次链接自动出现在正中间
#include <BleMouse.h> BleMouse bleMouse; void setup() { Serial.begin(115200); Serial.println("Starting BLE work!"); bleMouse.begin(); } void loop() { if(bleMouse.isConnected()) { unsigned long startTime; Serial.println("上滑"); bleMouse.move(0,0,1);// 视频切换 微信一个对话框宽度 delay(2000); Serial.println("下滑"); bleMouse.move(0,0,-1); delay(2000); Serial.println("右滑"); bleMouse.move(0,0,0,1); delay(2000); Serial.println("左滑"); bleMouse.move(0,0,0,-1); delay(2000); } }
鼠标点击样例
/** * 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()) { Serial.println("Left click"); bleMouse.click(MOUSE_LEFT); delay(500); Serial.println("Right click"); bleMouse.click(MOUSE_RIGHT); delay(500); Serial.println("Scroll wheel click"); bleMouse.click(MOUSE_MIDDLE); delay(500); Serial.println("Back button click"); bleMouse.click(MOUSE_BACK); delay(500); Serial.println("Forward button click"); bleMouse.click(MOUSE_FORWARD); delay(500); Serial.println("Click left+right mouse button at the same time"); bleMouse.click(MOUSE_LEFT | MOUSE_RIGHT); delay(500); Serial.println("Click left+right mouse button and scroll wheel at the same time"); bleMouse.click(MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE); delay(500); } }
鼠标滑动和点击样例2
/** * 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()) { unsigned long startTime; Serial.println("Scroll up"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,0,1); delay(100); } delay(500); Serial.println("Scroll down"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,0,-1); delay(100); } delay(500); Serial.println("Scroll left"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,0,0,-1); delay(100); } delay(500); Serial.println("Scroll right"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,0,0,1); delay(100); } delay(500); Serial.println("Move mouse pointer up"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,-1); delay(100); } delay(500); Serial.println("Move mouse pointer down"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(0,1); delay(100); } delay(500); Serial.println("Move mouse pointer left"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(-1,0); delay(100); } delay(500); Serial.println("Move mouse pointer right"); startTime = millis(); while(millis()<startTime+2000) { bleMouse.move(1,0); delay(100); } delay(500); } }