Example usage for android.bluetooth BluetoothDevice ACTION_PAIRING_REQUEST

List of usage examples for android.bluetooth BluetoothDevice ACTION_PAIRING_REQUEST

Introduction

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

Prototype

String ACTION_PAIRING_REQUEST

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

Click Source Link

Document

Broadcast Action: This intent is used to broadcast PAIRING REQUEST

Requires android.Manifest.permission#BLUETOOTH_ADMIN to receive.

Usage

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

@RequiresPermission(Manifest.permission.BLUETOOTH)
public NativeBluetoothStack(@NonNull Context applicationContext, @NonNull ErrorListener errorListener,
        @NonNull LoggerFacade logger) {//from  w  ww.  jav  a 2s  .  c  o m
    this.applicationContext = applicationContext;
    this.errorListener = errorListener;
    this.logger = logger;

    this.bluetoothManager = (BluetoothManager) applicationContext.getSystemService(Context.BLUETOOTH_SERVICE);
    this.adapter = bluetoothManager.getAdapter();
    if (adapter != null) {
        final BroadcastReceiver powerStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final int newState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                if (newState == BluetoothAdapter.STATE_ON) {
                    enabled.onNext(true);
                } else if (newState == BluetoothAdapter.STATE_OFF || newState == BluetoothAdapter.ERROR) {
                    enabled.onNext(false);
                }
            }
        };
        applicationContext.registerReceiver(powerStateReceiver,
                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
        enabled.onNext(adapter.isEnabled());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            final BroadcastReceiver pairingReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    final Intent broadcast = new Intent(ACTION_PAIRING_REQUEST);
                    broadcast.putExtra(GattPeripheral.EXTRA_NAME, device.getName());
                    broadcast.putExtra(GattPeripheral.EXTRA_ADDRESS, device.getAddress());
                    LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);
                }
            };
            applicationContext.registerReceiver(pairingReceiver,
                    new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));
        }
    } else {
        logger.warn(LOG_TAG, "Host device has no bluetooth hardware!");
        enabled.onNext(false);
    }
}