Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac OS X Pair Workaround #42

Open
dsaul opened this issue Jul 17, 2022 · 8 comments
Open

Mac OS X Pair Workaround #42

dsaul opened this issue Jul 17, 2022 · 8 comments

Comments

@dsaul
Copy link

dsaul commented Jul 17, 2022

In case it is relevant for anyone else, I was able to work around Mac OS X not pairing by not sending any commands for 5-10 seconds after it connected. This could likely be tuned lower, but is good for my use case.

Example:

unsigned long connectedMillis = 0; // delay actions until after connected for 10 seconds, because apple is dumb
// ...
BleMouse bleMouse;


void setup()
{
  // ...
}

void loop()
{
  // Start, apple is dumb.
  if(bleMouse.isConnected()) {
    if (connectedMillis == 0) {
      connectedMillis = millis();
    }
    
  }
  
  if (millis() < connectedMillis + (10 * 1000)) {
    return;
  }
  // End, apple is dumb.
  
  // ... 
}

Regards,
Dan

@jslay88
Copy link

jslay88 commented Jan 4, 2023

Doesn't seem to work for me. Sadly, because I had this working (not with the delay, just the library in general), for over a year with this Mac. Now, today out of no where, I cannot seem to get it to pair, no matter what I do.

#include <Arduino.h>
#include <BleMouse.h>

BleMouse bleMouse("Apple Magic Mouse", "Apple, Inc.", 74);
bool enable = true;
unsigned long lastMove = millis();
unsigned long currentTime = millis();
unsigned long connectedTime = 0;
unsigned long moveDelay = 5000;
signed int moveDirection = 1;

void setup() {
    Serial.begin(115200);
    Serial.println("Booting...");
    Serial.println("Starting BLE...");
    bleMouse.begin();
}

void loop() {
    currentTime = millis();

    if (!bleMouse.isConnected()) {
        connectedTime = 0;
    }
    if (enable && bleMouse.isConnected() && currentTime - lastMove >= moveDelay) {
        if (connectedTime == 0) {
            connectedTime = currentTime;
        }
        if (currentTime > connectedTime + (10 * 1000)) {
            bleMouse.move(0, moveDirection, 0);
            moveDirection = moveDirection * -1;
            lastMove = currentTime;
            Serial.println("Move Mouse!");
        }
    }
}

@dsaul
Copy link
Author

dsaul commented Jan 4, 2023

I found the Mac pairing screen had to be open as well. shrug

@jslay88
Copy link

jslay88 commented Jan 4, 2023 via email

@dsaul
Copy link
Author

dsaul commented Jan 4, 2023

Looks like you're making a mouse jiggler, here is the one I made: https://github.com/dsaul/ESP32-BLE-ScreenWakeSentinel maybe it'll help.

@jslay88
Copy link

jslay88 commented Jan 5, 2023

Yeah, I don't know what changed with this laptop today. Works with every other device I have except this one Mac, which it has been paired with exclusively for over a year. Cleared all connected devices, no dice still. Ordered some more boards just in case something weird has gone wrong with this one (even though it connects to everything else just fine).

Anyway, here is what I have been running for a while now, with your wait added in today (will need to use full partition to compile this. Ergo, no OTA).

image

#include <Arduino.h>
#include <BleMouse.h>
#include <WiFi.h>


BleMouse bleMouse("Apple Magic Mouse", "Apple, Inc.", 74);
const char* ssid = "your-ssid";
const char* password = "your-wpa-passcode";

bool enable = true;
unsigned long lastMove = millis();
unsigned long currentTime = millis();
unsigned long connectedTime = 0;
unsigned long moveDelay = 5000;
signed int moveDirection = 1;

WiFiServer server(80);
WiFiClient client;
String header;
String currentLine = "";

void setup() {
    Serial.begin(115200);
    Serial.println("Booting...");
    Serial.println("Starting BLE...");
    bleMouse.begin();
    Serial.print("Connecting WiFi...");
    WiFi.begin(ssid, password);
    server.begin();
}

