Example usage for android.bluetooth BluetoothDevice ERROR

List of usage examples for android.bluetooth BluetoothDevice ERROR

Introduction

In this page you can find the example usage for android.bluetooth BluetoothDevice ERROR.

Prototype

int ERROR

To view the source code for android.bluetooth BluetoothDevice ERROR.

Click Source Link

Document

Sentinel error value for this class.

Usage

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

@NonNull
@Override/*from w  w  w . j  av  a2 s .  co m*/
@RequiresPermission(allOf = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, })
public Observable<GattPeripheral> createBond() {
    return createObservable(new Observable.OnSubscribe<GattPeripheral>() {
        @Override
        public void call(final Subscriber<? super GattPeripheral> subscriber) {
            if (getBondStatus() == BOND_BONDED) {
                logger.info(GattPeripheral.LOG_TAG, "Device already bonded, skipping.");

                subscriber.onNext(NativeGattPeripheral.this);
                subscriber.onCompleted();
                return;
            }

            final Subscription subscription = createBondReceiver().subscribe(new Subscriber<Intent>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(Intent intent) {
                    final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
                            BluetoothDevice.ERROR);
                    final int previousState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
                            BluetoothDevice.ERROR);

                    logger.info(GattPeripheral.LOG_TAG,
                            "Bond status changed from " + BondException.getBondStateString(previousState)
                                    + " to " + BondException.getBondStateString(state));

                    if (state == BluetoothDevice.BOND_BONDED) {
                        logger.info(LOG_TAG, "Bonding succeeded.");

                        subscriber.onNext(NativeGattPeripheral.this);
                        subscriber.onCompleted();

                        unsubscribe();
                    } else if (state == BluetoothDevice.ERROR
                            || state == BOND_NONE && previousState == BOND_CHANGING) {
                        final int reason = intent.getIntExtra(BondException.EXTRA_REASON,
                                BondException.REASON_UNKNOWN_FAILURE);
                        logger.error(LOG_TAG,
                                "Bonding failed for reason " + BondException.getReasonString(reason), null);
                        subscriber.onError(new BondException(reason));

                        unsubscribe();
                    }
                }
            });

            if (!BluetoothDeviceCompat.createBond(bluetoothDevice)) {
                subscription.unsubscribe();
                subscriber.onError(new BondException(BondException.REASON_ANDROID_API_CHANGED));
            }
        }
    });
}

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

@NonNull
@Override/* w w  w.  ja va2  s.c om*/
@RequiresPermission(allOf = { Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, })
public Observable<GattPeripheral> removeBond(final @NonNull OperationTimeout timeout) {
    return createObservable(new Observable.OnSubscribe<GattPeripheral>() {
        @Override
        public void call(final Subscriber<? super GattPeripheral> subscriber) {
            if (getBondStatus() != BOND_BONDED) {
                logger.info(GattPeripheral.LOG_TAG, "Device not bonded, skipping.");

                subscriber.onNext(NativeGattPeripheral.this);
                subscriber.onCompleted();
                return;
            }

            final Subscription subscription = createBondReceiver().subscribe(new Subscriber<Intent>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(Intent intent) {
                    timeout.reschedule();

                    final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
                            BluetoothDevice.ERROR);
                    final int previousState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE,
                            BluetoothDevice.ERROR);

                    logger.info(GattPeripheral.LOG_TAG,
                            "Bond status changed from " + BondException.getBondStateString(previousState)
                                    + " to " + BondException.getBondStateString(state));

                    if (state == BluetoothDevice.BOND_NONE) {
                        logger.info(LOG_TAG, "Removing bond succeeded.");
                        timeout.unschedule();

                        subscriber.onNext(NativeGattPeripheral.this);
                        subscriber.onCompleted();

                        unsubscribe();
                    } else if (state == BluetoothDevice.ERROR) {
                        timeout.unschedule();

                        final int reason = intent.getIntExtra(BondException.EXTRA_REASON,
                                BondException.REASON_UNKNOWN_FAILURE);
                        logger.error(LOG_TAG,
                                "Removing bond failed for reason " + BondException.getReasonString(reason),
                                null);
                        subscriber.onError(new BondException(reason));

                        unsubscribe();
                    }
                }
            });

            timeout.setTimeoutAction(new Action0() {
                @Override
                public void call() {
                    subscription.unsubscribe();

                    // This can happen in Lollipop
                    if (getBondStatus() == BOND_NONE) {
                        subscriber.onNext(NativeGattPeripheral.this);
                        subscriber.onCompleted();
                    } else {
                        subscriber.onError(new OperationTimeoutException(Operation.REMOVE_BOND));
                    }
                }
            }, stack.getScheduler());

            if (!BluetoothDeviceCompat.removeBond(bluetoothDevice)) {
                subscription.unsubscribe();
                subscriber.onError(new BondException(BondException.REASON_ANDROID_API_CHANGED));
            } else {
                timeout.schedule();
            }
        }
    });
}