Ble Scan service of a device without connecting to

2019-02-26 13:59发布

问题:

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 ?

回答1:

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

The characteristics Device Name, UUID=0x2A00 is bundled into GAP (Generic Access), UUID = 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.

Adressing 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.

Kindly refer Bluetooth Development Portal for more details.