Problems with BLE connection and pairing

EspProblems1996
Posts: 3
Joined: Wed Sep 23, 2020 11:27 pm

Problems with BLE connection and pairing

Postby EspProblems1996 » Wed Sep 23, 2020 11:35 pm

I am making a BLE mouse.
The code that I am using works, but it's very finnicky.
Here is what you have to do in order to connect successfully.
1. Remove device from paired devices (if it's in there)
2. go to add device and select bluetooth
3. find device in list.
4. Reset device, and in the same moment, click it in windows pairing window. (very important)
5 device will now connect.
If you don't press the buttons in the right moment it doesn't work. If you disconnect and then reconnect, the mouse doesn't work, even thought in windows it says that it is connected.
Here is the code that I'm using for the BLE server

Code: Select all

void taskServer(void*){


    BLEDevice::init("ESP32_Omni_Device");
    BLEServer *pServer = BLEDevice::createServer();
    pServer->setCallbacks(new MyCallbacks());

    hid = new BLEHIDDevice(pServer);

    input = hid->inputReport(1); // <-- input REPORTID from report map
    std::string name = "Assist_IoT";
    hid->manufacturer()->setValue(name);

    hid->pnp(0x02, 0xe502, 0xa111, 0x0210);
    hid->hidInfo(0x00,0x01);

  BLESecurity *pSecurity = new BLESecurity();
  pSecurity->setKeySize();
  pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);

    hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor));
    hid->setBatteryLevel(5);
    hid->startServices();

    BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();

    //  The setAppearance function only changes what is passed in the adv packet...it does not set the communication protocol
    //pAdvertising->setAppearance(HID_KEYBOARD);
    //pAdvertising->setAppearance(HID_JOYSTICK);
    pAdvertising->setAppearance(HID_MOUSE);
    pAdvertising->addServiceUUID(hid->hidService()->getUUID());
    BLEDevice::startAdvertising();

    Serial.print("Advertising started!");
    vTaskDelay(portMAX_DELAY);
  
};
I call this code in the beginning of the setup function

Code: Select all

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
        Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    xTaskCreate(taskServer, "server", 20000, NULL, 5, NULL);
  /*.....  Uninmportant code ommited*/

}
After this code finishes, the main loop starts.
I am pretty new to this and don't have any idea where to even start, so any suggestion is appreciated.
Most of the past 2 days has been spent working with HID Descriptors and how to use them.

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: Problems with BLE connection and pairing

Postby mikemoy » Sat Sep 26, 2020 4:31 am

Just curious, if your only calling taskServer once in setup(). Why make it a task? why not just a function or put that code in startup().
If your using it other places ok, but then your implementation of how to use a thread needs some work.

EspProblems1996
Posts: 3
Joined: Wed Sep 23, 2020 11:27 pm

Re: Problems with BLE connection and pairing

Postby EspProblems1996 » Sat Sep 26, 2020 7:05 pm

mikemoy wrote:
Sat Sep 26, 2020 4:31 am
Just curious, if your only calling taskServer once in setup(). Why make it a task? why not just a function or put that code in startup().
If your using it other places ok, but then your implementation of how to use a thread needs some work.
"Because it was like that in the example I found" is my answer I'm afraid.
I don't really understand how it works. Does BLEDevice::startAdvertising(); last only for a short amount of time ?
What I'm doing now is essentially calling it once and never again I get what you're saying.
What do i need to do different if for example, i want to be able to pair and connect without restarting?
Call the bt pairing mode and then wait for a certain amount of time?
Isn't the bt controller on the esp32 standalone?

Who is online

Users browsing this forum: Google Adsense [Bot] and 78 guests