Android Prevent Bluetooth Pairing Dialog

前端 未结 1 1387
情深已故
情深已故 2020-12-30 03:12

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 tr

相关标签:
1条回答
  • 2020-12-30 03:53

    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.

    0 讨论(0)
提交回复
热议问题