Java Code Examples for android.bluetooth.BluetoothDevice#ACTION_PAIRING_REQUEST

The following examples show how to use android.bluetooth.BluetoothDevice#ACTION_PAIRING_REQUEST . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerialSocket.java    From SimpleBluetoothLeTerminal with MIT License 6 votes vote down vote up
private void onPairingBroadcastReceive(Context context, Intent intent) {
    // for ARM Mbed, Microbit, ... use pairing from Android bluetooth settings
    // for HM10-clone, ... pairing is initiated here
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if(device==null || !device.equals(this.device))
        return;
    switch (intent.getAction()) {
        case BluetoothDevice.ACTION_PAIRING_REQUEST:
            final int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, -1);
            Log.d(TAG, "pairing request " + pairingVariant);
            onSerialConnectError(new IOException(context.getString(R.string.pairing_request)));
            // pairing dialog brings app to background (onPause), but it is still partly visible (no onStop), so there is no automatic disconnect()
            break;
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
            final int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
            final int previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
            Log.d(TAG, "bond state " + previousBondState + "->" + bondState);
            break;
        default:
            Log.d(TAG, "unknown broadcast " + intent.getAction());
            break;
    }
}
 
Example 2
Source File: BluetoothA2dpSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
@RpcMinSdk(Build.VERSION_CODES.KITKAT)
@Rpc(
        description =
                "Connects to a paired or discovered device with A2DP profile."
                        + "If a device has been discovered but not paired, this will pair it.")
public void btA2dpConnect(String deviceAddress) throws Throwable {
    BluetoothDevice device = BluetoothAdapterSnippet.getKnownDeviceByAddress(deviceAddress);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    mContext.registerReceiver(new PairingBroadcastReceiver(mContext), filter);
    Utils.invokeByReflection(sA2dpProfile, "connect", device);
    if (!Utils.waitUntil(
            () -> sA2dpProfile.getConnectionState(device) == BluetoothA2dp.STATE_CONNECTED,
            120)) {
        throw new BluetoothA2dpSnippetException(
                "Failed to connect to device "
                        + device.getName()
                        + "|"
                        + device.getAddress()
                        + " with A2DP profile within 2min.");
    }
}
 
Example 3
Source File: BluetoothHearingAidSnippet.java    From mobly-bundled-snippets with Apache License 2.0 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.Q)
@RpcMinSdk(Build.VERSION_CODES.Q)
@Rpc(description = "Connects to a paired or discovered device with HA profile.")
public void btHearingAidConnect(String deviceAddress) throws Throwable {
    BluetoothDevice device = BluetoothAdapterSnippet.getKnownDeviceByAddress(deviceAddress);
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    context.registerReceiver(new PairingBroadcastReceiver(context), filter);
    Utils.invokeByReflection(hearingAidProfile, "connect", device);
    if (!Utils.waitUntil(
            () ->
                    hearingAidProfile.getConnectionState(device)
                            == BluetoothHearingAid.STATE_CONNECTED,
            TIMEOUT_SEC)) {
        throw new BluetoothHearingAidSnippetException(
                String.format(
                        "Failed to connect to device %s|%s with HA profile within %d sec.",
                        device.getName(), device.getAddress(), TIMEOUT_SEC));
    }
}
 
Example 4
Source File: G5CollectionService.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initScanCallback();
    }
    advertiseTimeMS.add((long)0);

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings(true);

    // TODO check this
    //bgToSpeech = BgToSpeech.setupTTS(getApplicationContext()); //keep reference to not being garbage collected
    // handler = new Handler(getApplicationContext().getMainLooper());

    final IntentFilter bondintent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//KS turn on
    bondintent.addAction(BluetoothDevice.ACTION_FOUND);//KS add
    registerReceiver(mPairReceiver, bondintent);//KS turn on

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}
 
Example 5
Source File: G5CollectionService.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initScanCallback();
    }
    advertiseTimeMS.add((long)0);

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings(true);

    // TODO check this
    //bgToSpeech = BgToSpeech.setupTTS(getApplicationContext()); //keep reference to not being garbage collected
    // handler = new Handler(getApplicationContext().getMainLooper());

    final IntentFilter bondintent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//KS turn on
    bondintent.addAction(BluetoothDevice.ACTION_FOUND);//KS add
    registerReceiver(mPairReceiver, bondintent);//KS turn on

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}
 
Example 6
Source File: G5CollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initScanCallback();
    }
    advertiseTimeMS.add((long)0);
    service = this;
    mContext = getApplicationContext();//KS
    //KS foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), service);
    //KS foregroundServiceStarter.start();

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();

    // TODO check this
    //KS bgToSpeech = BgToSpeech.setupTTS(getApplicationContext()); //keep reference to not being garbage collected
    // handler = new Handler(getApplicationContext().getMainLooper());

    final IntentFilter bondintent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//KS turn on
    bondintent.addAction(BluetoothDevice.ACTION_FOUND);//KS add
    registerReceiver(mPairReceiver, bondintent);//KS turn on

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}
 
