简体   繁体   中英

On pairing or connecting a Bluetooth Scanner(Inateck) and a mobile device the activity gets destroyed

I am setting up a Bluetooth Scanner Android Application, where I am trying to connect with a Bluetooth scanner (Inateck) and a mobile device using android code.

My Requirements: Pair both the devices through my Application. Connect with the device. Bluetooth scanner will scan the code from bar codes and I need to display it on my Applications Edit Text.

First I will discover Bluetooth devices using Android Bluetooth API.

public final BroadcastReceiver AvailableBlueToothDevicesBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {


        final String action = intent.getAction();
        Log.d(TAG, "onReceive: ACTION FOUND.");

        if (action.equals(BluetoothDevice.ACTION_FOUND)) {

            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if (availableDevice == null) {
                availableDevice = new MutableLiveData<BluetoothDevice>();
            }
            availableDevice.setValue(device);

        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

            //Done searching
            if (availableDevice == null) {
                availableDevice = new MutableLiveData<BluetoothDevice>();
            }
            availableDevice.setValue(null);
        }
    }
};

This is the broadcast receiver for getting the available devices.

I will register this

IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
                        getContext().registerReceiver(broadcastManager.AvailableBlueToothDevicesBroadcastReceiver, discoverDevicesIntent);  



With this I will get a list of Bluetooth devices nearby. I will list that in a Recyclerview. On cliking any of these devices pairing between the device has to be performed. So for that I have used the below code.

private void pairDevice(BluetoothDevice bluetoothDevice){

 if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {                 
                       Log.d(TAG, "Trying to pair with " + 
                       bluetoothDevicebluetoothDevice.getName());
                       bluetoothDevice.createBond();
            }

}

Once this code gets executed a dialog box appears, which ask for pairing with the device. Once pairing is complete my Activity's onDestroy() lifecycle event gets called and my activity is recreated by default. This looses my previous data in my activity. This should not happen. But pairing is success.

This happens when I try to connect my Android Mobile phone and a Bluetooth Scanner Device. But if I Tried to pair between two android phones the onDestroy() event getting called does not occur. Can anyone tell me why this happens?

Some device configurations can change during runtime (such as screen orientation, keyboard availability , and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

In my case I was trying to connect to a Bluetooth Scanner which will act as an physical keyboard. So we need to add the configChanges in the activity in AndroidManifest.xml

android:configChanges="orientation|screenSize|keyboard|keyboardHidden|navigation"

But orientation and screen size is not must in config changes. But the others are.

For more please refer: https://developer.android.com/guide/topics/resources/runtime-changes

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