BluetoothAdapter in putExtra - android

I have a requirement where I want to develop a hybrid application and list the Bluetooth devices, scan and connect, share files etc.
So I want to add BluetoothAdapter mAdapter value into extras and pass it into onActivity result. Like enableIntent.putExtra("", mAdapter); and use it in onActivityResult by getExtra(...). Is there a way to do so??..
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
((Activity)context).startActivityForResult(enableIntent, RequestCodes.BLUETOOTH_ENABLE_CODE);
}

Why don't you use Application class to store that data and use wherever you need.
Check this post for how to use Application class.

Related

How to use ACTION_OPEN_DOCUMENT_TREE without startActivityForResult?

I want to select a folder on my phone's SD card. At the moment I am doing this:
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
Log.d(TAG, "Activity result!");
if (result.getResultCode() == Activity.RESULT_OK) {
}
}
});
..
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
activityResultLauncher.launch(intent);
How do I get the folder back from the activity? What if I display another activity - how can I tell which activityresult is which?
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
activityResultLauncher.launch(intent);
The old way of doing things makes sense to me (e.g. see here with request codes and intents), but I don't understand how this is done with the new API calls.
I think your confusion stems from using the wrong contract. ActivityResultContracts.StartActivityForResult is more for cases where you have your own activity and you want to start it and process the result in a custom way. In that case you would return the result in an Intent and then use activityResult.getData() to get that Intent and process it.
For ACTION_OPEN_DOCUMENT_TREE there is no need to do this (and I have no idea what format the Android OS would return, it's probably not documented), you can just use ActivityResultContracts.OpenDocumentTree.
How do I get the folder back from the activity?
Switching to use ActivityResultContracts.OpenDocumentTree will allow you to define an ActivityResultCallback<Uri> rather than a ActivityResultCallback<ActivityResult>. You can then process the received Uri as you normally would.
What if I display another activity - how can I tell which activityresult is which?
If you are starting another activity, you'd use a different ActivityResultLauncher (and have to call registerForActivityResult separately for that case, with its own associated callback). The Android OS takes care of calling the correct callback based on the launcher used to launch the activity.

Custom Bluetooth message mBluetoothAdapter

With the following code you can pop up a window with bluetooth acces request:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
The output of the code is the following image:
Is it possible to change the message (so not "An app wants to turn on Bluetooth", but something like "Hey can you please turn on Bluetooth?"
You should create your own dialog with your own message and then call
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
And do not forget to declare the BLUETOOTH_ADMIN permission in your manifest.
Anyway, this operation is discouraged... The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
No, this is build-in Android functionality, so you would have to make a custom dialog which asks the question the way you want.
Then you would enable or disable Bluetooth programmatically based on what the users choice was.

How to emulate intents through espresso

I have developed an App which starts through an intent-filter and get data from this intent.
The data will be decrypted and used to fill up my UI. Could I simulate this intent using espresso? how?
Thanks for any answer.
Intent i = new Intent(ACTION_NDEF_DISCOVERED);
i.putExtra(EXTRA_NDEF_MESSAGES, mArrayList);
setActivityIntent(i);
// Espresso will not launch
//our activity for us, we must launch it via getActivity().
getActivity();

How to run Device Police Manager From A Service

I am able to create an Activity that uses the DevicePolicyManager API's.
The tutorials show that I need use it the following fashion:
if (!mDPM.isAdminActive(mAdminName)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra("wipe-data", DeviceAdminInfo.USES_POLICY_WIPE_DATA);
startActivityForResult(intent, REQUEST_ENABLE);
}
else {
mDPM.wipeData(0);
}
However I would like this to run inside a Service.
But I cant call
startActivityForResult
from within a Service.
So what would be the best approach or strategy for me to try ?
The only reason you need to call startActivityForResult() is if your app is not presently configured as a device administrator, to lead the user to go set that up for you. Hence, put that portion of your logic inside of your user interface.
Your service itself would just skip doing anything if isAdminActive() returns false.

Enable Android bluetooth from documentation

I am trying to get my Activity to enable Bluetooth with the Android 2.0.1 SDK, I am using some code straight from the documentation here: http://developer.android.com/guide/topics/wireless/bluetooth.html
Which is:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
I get an error at the REQUEST_ENABLE_BT part where Eclipse says it cannot be resolved. What am I doing wrong?
The REQUEST_ENABLE_BT part is a request code that you should handle in your onActivityResult method. In that method you'll be notified whether or not enabling Bluetooth was successful.
In that code snippet they didn't show the definition of it, but it's just a constant integer so you can set it to any value you like.
See the documentation for startActivityForResult for more info about getting results back from activity launches.

Categories

Resources