UGandhi
UGandhi

Reputation: 579

Unable to get central Device Name via Web-Bluetooth API

I am trying to fetch the central device name where central is Javascript based web-app and pheripheral is Nano ble 33.

I am using web-bluetooth API and I am able to get pheripheral device name following this enter link description here

and on the other hand, I am able to get the central address using central.address() but I am getting nothing when I am using central.deviceName() or central.localName().

I am wondering where should I setDeviceName() in central. Is it required ?

Thanks in advance.

Upvotes: 0

Views: 373

Answers (1)

François Beaufort
François Beaufort

Reputation: 5629

According to arduino example at https://docs.arduino.cc/retired/archived-libraries/CurieBLE#example-1, it seems like you should be able to get the BLECentral with the code below:

void loop() {
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());
  }
}

However, BLECentral does not allow you get the central device name information according to https://github.com/arduino/ArduinoCore-arc32/blob/master/libraries/CurieBLE/src/BLECentral.h

class BLECentral {
  public:
    bool connected(void); // is the central connected
    
    const char* address(void) const; // address of the Central in string form

    bool disconnect(void); // Disconnect the central if it is connected
    void poll(void); // Poll the central for events
...

I'd suggest you use the address and use "Central 00:11:22:33:44:55" for the name if that's required for you.

To be clear, Web Bluetooth does not allow you to get the Central device name. It allows web pages running in the Central role, to connect to GATT Servers over either a Bluetooth connection.

Having said that, nothing prevents you though to have a custom Bluetooth Characteristic on your BLE peripheral that returns the device address of the Central as a value so that you can access it from the web page. See JS example below:

const yourServiceUuid = 0x1234;
const yourCharacteristicUuid = 0x5678;

const device = await navigator.bluetooth.requestDevice({
    // filters: [...] <- Prefer filters to save energy & show relevant devices.
    acceptAllDevices: true,
    optionalServices: [yourServiceUuid]});
const server = await device.gatt.connect();
const service = await server.getPrimaryService(yourServiceUuid);
const characteristic = await service.getCharacteristic(yourCharacteristicUuid);

const value = await characteristic.readValue();
const decoder = new TextDecoder('utf-8');
console.log(`> Central name: "Central ${decoder.decode(value)}"`);

Upvotes: 1

Related Questions