void loop() {
    currentTime = millis();

    if (!bleMouse.isConnected()) {
        connectedTime = 0;
    }
    if (enable && bleMouse.isConnected() && currentTime - lastMove >= moveDelay) {
        if (connectedTime == 0) {
            connectedTime = currentTime;
        }
        if (currentTime > connectedTime + (10 * 1000)) {
            bleMouse.move(0, moveDirection, 0);
            moveDirection = moveDirection * -1;
            lastMove = currentTime;
            Serial.println("Move Mouse!");
        }
    }

    if (WiFi.status() != WL_CONNECTED) {
        Serial.println(".");
    } else if (client) {
        if (client.connected()) {
            if (client.available()) {  // Read bytes from client
                char c = client.read();
                Serial.write(c);
                header += c;
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        // Begin Server Response
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type: text/html");
                        client.println("Connection: close");
                        client.println();

                        if (header.indexOf("POST /") >= 0) {  // Handle change request
                            enable = !enable;
                            if (enable) {
                                bleMouse.move(0, moveDirection * 100, 0);
                                delay(50);
                                bleMouse.move(0, moveDirection * -100, 0);
                            }
                            Serial.print("Enable Toggled: ");
                            Serial.println(enable);
                        }

                        String bleConnectedClass = "red";
                        String enableClass = "green";
                        if (bleMouse.isConnected()) {
                            bleConnectedClass = "green";
                        }
                        if (!enable) {
                            enableClass = "red";
                        }

                        client.println("<!DOCTYPE html><html>");
                        client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
                        client.println("<link rel=\"icon\" href=\"data:,\">"); //  Empty favicon to stop generating requests for it
                        client.println("<style>");
                        client.println("html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; background-color: #212A2E; color: #F7F8F8; }");
                        client.println(".dot { height: 20px; width: 20px; border-radius: 50%; display: inline-block; }");
                        client.println(".green { background-color: lightgreen; }");
                        client.println(".red { background-color: red; }");
                        client.println("</style>");
                        client.println("<body><h1>Mouse Mover</h1>");
                        client.println("<div>");
                        client.println("<p>BLE Connection: <span class=\"dot " + bleConnectedClass + "\"></span></p>");
                        client.println("<p>Enabled: <span class=\"dot " + enableClass + "\"></span></p>");
                        client.println("<form action=\"\" method=\"post\"><button type=\"submit\">Toggle</button></form>");
                        client.println("</div>");
                        client.println("</body>");
                        client.println("</html>");
                        client.println();

                        // Clean up
                        header = "";
                        client.stop();
                        Serial.println("Client Disconnected!");
                        Serial.println();

                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
    } else {
        client = server.available();
    }
}

@jslay88
Copy link

jslay88 commented Jan 5, 2023

I got it working! Pairs flawlessly every time.

I had to switch over to NimBLE, so clearly an issue with the built in library. Didn't need to change any of my code for the transition.

https://github.com/wakwak-koba/ESP32-NimBLE-Mouse

Hope to see this library incorporate NimBLE down the road, as I can now also do OTA updates.

@rossiluis22
Copy link

rossiluis22 commented Feb 15, 2023

I'm facing bluetooth disconnection and not able to connected until I do a reset of the ESP32, also tried in a different board, same issue. Is it working properly on your Mac? @jslay88 , could you please help me to get this sort?

this is what I have:

platformio.ini

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp-wrover-kit]
platform = espressif32
board = esp-wrover-kit
framework = arduino
board_build.partitions = huge_app.csv
upload_port = /dev/tty.usbserial-0001
monitor_port = /dev/tty.usbserial-0001
monitor_speed = 115200
lib_deps = 
	t-vk/ESP32 BLE Mouse@^0.3.1

src/sentinel.ino

#include <Arduino.h>
#include <BleMouse.h>
#include <WiFi.h>


BleMouse bleMouse("Apple Magic Mouse", "Apple, Inc.", 74);
const char* ssid = "ssid";
const char* password = "pass";

bool enable = true;
unsigned long lastMove = millis();
unsigned long currentTime = millis();
unsigned long connectedTime = 0;
unsigned long moveDelay = 5000;
signed int moveDirection = 1;

WiFiServer server(80);
WiFiClient client;
String header;
String currentLine = "";

void setup() {
    Serial.begin(115200);
    Serial.println("Booting...");
    Serial.println("Starting BLE...");
    bleMouse.begin();
    Serial.print("Connecting WiFi...");
    WiFi.begin(ssid, password);
    server.begin();
}

void loop() {
    currentTime = millis();

    if (!bleMouse.isConnected()) {
        connectedTime = 0;
    }
    if (enable && bleMouse.isConnected() && currentTime - lastMove >= moveDelay) {
        if (connectedTime == 0) {
            connectedTime = currentTime;
        }
        if (currentTime > connectedTime + (10 * 1000)) {
            bleMouse.move(0, moveDirection, 0);
            moveDirection = moveDirection * -1;
            lastMove = currentTime;
            Serial.println("Move Mouse!");
        }
    }

    if (WiFi.status() != WL_CONNECTED) {
        Serial.println(".");
    } else if (client) {
        if (client.connected()) {
            if (client.available()) {  // Read bytes from client
                char c = client.read();
                Serial.write(c);
                header += c;
                if (c == '\n') {
                    if (currentLine.length() == 0) {
                        // Begin Server Response
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type: text/html");
                        client.println("Connection: close");
                        client.println();

                        if (header.indexOf("POST /") >= 0) {  // Handle change request
                            enable = !enable;
                            if (enable) {
                                bleMouse.move(0, moveDirection * 100, 0);
                                delay(50);
                                bleMouse.move(0, moveDirection * -100, 0);
                            }
                            Serial.print("Enable Toggled: ");
                            Serial.println(enable);
                        }

                        String bleConnectedClass = "red";
                        String enableClass = "green";
                        if (bleMouse.isConnected()) {
                            bleConnectedClass = "green";
                        }
                        if (!enable) {
                            enableClass = "red";
                        }

                        client.println("<!DOCTYPE html><html>");
                        client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
                        client.println("<link rel=\"icon\" href=\"data:,\">"); //  Empty favicon to stop generating requests for it
                        client.println("<style>");
                        client.println("html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center; background-color: #212A2E; color: #F7F8F8; }");
                        client.println(".dot { height: 20px; width: 20px; border-radius: 50%; display: inline-block; }");
                        client.println(".green { background-color: lightgreen; }");
                        client.println(".red { background-color: red; }");
                        client.println("</style>");
                        client.println("<body><h1>Mouse Mover</h1>");
                        client.println("<div>");
                        client.println("<p>BLE Connection: <span class=\"dot " + bleConnectedClass + "\"></span></p>");
                        client.println("<p>Enabled: <span class=\"dot " + enableClass + "\"></span></p>");
                        client.println("<form action=\"\" method=\"post\"><button type=\"submit\">Toggle</button></form>");
                        client.println("</div>");
                        client.println("</body>");
                        client.println("</html>");
                        client.println();

                        // Clean up
                        header = "";
                        client.stop();
                        Serial.println("Client Disconnected!");
                        Serial.println();

                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
    } else {
        client = server.available();
    }
}

@jslay88
Copy link

jslay88 commented Feb 19, 2023

Use the NimBLE library I linked above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants