Search code examples
javabluetoothbluetooth-lowenergydeviceandroid-bluetooth

Ble Scan service of a device without connecting to it


I want to scan some device with BLE. I only want to show my device, so for know I get the name of the device and if it is the good one I put it in my list.

if (device.getName().contains(DEVICE_NAME)) {
        mDevices.put(device.hashCode(), device);
        invalidateOptionsMenu();
    }

My problem is that if I change the name of my device this check will be false. So I look if it was possible to get some uuid of some services that I add to do the check with something that would not change. And the only way is to connect to the device doing a device.connectGatt(this, false, mGattCallback); and after with the gatt I discover service but, is it possible to discover some services from a device without connectiong to it ?


Solution

  • I am not with the android background, but I have understanding of how BLE works.

    Take a look at all standard GATT characteristics here and all the standard GATT services here.
    The characteristic Device Name (UUID is 0x2A00) is bundled into Generic Access service (UUID is 0x1800). The GAP is always broadcasted within the advertisement packet from the GATT Server.
    A GATT Client should run a discovery scan to receive the advertisement and parse the advertisement packet to receive the GAP descriptor. While you are doing this, technically speaking the connection is yet to be made.

    Addressing to:

    If I change the name of my device this check will be false, So I look if it was possible to get some uuid of some services that I add to do the check with something that would not change.

    Assuming that you want your app to connect only to a particular set of device that has some unique identification. Say for example, a BLE-enabled digital pen configured by you should only connect to your app and no other device gadget should be able to connect with your app. If that is the use case and if you have the liberty to configure the GATT server on the BLE device then you can add some custom characteristics in GAP service on GATT server which will be unique to those devices. Needless to say, you will have to generate a 128-bit custom UUID for that characteristics and the UUID will be known to your application as well. This is more like a work-around solution.

    Take a look at scan filter in android. It allows you to scan by service UUID, which needs to be sent through advertisement by BLE peripheral. I haven't worked on android, but I am certain that the ScanFilter would allow you to to filter by either a 16-bit or a 128-but UUID.

    Kindly refer Bluetooth Development Portal for more details.