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();
Related
I have an activity where I ask to enable Bluetooth and discovery mode.
Requests are executed correctly and are taken by onactivityresult().
The problem is in the discovery request.
If I click on reject then RESULT_CANCELED is taken correctly, while if I click on allow, the result code is 120 and therefore it is not RESULT_OK and I cannot start the activity, it is normal that it is 120 and not RESULT_OK?
MyActivity
public class TransitionActivity extends AppCompatActivity {
private static final int ENABLE_REQUEST = 0;
private static final int DISCOVER_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){ //bluetooth not supported
Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show();
finish();
}
if(!bluetoothAdapter.isEnabled()){
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i,ENABLE_REQUEST);
}
if(!bluetoothAdapter.isDiscovering()){
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(i,DISCOVER_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ENABLE_REQUEST){
if(resultCode == RESULT_OK){
}
if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "You need to enable the bluetooth.", Toast.LENGTH_SHORT).show();
finish();
}
}
if(requestCode == DISCOVER_REQUEST){
System.out.println("RESULT CODE" + resultCode); //it is 120
if(resultCode == RESULT_OK){ //skipped
Intent secondActivity = new Intent(this, com.project.secondActivity.class);
this.startActivity(secondActivity);
}
if(resultCode == RESULT_CANCELED){
finish();
}
}
}
}
Value of resultCode is same as discoverable duration EXTRA_DISCOVERABLE_DURATION passed in Intent.
and by default duration is 120. So it is OK.
If you will start activity like this
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
startActivityForResult(i,DISCOVER_REQUEST);
It will return 240 as result code.
I have an app used for Bluetooth control of an embedded system using the SPP protocol that has worked fine until Marshmallow. If Bluetooth is enabled when I start the app everything is good. I use startActivityForResult() to prompt the user to allow Bluetooth to be enabled if it's not already. It used to block until a result was given but under Marshmallow it blows through it and crashes immediately with a null pointer exception. I added a "hack" in onResume() to keep it from doing this by returning if not enabled, but am wondering if this is poor practice?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btAdapter = BluetoothAdapter.getDefaultAdapter();
if (btAdapter == null) {
Toast.makeText(getApplicationContext(), "No bluetooth detected", Toast.LENGTH_SHORT).show();
finish();
} else {
if (!btAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, ENABLE_BT_REQUEST_CODE);
}
}
}
#Override
public void onResume() {
super.onResume();
/* This keeps from crashing immediately if BT not enabled */
if (!btAdapter.isEnabled()) {
return;
};
if (btAdapter.isDiscovering()) {
btAdapter.cancelDiscovery();
}
// Rest of code .......
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check Result from turnOnBT()
if (requestCode == ENABLE_BT_REQUEST_CODE) {
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Bluetooth must be enabled to continue", Toast.LENGTH_SHORT).show();
finish();
}
}
}
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
}
I am trying to initialize/ turn on the NFC and the BT modules in same activity.
I need them both to be enabled before I continue the task.
I do understand that OnResultActivity is Async so I am trying to figure out what would be the best way to achieve it?
Here's most of the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initBT();
initNFC();
Intent nextIntent;
if(SaveSharedPreference.getUserName(MainActivity.this).length() == 0)
{
nextIntent = new Intent(MainActivity.this, LoginActivity.class);
}
else
{
nextIntent = new Intent(MainActivity.this, MainMenuActivity.class);
}
startActivity(nextIntent);
finish();
}
private void initBT() {
if(BTModule.GetInstance().initBT().equals(Constants.eBluetoothStatus.BT_DISABLED)){
Intent enableBtIntent = new Intent(BTModule.GetAdapter().ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void initNFC(){
mNFCState = NFCModule.initNFC(this);
if(mNFCState == eNFCStatus.NFC_NOT_SUPPORTED){
Toast.makeText(this, "NFC is not suppoeted for this device", Toast.LENGTH_LONG).show();
}
else if(mNFCState == eNFCStatus.NFC_DISABLED){
Intent nfcIntent;
if(android.os.Build.VERSION.SDK_INT >= 16){
nfcIntent = new Intent(android.provider.Settings.ACTION_NFC_SETTINGS);
}
else{
nfcIntent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}
startActivity(nfcIntent);
Toast.makeText(this, "Please enable NFC", Toast.LENGTH_LONG);
}
else{
Toast.makeText(this, "NFC is up and running", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == eBluetoothStatus.BT_OK.ordinal()){
Toast.makeText(this, "BT is up and running", Toast.LENGTH_LONG).show();
return;
}
}
}
As I am a newbie please feel free to correct me if I am wrong with anything :)
thanks!!
Use startActivityForResult instead of startActivity in initNFC method. Also define 2 boolean variables like isNfcEnabled and isBtEnabled. In onActivityResult method check those booleans. If both of them are enable, do whatever you want.
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.