Example usage for android.bluetooth BluetoothGatt getServices

List of usage examples for android.bluetooth BluetoothGatt getServices

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGatt getServices.

Prototype

public List<BluetoothGattService> getServices() 

Source Link

Document

Returns a list of GATT services offered by the remote device.

Usage

From source file:org.bcsphere.bluetooth.BluetoothG43plus.java

private void getDeviceAllDataManage(BluetoothGatt gatt, int status) {
    String deviceAddress = getDeviceAddress(gatt);
    if (getDeviceAllDataCC.get(deviceAddress) != null) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            JSONObject obj = new JSONObject();
            JSONArray servicesInfo = new JSONArray();
            List<BluetoothGattService> services = gatt.getServices();
            for (int i = 0; i < services.size(); i++) {
                JSONObject serviceInfo = new JSONObject();
                Tools.addProperty(serviceInfo, Tools.SERVICE_INDEX, i);
                Tools.addProperty(serviceInfo, Tools.SERVICE_UUID, services.get(i).getUuid());
                Tools.addProperty(serviceInfo, Tools.SERVICE_NAME, Tools.lookup(services.get(i).getUuid()));
                List<BluetoothGattCharacteristic> characteristics = services.get(i).getCharacteristics();
                JSONArray characteristicsInfo = new JSONArray();
                for (int j = 0; j < characteristics.size(); j++) {
                    JSONObject characteristicInfo = new JSONObject();
                    Tools.addProperty(characteristicInfo, Tools.CHARACTERISTIC_INDEX, j);
                    Tools.addProperty(characteristicInfo, Tools.CHARACTERISTIC_UUID,
                            characteristics.get(j).getUuid());
                    Tools.addProperty(characteristicInfo, Tools.CHARACTERISTIC_NAME,
                            Tools.lookup(characteristics.get(j).getUuid()));
                    Tools.addProperty(characteristicInfo, Tools.CHARACTERISTIC_PROPERTY,
                            Tools.decodeProperty(characteristics.get(j).getProperties()));
                    List<BluetoothGattDescriptor> descriptors = new ArrayList<BluetoothGattDescriptor>();
                    JSONArray descriptorsInfo = new JSONArray();
                    for (int k = 0; k < descriptors.size(); k++) {
                        JSONObject descriptorInfo = new JSONObject();
                        Tools.addProperty(descriptorInfo, Tools.DESCRIPTOR_INDEX, k);
                        Tools.addProperty(descriptorInfo, Tools.DESCRIPTOR_UUID, descriptors.get(k).getUuid());
                        Tools.addProperty(descriptorInfo, Tools.DESCRIPTOR_NAME,
                                Tools.lookup(descriptors.get(k).getUuid()));
                        descriptorsInfo.put(descriptorInfo);
                    }// w  w  w. jav  a  2 s. c om
                    Tools.addProperty(characteristicInfo, Tools.DESCRIPTORS, descriptorsInfo);
                    characteristicsInfo.put(characteristicInfo);
                }
                Tools.addProperty(serviceInfo, Tools.CHARACTERISTICS, characteristicsInfo);
                servicesInfo.put(serviceInfo);
            }
            Tools.addProperty(obj, Tools.DEVICE_ADDRESS, deviceAddress);
            Tools.addProperty(obj, Tools.SERVICES, servicesInfo);
            getDeviceAllDataCC.get(deviceAddress).success(obj);
            getDeviceAllDataCC.remove(deviceAddress);
            deviceServices.put(deviceAddress, services);
        } else {
            Tools.sendErrorMsg(getDeviceAllDataCC.get(deviceAddress));
            getDeviceAllDataCC.remove(deviceAddress);
        }
    }
}

From source file:com.nbplus.iotapp.bluetooth.BluetoothLeService.java

/**
 * Retrieves a list of supported GATT services on the connected device. This should be
 * invoked only after {@code BluetoothGatt#discoverServices()} completes successfully.
 *
 * @return A {@code List} of supported services.
 *///from w w w . j a  v a  2s  .  co m
public List<BluetoothGattService> getSupportedGattServices(String address) {
    if (StringUtils.isEmptyString(address)) {
        Log.w(TAG, "Unknown address");
    }
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (bluetoothGatt == null)
        return null;

    return bluetoothGatt.getServices();
}

From source file:is.hello.buruberi.bluetooth.stacks.android.NativeGattPeripheral.java