Example 7
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {

    if (scanMeister == null) {
        scanMeister = new ScanMeister()
                .applyKnownWorkarounds()
                .addCallBack(this, TAG);
    }

    foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), this);
    foregroundServiceStarter.start();
    //mContext = getApplicationContext();
    dexCollectionService = this;
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();
    //bgToSpeech = BgToSpeech.setupTTS(mContext); //keep reference to not being garbage collected
    if (CollectionServiceStarter.isDexBridgeOrWifiandDexBridge()) {
        Log.i(TAG, "onCreate: resetting bridge_battery preference to 0");
        prefs.edit().putInt("bridge_battery", 0).apply();
        //if (Home.get_master()) GcmActivity.sendBridgeBattery(prefs.getInt("bridge_battery",-1));
    }

    cloner.dontClone(
            android.bluetooth.BluetoothDevice.class,
            android.bluetooth.BluetoothGattService.class
    );

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
    Log.i(TAG, "onCreate: STARTING SERVICE: pin code: " + DEFAULT_BT_PIN);

    Blukon.unBondIfBlukonAtInit();

}
 
Example 8
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {

    if (scanMeister == null) {
        scanMeister = new ScanMeister()
                .applyKnownWorkarounds()
                .addCallBack(this, TAG);
    }

    foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), this);
    foregroundServiceStarter.start();
    //mContext = getApplicationContext();
    dexCollectionService = this;
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();
    //bgToSpeech = BgToSpeech.setupTTS(mContext); //keep reference to not being garbage collected
    if (CollectionServiceStarter.isDexBridgeOrWifiandDexBridge()) {
        Log.i(TAG, "onCreate: resetting bridge_battery preference to 0");
        prefs.edit().putInt("bridge_battery", 0).apply();
        //if (Home.get_master()) GcmActivity.sendBridgeBattery(prefs.getInt("bridge_battery",-1));
    }

    cloner.dontClone(
            android.bluetooth.BluetoothDevice.class,
            android.bluetooth.BluetoothGattService.class
    );

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
    Log.i(TAG, "onCreate: STARTING SERVICE: pin code: " + DEFAULT_BT_PIN);

    Blukon.unBondIfBlukonAtInit();

}
 
Example 9
Source File: BluetoothGlucoseMeter.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}
 
Example 10
Source File: G5CollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        initScanCallback();
    }
    advertiseTimeMS.add((long)0);
    service = this;
    mContext = getApplicationContext();//KS
    //KS foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), service);
    //KS foregroundServiceStarter.start();

    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();

    // TODO check this
    //KS bgToSpeech = BgToSpeech.setupTTS(getApplicationContext()); //keep reference to not being garbage collected
    // handler = new Handler(getApplicationContext().getMainLooper());

    final IntentFilter bondintent = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//KS turn on
    bondintent.addAction(BluetoothDevice.ACTION_FOUND);//KS add
    registerReceiver(mPairReceiver, bondintent);//KS turn on

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}
 
Example 11
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {

    if (scanMeister == null) {
        scanMeister = new ScanMeister()
                .applyKnownWorkarounds()
                .addCallBack(this, TAG);
    }

    foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), this);
    foregroundServiceStarter.start();
    //mContext = getApplicationContext();
    dexCollectionService = this;
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();
    //bgToSpeech = BgToSpeech.setupTTS(mContext); //keep reference to not being garbage collected
    if (CollectionServiceStarter.isDexBridgeOrWifiandDexBridge()) {
        Log.i(TAG, "onCreate: resetting bridge_battery preference to 0");
        prefs.edit().putInt("bridge_battery", 0).apply();
        //if (Home.get_master()) GcmActivity.sendBridgeBattery(prefs.getInt("bridge_battery",-1));
    }

    cloner.dontClone(
            android.bluetooth.BluetoothDevice.class,
            android.bluetooth.BluetoothGattService.class
    );

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
    Log.i(TAG, "onCreate: STARTING SERVICE: pin code: " + DEFAULT_BT_PIN);

    Blukon.unBondIfBlukonAtInit();

}
 
Example 12
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {

    if (scanMeister == null) {
        scanMeister = new ScanMeister()
                .applyKnownWorkarounds()
                .addCallBack(this, TAG);
    }

    foregroundServiceStarter = new ForegroundServiceStarter(getApplicationContext(), this);
    foregroundServiceStarter.start();
    //mContext = getApplicationContext();
    dexCollectionService = this;
    prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    listenForChangeInSettings();
    //bgToSpeech = BgToSpeech.setupTTS(mContext); //keep reference to not being garbage collected
    if (CollectionServiceStarter.isDexBridgeOrWifiandDexBridge()) {
        Log.i(TAG, "onCreate: resetting bridge_battery preference to 0");
        prefs.edit().putInt("bridge_battery", 0).apply();
        //if (Home.get_master()) GcmActivity.sendBridgeBattery(prefs.getInt("bridge_battery",-1));
    }

    cloner.dontClone(
            android.bluetooth.BluetoothDevice.class,
            android.bluetooth.BluetoothGattService.class
    );

    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
    Log.i(TAG, "onCreate: STARTING SERVICE: pin code: " + DEFAULT_BT_PIN);

    Blukon.unBondIfBlukonAtInit();

}
 
Example 13
Source File: BluetoothGlucoseMeter.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    final IntentFilter pairingRequestFilter = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
    pairingRequestFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
    registerReceiver(mPairingRequestRecevier, pairingRequestFilter);
}