ESP32 cant connect to a bluetooth handheld barcode scanner

118 Views Asked by At

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);
}```
2

There are 2 best solutions below

2
Rob Napier On

The version of connect() you're calling takes a name, not an address. The address form connects like this:

uint8_t address[6]  = {0xAA, 0xFE, 0x45, 0x2E, 0x0E, 0x39}
bool connected = SerialBT.connect(address);

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.

2
Risto On

Bluetooth is used today in two non-compatible variants, "Bluetooth Classic" and "Bluetooth Low Energy". You should make sure that you are using the same variant on both connection sides.

Another explanation for this problem could be the protocol used, as already mentioned by @kunif. All scanners I know use the HID protocol by default and not the "Serial Port" emulation.

The last thing I can think of is that the scanner could automatically connect to a device to which it has already been connected once. In such a case, it may help to reset the scanner to the factory settings and then reconnect it to the second device.

A look at the manual of the scanner (btw which one?) should give you the necessary information and let you solve the problem.