Android Bluetooth: Get UUIDs of discovered devices

后端 未结 6 1630
悲哀的现实
悲哀的现实 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;
    }
    

提交回复
热议问题