@NonNull
@Override//from ww  w  .ja va  2 s. c  om
@RequiresPermission(Manifest.permission.BLUETOOTH)
public Observable<Map<UUID, ? extends GattService>> discoverServices(final @NonNull OperationTimeout timeout) {
    final ConnectedOnSubscribe<Map<UUID, ? extends GattService>> onSubscribe = new ConnectedOnSubscribe<Map<UUID, ? extends GattService>>(
            this) {
        @Override
        public void onSubscribe(@NonNull BluetoothGatt gatt,
                @NonNull final Subscriber<? super Map<UUID, ? extends GattService>> subscriber) {
            final Runnable onDisconnect = addTimeoutDisconnectListener(subscriber, timeout);
            setupTimeout(Operation.DISCOVER_SERVICES, timeout, subscriber, onDisconnect);

            gattDispatcher.servicesDiscovered = new ServicesDiscoveredListener() {
                @Override
                public void onServicesDiscovered(@NonNull BluetoothGatt gatt, int status) {
                    timeout.unschedule();

                    removeDisconnectListener(onDisconnect);

                    if (status == BluetoothGatt.GATT_SUCCESS) {
                        NativeGattPeripheral.this.services = NativeGattService.wrap(gatt.getServices(),
                                NativeGattPeripheral.this);
                        subscriber.onNext(services);
                        subscriber.onCompleted();

                        gattDispatcher.servicesDiscovered = null;
                    } else {
                        logger.error(LOG_TAG,
                                "Could not discover services. " + GattException.statusToString(status), null);

                        NativeGattPeripheral.this.services = Collections.emptyMap();

                        subscriber.onError(new GattException(status, Operation.DISCOVER_SERVICES));

                        gattDispatcher.servicesDiscovered = null;
                    }
                }
            };

            if (gatt.discoverServices()) {
                timeout.schedule();
            } else {
                gattDispatcher.servicesDiscovered = null;
                removeDisconnectListener(onDisconnect);

                subscriber.onError(new ServiceDiscoveryException());
            }
        }
    };

    // See <https://code.google.com/p/android/issues/detail?id=58381>
    return createObservable(onSubscribe).delay(SERVICES_DELAY_S, TimeUnit.SECONDS, stack.getScheduler());
}

From source file:com.beestar.ble.ble.ui.MultipleBleActivity.java

private void setBleServiceListener() {
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override//from www.jav a 2 s . co m
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", false);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_CONNECTING) {

            } else if (newState == BluetoothProfile.STATE_CONNECTED) {
                for (int i = 0; i < deviceList.size(); i++) {
                    HashMap<String, Object> devMap = (HashMap<String, Object>) deviceList.get(i);
                    if (devMap.get("address").toString().equals(gatt.getDevice().getAddress())) {
                        ((HashMap) deviceList.get(i)).put("isConnect", true);
                        return;
                    }
                }
            } else if (newState == BluetoothProfile.STATE_DISCONNECTING) {

            }
        }
    });
    //??
    mBleService.setOnServicesDiscoveredListener(new MultipleBleService.OnServicesDiscoveredListener() {
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            String characteristics_uuid_read = null;
            String characteristics_uuid_write = null;
            String read_Uuid = null;
            String write_Uuid = null;
            BluetoothGattCharacteristic characteristic;
            if (status == gatt.GATT_SUCCESS) {
                String address = gatt.getDevice().getAddress();
                List<BluetoothGattService> services = gatt.getServices();
                commonUtils = new CommonUtils(getApplicationContext());
                int j, k;
                for (int i = 0; i < services.size(); i++) {

                    if (services.get(i).getUuid().toString().contains("ffe5")) {
                        write_Uuid = services.get(i).getUuid().toString();
                        Log.i("info", "--------------------" + i);

                        characteristics_uuid_write = services.get(i).getCharacteristics().get(0).getUuid()
                                .toString();

                    }
                    if (services.get(i).getUuid().toString().contains("ffe0")) {
                        Log.i("info", "--------------------" + i);
                        characteristic = services.get(i).getCharacteristic(services.get(i).getUuid());
                        read_Uuid = services.get(i).getUuid().toString();
                        characteristics_uuid_read = services.get(i).getCharacteristics().get(0).getUuid()
                                .toString();

                    }
                }
                Equipment_Device equipment_device = new Equipment_Device();
                equipment_device.setAddress(address);
                equipment_device.setRead_uuid(read_Uuid);
                equipment_device.setRead_uuid(write_Uuid);
                equipment_device.setCh_read_uuid(characteristics_uuid_read);
                equipment_device.setCh_write_uuid(characteristics_uuid_write);
                commonUtils.insertStudent(equipment_device);
            }
        }
    });
    mBleService.setOnConnectListener(new MultipleBleService.OnConnectionStateChangeListener() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

        }
    });
    mBleService.setOnDataAvailableListener(new MultipleBleService.OnDataAvailableListener() {
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {

        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

        }

        @Override
        public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {

        }
    });
}