Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 953
Keith Darragh Avatar asked Jul 31 '13 13:07

Keith Darragh


People also ask

How do I stop Bluetooth pairing request?

To do this, turn on Bluetooth, connect to the devices you are interested in, and then turn off Bluetooth discoverability. This way, the requesting device will not “see” your device and the requests will cease.

How do I block Bluetooth on my Android?

It's also easy to disable Bluetooth from the Settings app, but it takes a few more taps. To do it, open Settings and navigate to Connected Devices > Connection Preferences > Bluetooth and turn off the switch beside “Use Bluetooth.”

What is Bluetooth pairing request?

Bluetooth pairing is a form of information registration for linking devices. By registering device information (pairing) between devices, they can connect. To use a Bluetooth device, you must first pair it with another Bluetooth device. Pairing is a bit like exchanging phone numbers.

Can someone connect to my Bluetooth without me knowing?

Can someone connect to my Bluetooth without me knowing? Theoretically, anyone can connect to your Bluetooth and gain unauthorized access to your device if the visibility of your Bluetooth device is on.


1 Answers

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.

like image 194
Johnny C Avatar answered Sep 20 '22 19:09

Johnny C