Android Bluetooth: Get UUIDs of discovered devices

后端 未结 6 1631
悲哀的现实
悲哀的现实 2020-12-01 08:15

As I\'m currently working on a little bluetooth library for Android, I\'m trying to get all the service uuids of the devices I discovered in my surrounding.

When my

相关标签:
6条回答
  • 2020-12-01 08:23

    Here is a good example of how to get UUIDs of service characteristics from a service that I did for getting heart rate devices:

    private class HeartRateBluetoothGattCallback extends BluetoothGattCallback {
    
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {         
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                logMessage("CONNECTED TO " + gatt.getDevice().getName(), false, false);
                gatt.discoverServices();    
            } else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
                logMessage("DISCONNECTED FROM " + gatt.getDevice().getName(), false, false);
                if(mIsTrackingHeartRate)
                    handleHeartRateDeviceDisconnection(gatt);
            } 
        }
    
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                logMessage("DISCOVERING SERVICES FOR " + gatt.getDevice().getName(), false, false);
    
                if(mDesiredHeartRateDevice != null && 
                        gatt.getDevice().getAddress().equals(mDesiredHeartRateDevice.getBLEDeviceAddress())) {
    
                    if(subscribeToHeartRateGattServices(gatt)) {
    
                        mIsTrackingHeartRate = true;
                        setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.CONNECTED);
                        broadcastHeartRateDeviceConnected(gatt.getDevice());
    
                    } else
                        broadcastHeartRateDeviceFailedConnection(gatt.getDevice());
    
                } else {
                    parseGattServices(gatt);
                    disconnectGatt(getDiscoveredBLEDevice(gatt.getDevice().getAddress()));
                }
            }   
        }
    
        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            if(characteristic.getUuid().equals(UUID.fromString(HEART_RATE_VALUE_CHAR_READ_ID))) {
                int flag = characteristic.getProperties();
                int format = -1;
    
                if ((flag & 0x01) != 0) 
                    format = BluetoothGattCharacteristic.FORMAT_UINT16;
                else 
                    format = BluetoothGattCharacteristic.FORMAT_UINT8;
    
                Integer heartRateValue = characteristic.getIntValue(format, 1);
                if(heartRateValue != null)
                    broadcastHeartRateValue(heartRateValue);
                else
                    Log.w(SERVICE_NAME, "UNABLE TO FORMAT HEART RATE DATA");
            }
        };
    
    };
    
    private void parseGattServices(BluetoothGatt gatt) {
        boolean isHeartRate = false;
        for(BluetoothGattService blueToothGattService : gatt.getServices()) {
            logMessage("GATT SERVICE: " + blueToothGattService.getUuid().toString(), false, false);
            if(blueToothGattService.getUuid().toString().contains(HEART_RATE_DEVICE_SERVICE_CHARACTERISTIC_PREFIX))
                isHeartRate = true;
        }   
    
        if(isHeartRate) {
            setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.IS_HEART_RATE);
            broadcastHeartRateDeviceFound(getDiscoveredBLEDevice(gatt.getDevice().getAddress()));
        } else 
            setDeviceScanned(getDiscoveredBLEDevice(gatt.getDevice().getAddress()), DiscoveredBLEDevice.NOT_HEART_RATE);
    }
    
    private void handleHeartRateDeviceDisconnection(BluetoothGatt gatt) {
        broadcastHeartRateDeviceDisconnected(gatt.getDevice());
        gatt.close();
    
        clearoutHeartRateData();
        scanForHeartRateDevices();
    }
    
    private void disconnectGatt(DiscoveredBLEDevice device) {
        logMessage("CLOSING GATT FOR " + device.getBLEDeviceName(), false, false);
        device.getBlueToothGatt().close();
        device.setBlueToothGatt(null);
        mInDiscoveryMode = false;
    }
    
    private boolean subscribeToHeartRateGattServices(BluetoothGatt gatt) {
        for(BluetoothGattService blueToothGattService : gatt.getServices()) {
            if(blueToothGattService.getUuid().toString().contains(HEART_RATE_DEVICE_SERVICE_CHARACTERISTIC_PREFIX)) {
                mHeartRateGattService = blueToothGattService;
    
                for(BluetoothGattCharacteristic characteristic : mHeartRateGattService.getCharacteristics()) {
                    logMessage("CHARACTERISTIC UUID = " + characteristic.getUuid().toString(), false, false);
    
                    for(BluetoothGattDescriptor descriptor :characteristic.getDescriptors()) {
                        logMessage("DESCRIPTOR UUID = " + descriptor.getUuid().toString(), false, false);
                    }
    
                    if(characteristic.getUuid().equals(UUID.fromString(HEART_RATE_VALUE_CHAR_READ_ID))) {
                        gatt.setCharacteristicNotification(characteristic, true);
                        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(HEART_RATE_VALUE_CHAR_DESC_ID));
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                        return gatt.writeDescriptor(descriptor);
                    }
                }
    
                break; //break out of master for-loop
            }
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 08:27

    The documentation on this states...

    Always contains the extra field BluetoothDevice.EXTRA_UUID

    However, just like you, I have found this not to be true.

    If you call fetchUuidsWithSdp() while device discovery is still taking place BluetoothDevice.EXTRA_UUID can be null.

    You should wait until you receive BluetoothAdapter.ACTION_DISCOVERY_FINISHED before you make any calls to fetchUuidsWithSdp().

    0 讨论(0)
  • 2020-12-01 08:31

    I suppose you need to be paired with the device in order to receive the uuids. At least, this is what happened to me.

    0 讨论(0)
  • 2020-12-01 08:41

    NOTE: This solution applies to CLASSIC bluetooth and not BLE. For BLE check how to send manufacturer specific Data in advertiser on the peripheral side

    The problem with fetching Uuids is that you have only one bluetooth adapter, and we cannot have parallel api calls which uses adapter for its purpose.

    As Eddie pointed out, wait for BluetoothAdapter.ACTION_DISCOVERY_FINISHED and then call fetchUuidsWithSdp().

    Still this cannot guarantee uuids to be fetched for all devices. In addition to this one must wait for each subsequent call to fetchuuidsWithSdp() to complete, and then give a call to this method for another device.

    See the code below --

    ArrayList<BluetoothDevice> mDeviceList = new ArrayList<BluetoothDevice>();
    
    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
    
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                mDeviceList.add(device);
            } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
                // discovery has finished, give a call to fetchUuidsWithSdp on first device in list.
                if (!mDeviceList.isEmpty()) {
                    BluetoothDevice device = mDeviceList.remove(0);
                    boolean result = device.fetchUuidsWithSdp();
                }
            } else if (BluetoothDevice.ACTION_UUID.equals(action)) {
                // This is when we can be assured that fetchUuidsWithSdp has completed.
                // So get the uuids and call fetchUuidsWithSdp on another device in list
    
                BluetoothDevice deviceExtra = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                Parcelable[] uuidExtra = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                System.out.println("DeviceExtra address - " + deviceExtra.getAddress());
                if (uuidExtra != null) {
                    for (Parcelable p : uuidExtra) {
                        System.out.println("uuidExtra - " + p);
                    }
                } else {
                    System.out.println("uuidExtra is still null");
                }
                if (!mDeviceList.isEmpty()) {
                    BluetoothDevice device = mDeviceList.remove(0);
                    boolean result = device.fetchUuidsWithSdp();
                }
            }
        }
    }
    

    UPDATE: Latest android versions (mm & above) would result in triggering a pairing process with each device

    0 讨论(0)
  • 2020-12-01 08:45

    Below worked for me to fetch the records from the remote device

    -0-
    registerReceiver(..,
                    new IntentFilter(BluetoothDevice.ACTION_UUID));
    

    -1- device.fetchUuidsWithSdp();

    -2-from within the broadcase receiver

       if (BluetoothDevice.ACTION_UUID.equals(action)) {
                            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                            Parcelable[] uuids = intent.getParcelableArrayExtra(BluetoothDevice.EXTRA_UUID);
                            for (Parcelable ep : uuids) {
                                Utilities.print("UUID records : "+ ep.toString());
                            }
                        }
    

    You can also fetch the offline cached UUID records with

     BluetoothDevice.getUuids();
    
    0 讨论(0)
  • device.getUuids() use this to get all the uuid of that paired device in the form of ParcelUuid; Example code below:-

    private void get_uuid_from_paired_devices(){
            Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
            for (BluetoothDevice device: pairedDevices){
    
                for (ParcelUuid uuid: device.getUuids()){
                    String uuid_string = uuid.toString();
                    Log.d(TAG, "uuid : "+uuid_string);
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题