Turning bluetooth on, gives back wrong request code in onActivityResult - android

This is my code, for turning on the bluetooth:
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Also:
public static final int REQUEST_ENABLE_BT = 9;
This is my onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
bluetoothSetupDone();
} else {
// User did not enable Bluetooth or an error occurred
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
The result code is correct, but the request code is not.
Even if the user presses no, or yes on the popup dialog for turning on bluetooth. The value of the requestCode variable in the onActivityResult is some random number (196617), but it should be 9.

Damn. Should have used:
getSupportActivity().startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
wrong requestCode in onActivityResult
http://blog.tgrigsby.com/2012/04/18/android-fragment-frustration.aspx

I was calling startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); from a fragment, So i added getActivity().startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);and this solved my issue.

Related

onActivityResult not called in Android

I have the following code
public void changeContentOnClick(View view) {
Intent intent = new Intent(this, ChangeNodeContentActivity.class);
intent.putExtra("SELECTED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("SELECTED_NODE_CONTENT", selectedNode.getNodeContent());
startActivityForResult(intent,RESULT_OK);
Log.d(TAG, "Can I get here?");
onActivityResult(RESULT_OK, RESULT_OK, intent);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String editedNodeId = data.getStringExtra("EDITED_NODE_ID");
String editedNodeContent = data.getStringExtra("EDITED_NODE_CONTENT");
Node nodeChecker = new Node(editedNodeId);
Node editedNode = new Node(editedNodeId, editedNodeContent);
Log.d(TAG, editedNode.toString());
nodes.set(nodes.indexOf(nodeChecker), editedNode);
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
Somehow, after startActivityForResult method is called, everything stops. The log with message "Can I get here?" is never printed and I don't understand why not. I have followed the answer to this topic, but somehow couldn't make it work. Do I call the onActivityResult in the wrong way or place? Please help me out!
The that should send back some info from the ChangeNodeContentActivity and the one that handles the result code is the following on click listener:
public void changeContentOnClick(View view) {
Intent intent = getIntent();
selectedNode.setNodeContent(nodeContentDisplay.getText().toString());
intent.putExtra("EDITED_NODE_ID", selectedNode.getNodeId());
intent.putExtra("EDITED_NODE_CONTENT", selectedNode.getNodeContent());
setResult(RESULT_OK, intent);
Log.d(TAG, selectedNode.toString());
finish();
}
startActivityForResult() method receives an intent and requestCode, so you should change it to:
startActivityForResult(intent, REQUEST_CODE)
And onActivityResult() method is automatically called when returning from the activity, so you should remove the direct call you made to the method, i.e. onActivityResult(RESULT_OK, RESULT_OK, intent)

ActivityForResult doesn't work

I am new in android. I was making a sample. I was going to ask a user to turn on bluetooth. The ask form shows but onActivity result does not work. Why?
Here is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, RESULT_OK);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
}
}
Your request code should be greater than Zero, but RESULT_OK = -1 so try to give Request code more than 0,
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 101);
Src
RESULT_OK is -1 and startActivityForResult expects requestCode >=0.
RESULT_OK (added in API level 1)
int RESULT_OK
Standard activity result: operation succeeded.
Constant Value: -1 (0xffffffff)
Method startActivityForResult (added in API level 1)
void startActivityForResult (Intent intent, int requestCode)
Parameters
intent Intent: The intent to start.
requestCode int: If >= 0, this code will be returned in onActivityResult() when the activity exits.
Throws
android.content.ActivityNotFoundException
You should handle response as per requestCode.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Can use switch case also if more requestCode
if (requestCode == 100) {
// do something
} else if (requestCode == 101) {
// do something
} else {
// do something
}
Toast.makeText(this, "onActivityResult", Toast.LENGTH_SHORT).show();
}

how to wait till bluetooth turn on in android

I need to turn on Bluetooth in an android device programmatically and wait till it on to proceed to next line of code.
My code is as below
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
ctx.startActivity(enableBtIntent);
}
When doing like this, the code continue to execute from next line without waiting for bluetooth completely on. Is there any way to solve this? Can I add a look to check if bluetooth is on?
You can register a BroadcastReceiver to listen for state changes on the BluetoothAdapter.
First create a BroadcastReceiver that will listen for state changes
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (bluetoothState) {
case BluetoothAdapter.STATE_ON:
//Bluethooth is on, now you can perform your tasks
break;
}
}
}
};
Then register the BroadcastReceiver with your Activity when it is created so that it can start receiving events.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set a filter to only receive bluetooth state changed events.
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, filter);
}
Remember to unregister the listener when the Activity is destroyed.
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
}
You can use startActivityForResult() and check for whether resultCode is RESULT_OK in onActivityResult() with bluetooth permission in your Manifest file like..
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 0);
}
onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
// bluetooth enabled
}else{
// show error
}
}
ACTION_REQUEST_ENABLE
Use this code
Permissions on your menifest file
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
and code
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent , 0);
} else
{
Toast.makeText(getApplicationContext(),"Already on", Toast.LENGTH_LONG).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(),"Turned on",Toast.LENGTH_LONG).show();
}
if(resultCode == RESULT_CANCELED){
}
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//do your things
}

Android: force bluetooth

Is there a way to force bluetooth on?
all I've found so far is this (using the estimote sdk, which I'm working with):
// Check if device supports Bluetooth Low Energy.
if (!beaconManager.hasBluetooth()) {
Toast.makeText(this, "Device does not have Bluetooth Low Energy", Toast.LENGTH_LONG).show();
return;
}
// If Bluetooth is not enabled, let user enable it.
if (!beaconManager.isBluetoothEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else {
connectToBlueTooth();
}
And then in the onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
connectToBlueTooth();
}
else {
Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_LONG).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
But this asks to the user if he wants to turn on bluetooth... but is there a way to turn it on without asking the user?
And, if there is no way to do it, how can I use this technique outside of an activity?
Thanks
Try BluetoothAdapter like this:
BluetoothAdapter.getDefaultAdapter().enable();

How do I start thread only after first activity is finished?

In my application I have scan button which scan qr code. Code is like this:
btnScan.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 1);
ClearForm();
//if (!CheckCHFID())return;
pd = ProgressDialog.show(EnquireActivity.this, "", getResources().getString(R.string.GetingInsuuree));
new Thread(){
public void run(){
getInsureeInfo();
pd.dismiss();
}
}.start();
}
});
Now the problem is before I scan the code it starts finding the information which is getInsureeInfo(); How can I control that it should execute only after user scans the code successfully?
Thanks in advance.
you need to move the part that you want to happen after the scan to the onActivityResult() method.
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan.
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
}
}
}
Also I think you are going to have to use a Handler to send a message from the work thread to the main thread when it is time to hide your progress dialog. I don't think it will let you call dismiss on it from the background thread. That is just a hunch though, not tested.
put it in OnActivityResult method ,ovveride it.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 1:
if (resultCode == RESULT_OK) {
//put your stuff here....
break;
}
}
}

Categories

Resources