17

I'm developing an internal application which uses Bluetooth for printing. I want the Bluetooth pairing to occur without user input. I have managed to get that working by trapping the android.bluetooth.device.action.PAIRING_REQUEST broadcast.

In my broadcast receiver I call the setPin method, and pairing works ok, but a BluetoothPairingDialog is displayed for a second or two, then it disappears - see link below.

https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/bluetooth/BluetoothPairingDialog.java

Since the broadcast is non-ordered, I can't call abortBroadcast(), and was wondering if there was any other way to prevent the pairing dialog from appearing. Can I hook into the window manager in some way?

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
Keith Darragh
  • 171
  • 1
  • 5
  • I am having the same issue. I can dismiss the dialog without user input by calling device.cancelPairingUserInput(); device.setPairingConfirmation( true ); but only after receiving the BluetoothDevice.ACTION_BOND_STATE_CHANGED action with state BluetoothDevice.BOND_BONDING so the dialog comes up briefly and is then dismissed. – Johnny C May 15 '14 at 21:57
  • any update on this? I need to prevent this in cordova – Amit Gandole Sep 30 '19 at 05:29
  • Sorrry, I haven't been working on this project for a few years. I never did get around this issue. – Keith Darragh Sep 30 '19 at 13:15

1 Answers1

3

I honestly haven't been able to come up with a way to do this without modifying the sdk. If you're the OEM, it's easy (I'm on 4.3):

In packages/apps/Settings/AndroidManifest.xml, comment the intent filter for the pairing dialog:

<activity android:name=".bluetooth.BluetoothPairingDialog"
          android:label="@string/bluetooth_pairing_request"
          android:excludeFromRecents="true"
          android:theme="@*android:style/Theme.Holo.Dialog.Alert">
    <!-- <intent-filter>
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter> -->
</activity>

In frameworks/base/core/java/android/bluetooth/BluetoothDevice.java remove the @hide javadoc annotation from this constant

@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_PAIRING_REQUEST =
        "android.bluetooth.device.action.PAIRING_REQUEST";

and this method

public boolean setPairingConfirmation(boolean confirm) 

Then register your own activity or broadcast receiver for the BluetoothDevice.PAIRING_REQUEST action. This broadcast receiver allows pairing to continue without user input (only if no pin is required):

@Override
public void onReceive(Context context, Intent intent) {    
   if( intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST) ) {
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
      device.setPairingConfirmation( true );
   }
}

You'll need to rebuild the sdk and compile your code against the new version to get access to the constant and methods, and replace Settings.apk on the /system partition to disable the dialog. You may possibly also need to be running as a system app but I think likely not.

Johnny C
  • 1,799
  • 1
  • 16
  • 27