I want to connect a esp32 to a bluetooth handheld barcode scanner(The ones we see in grocery stores). The scanner connects to my smartphone easily, but cant establish a connection with the esp32. I can see the barcode scanner listed every time in the list of available connections. But while pairing the output always is "Failed to connect to the barcode scanner".
I am okay with hardcoding the scanner address and have tried doing that, but the connection fails every time. I dont think i need to code for BLE as the scanner connects to my smartphone easily.
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
#define BT_DISCOVER_TIME 5000
static bool btScanSync = true;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); // Bluetooth device name
Serial.println("The device started, now you can pair it with Bluetooth!");
if (btScanSync) {
Serial.println("Starting discover...");
BTScanResults *pResults = SerialBT.discover(BT_DISCOVER_TIME);
if (pResults) {
pResults->dump(&Serial);
delay(1000); // Introduce a delay after discovery (adjust as needed)
} else {
Serial.println("Error on BT Scan, no result!");
}
}
String barcodeScannerAddress = "aa:fe:45:2e:0e:39";
if (SerialBT.connect(barcodeScannerAddress)) {
Serial.println("Connected to the barcode scanner!");
} else {
Serial.println("Failed to connect to the barcode scanner.");
}
}
void loop() {
delay(100);
}```
The version of
connect()you're calling takes a name, not an address. The address form connects like this:What do you mean by "connects" when you describe connecting to your phone? Is it over SPP (which is what you're using here)? Or is it some other protocol? If it's HID, then that's completely unrelated to what you've written here.
SPP is a serial port profile. It is classic-only and is not usable by iPhone apps (it is very popular on Android, however).
HID is for things like mice and keyboards. Barcode scanners often pretend to be a keyboard, so you need to treat it like one if that's how this scanner works. You would set this up as though it were a bluetooth keyboard.