aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/google/android/mobly/snippet/bundled/bluetooth/PairingBroadcastReceiver.java
blob: f7048978be44a50329ac111823cddce6aebadb67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.google.android.mobly.snippet.bundled.bluetooth;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import com.google.android.mobly.snippet.util.Log;
import com.google.android.mobly.snippet.bundled.utils.Utils;

@TargetApi(Build.VERSION_CODES.KITKAT)
public class PairingBroadcastReceiver extends BroadcastReceiver {
    private final Context mContext;
    public static IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);

    public PairingBroadcastReceiver(Context context) throws Throwable {
        mContext = context;
        Utils.adaptShellPermissionIfRequired(mContext);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.d("Confirming pairing with device: " + device.getAddress());
            device.setPairingConfirmation(true);
            mContext.unregisterReceiver(this);
        }
    }
}