Bluetooth joystick mouse

On windows the device is detected as a mouse but when i connect to it it gives driver error(the windows pc) and disconnects

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#define JOYSTICK_X_PIN 32
#define JOYSTICK_Y_PIN 33
#define JOYSTICK_THRESHOLD 10

#define HID_SERVICE_UUID "1812"
#define HID_INFORMATION_UUID "2A4A"
#define HID_REPORT_MAP_UUID "2A4B"
#define HID_INPUT_REPORT_UUID "2A4D"

BLECharacteristic *inputCharacteristic;
bool isConnected = false;

void setupJoystick()
{
  pinMode(JOYSTICK_X_PIN, INPUT);
  pinMode(JOYSTICK_Y_PIN, INPUT);
}

void setupBLE(); // Function prototype for setupBLE()

void setupBLE()
{
  class MyServerCallbacks : public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      isConnected = true;
      Serial.println("Connected to device!");
    }

    void onDisconnect(BLEServer* pServer) {
      isConnected = false;
      Serial.println("Disconnected from device!");
    }
  };

  BLEDevice::init("JoystickMouse");

  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pHIDService = pServer->createService(BLEUUID(HID_SERVICE_UUID));

  inputCharacteristic = pHIDService->createCharacteristic(
      BLEUUID(HID_INPUT_REPORT_UUID),
      BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY
  );
  inputCharacteristic->addDescriptor(new BLE2902());

  pServer->setCallbacks(new MyServerCallbacks()); // Set server callbacks

  pHIDService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->setAppearance(0x03C2); // HID Mouse appearance (0x03C2)
  pAdvertising->addServiceUUID(BLEUUID(HID_SERVICE_UUID));
  pAdvertising->start();
}

void moveMouse(int8_t x, int8_t y)
{
  uint8_t mouseData[3];
  mouseData[0] = 0x00; // Button: None
  mouseData[1] = x;    // X-axis movement
  mouseData[2] = y;    // Y-axis movement
  inputCharacteristic->setValue(mouseData, sizeof(mouseData));
  inputCharacteristic->notify();
}

void readJoystick()
{
  int xVal = analogRead(JOYSTICK_X_PIN);
  int yVal = analogRead(JOYSTICK_Y_PIN);

  if (abs(xVal - 512) > JOYSTICK_THRESHOLD || abs(yVal - 512) > JOYSTICK_THRESHOLD)
  {
    int8_t x = map(xVal, 0, 4095, -127, 127);
    int8_t y = map(yVal, 0, 4095, -127, 127);

    moveMouse(x, y);
  }
}

void setup()
{
  Serial.begin(115200);
  setupJoystick();
  setupBLE();
}

void loop()
{
  if (isConnected)
  {
    readJoystick();
  }

  delay(50);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.