Is there any library available for Arduino Nano BLE to emulate it as an HID device over BLE?
I tried writing my own code using the ArduinoBLE library, but it is not being recognized as a mouse.
I also found out that currently, there is no support for pairing in this library. Due to this, nano BLE won't be able to connect to Windows PC. Are there any updates planned to add this feature in the upcoming releases?
Any help would be greatly appreciated.
Thank you.
#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>
float ax, ay, az; // accelerometer values
//Defining HID Serivce
BLEService hidService("1812"); // declare it as an HID device
BLECharCharacteristic bootModeProtocol("2A4e", BLERead | BLEWriteWithoutResponse);
//BLECharacteristic bootMouseReport("2A33", BLERead | BLENotify,3);//Seting up the mouse specific characteristic
BLECharacteristic hidInformation("2A4A",BLERead, 4 );
BLECharacteristic reportMap("2A4B", BLERead,50);
BLECharCharacteristic controlPoint("2A4C",BLEWriteWithoutResponse);
BLECharacteristic mouseReport("2A4D", BLERead | BLENotify, 3); //the report value
//Defining Battery Service
BLEService batteryService("180F");
BLECharCharacteristic batteryLevel("2A19", BLERead | BLENotify);
//Device Information Service
BLEService deviceInformation("180A");
BLECharacteristic manufacturer("2a29", BLERead, 10);
BLECharacteristic pnpID("2A50", BLERead, 7);
unsigned char bleValue[3] = {0x00, 0x00, 0x00};
unsigned char reportMapValue[50] = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0xA1, 0x00, // COLLECTION (Physical)
0x85, 0x00, // REPORT_ID
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM
0x29, 0x03, // USAGE_MAXIMUM
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x05, // REPORT_SIZE (5)
0x81, 0x03, // INPUT (Const,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x02, // REPORT_COUNT (2)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xC0, // END_COLLECTION
0xC0 // END COLLECTION
};
const unsigned char bootMode = 0x00;
const unsigned char batteryDummy = 0x63;
unsigned char hidInfo[3] = {0x4a, 0x00, 0xc0};
unsigned char pnp[4] = {0x02, 0x00, 0x00, 0x12};
unsigned char man[6] = {0x68, 0x61, 0x72, 0x73, 0x68, 0x61};
float prevspeedx;
float prevspeedy;
float currentspeedx;
float currentspeedy;
byte movex;
byte movey;
void setup() {
Serial.begin(9600); // initialize Serial communication
while (!Serial); // wait for the serial port to open
// initialize device
Serial.println("Initializing IMU device...");
IMU.begin();
// verify connection
Serial.println("Testing device connections...");
if (IMU.begin()) {
Serial.println("CurieIMU connection successful");
} else {
Serial.println("CurieIMU connection failed");
}
if (!BLE.begin()) {
//Serial.println("starting BLE failed!");
while (1);
}
// configure Arduino LED for activity indicator
pinMode(LED_BUILTIN, OUTPUT);
// Add service name and UUID for bluetooth
BLE.setLocalName("accMouse");
BLE.setAdvertisedServiceUuid(hidService.uuid());
// Add services and characteristics
//HID Service and Characteristics
//hidService.addCharacteristic(bootMouseReport);
Serial.println("bootMouseReport added");
hidService.addCharacteristic(bootModeProtocol);
hidService.addCharacteristic(hidInformation);
hidService.addCharacteristic(reportMap);
hidService.addCharacteristic(controlPoint);
hidService.addCharacteristic(mouseReport);
BLE.addService(hidService);
Serial.println("adding HID Service");
//Battery service and characteristic
batteryService.addCharacteristic(batteryLevel);
BLE.addService(batteryService);
//Device Information
deviceInformation.addCharacteristic(pnpID);
BLE.addService(deviceInformation);
// Setting the initial value for our mouse input
bootModeProtocol.writeValue('0');
//bootMouseReport.writeValue(bleValue,3);
Serial.print("boot mouse report value: ");
Serial.print(bleValue[0]);
Serial.print(bleValue[1]);
Serial.print(bleValue[2]);
batteryLevel.writeValue(batteryDummy);
hidInformation.writeValue(hidInfo, 4);
pnpID.writeValue(pnp, 7);
reportMap.writeValue(reportMapValue,50);
mouseReport.writeValue(bleValue,3);
manufacturer.writeValue(man, 10);
BLE.setAppearance(962);
BLE.advertise();
}
void loop() {
// look for central device to connect
BLEDevice central = BLE.central();
BLE.poll();
// if a central is connected then we start to do things
if (central) {
while (central.connected()) {
// read raw accel/gyro measurements from device
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(ax, ay, az);
digitalWrite(LED_BUILTIN, HIGH);
bleValue[1] = movex;
bleValue[2] = movey;
//mouseReport.writeValue(bleValue,3);
}
}
digitalWrite(LED_BUILTIN, LOW);
}
}