简体   繁体   中英

Android Prevent Bluetooth Pairing Dialog

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM