Example usage for android.bluetooth BluetoothGatt discoverServices

List of usage examples for android.bluetooth BluetoothGatt discoverServices

Introduction

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

Prototype

public boolean discoverServices() 

Source Link

Document

Discovers services offered by a remote device as well as their characteristics and descriptors.

Usage

From source file:com.megster.cordova.ble.central.Peripheral.java

@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

    this.gatt = gatt;

    if (newState == BluetoothGatt.STATE_CONNECTED) {

        connected = true;/*from  www  . j av a2 s .  c o m*/
        gatt.discoverServices();

    } else {

        if (connectCallback != null) {
            connectCallback.error(this.asJSONObject());
        }
        disconnect();
    }

}

From source file:com.wolkabout.hexiwear.service.BluetoothService.java

private void createGATT(final BluetoothDevice device) {
    bluetoothGatt = device.connectGatt(this, true, new BluetoothGattCallback() {
        @Override//w  w  w.j  a  v a2s . com
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            isConnected = BluetoothProfile.STATE_CONNECTED == newState;
            if (isConnected) {
                Log.i(TAG, "GATT connected.");
                startForeground(442, getNotification(device));
                gatt.discoverServices();
            } else {
                Log.i(TAG, "GATT disconnected.");
                NotificationService_.intent(BluetoothService.this).stop();
                notificationManager.notify(442, getNotification(device));
                gatt.connect();
            }

            final Intent connectionStateChanged = new Intent(CONNECTION_STATE_CHANGED);
            connectionStateChanged.putExtra(CONNECTION_STATE, isConnected);
            sendBroadcast(connectionStateChanged);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            Log.i(TAG, "Services discovered.");
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            discoverCharacteristics(gatt);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            Log.i(TAG, "Characteristic written: " + status);

            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final byte command = characteristic.getValue()[0];
            switch (command) {
            case WRITE_TIME:
                Log.i(TAG, "Time written.");
                final BluetoothGattCharacteristic batteryCharacteristic = readableCharacteristics
                        .get(Characteristic.BATTERY.getUuid());
                gatt.setCharacteristicNotification(batteryCharacteristic, true);
                for (BluetoothGattDescriptor descriptor : batteryCharacteristic.getDescriptors()) {
                    if (descriptor.getUuid().toString().startsWith("00002904")) {
                        descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                        gatt.writeDescriptor(descriptor);
                    }
                }
                break;
            case WRITE_NOTIFICATION:
                Log.i(TAG, "Notification sent.");
                if (notificationsQueue.isEmpty()) {
                    Log.i(TAG, "Reading characteristics...");
                    readNextCharacteristics(gatt);
                } else {
                    Log.i(TAG, "writing next notification...");
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }
                break;
            default:
                Log.w(TAG, "No such ALERT IN command: " + command);
                break;
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            readCharacteristic(gatt, Characteristic.MANUFACTURER);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                final BluetoothGattCharacteristic gattCharacteristic, int status) {
            if (status == BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION) {
                handleAuthenticationError(gatt);
                return;
            }

            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            switch (characteristic) {
            case MANUFACTURER:
                manufacturerInfo.manufacturer = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.FW_REVISION);
                break;
            case FW_REVISION:
                manufacturerInfo.firmwareRevision = gattCharacteristic.getStringValue(0);
                readCharacteristic(gatt, Characteristic.MODE);
                break;
            default:
                Log.v(TAG, "Characteristic read: " + characteristic.name());
                if (characteristic == Characteristic.MODE) {
                    final Mode newMode = Mode.bySymbol(gattCharacteristic.getValue()[0]);
                    if (mode != newMode) {
                        onModeChanged(newMode);
                    }
                } else {
                    onBluetoothDataReceived(characteristic, gattCharacteristic.getValue());
                }

                if (notificationsQueue.isEmpty()) {
                    readNextCharacteristics(gatt);
                } else {
                    alertIn.setValue(notificationsQueue.poll());
                    gatt.writeCharacteristic(alertIn);
                }

                break;
            }
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                BluetoothGattCharacteristic gattCharacteristic) {
            final String characteristicUuid = gattCharacteristic.getUuid().toString();
            final Characteristic characteristic = Characteristic.byUuid(characteristicUuid);
            Log.d(TAG, "Characteristic changed: " + characteristic);

            if (characteristic == Characteristic.BATTERY) {
                onBluetoothDataReceived(Characteristic.BATTERY, gattCharacteristic.getValue());
            }
        }
    });
}

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

public void discoveryServices(String address) {
    if (StringUtils.isEmptyString(address)) {
        Log.w(TAG, "Unknown address");
    }//from  w ww.jav  a2 s  .com
    BluetoothGatt bluetoothGatt = mConnectedBluetoothGattMap.get(address);
    if (bluetoothGatt != null) {
        bluetoothGatt.discoverServices();
    }
}

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

@NonNull
@Override/*w  w  w.j  av a  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());
}