I'm developing an android app that needs a BT connection. After reading the Android Developers page, the given method such us isEnabled() doesn't work. The error is cannot resolve symbol isEnabled.
The imported library is android.bluetooth.BluetoothAdapter. In the manifest file, following the instructions of the Android page, I also have inserted the permission to Bluetooth, BT admin and fine location.
The code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
Log.i("Fallo","Dispositivo sin bluetooth");
}
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
You are creating your BluetoothAdapter object outside the onCreate method. Put everything inside the onCreate curly brackets.
Related
I don't understand how to play text to speech with audio focus (there is spotify running some music when I try my app),
I don't understand the audioFocusChangeListener.
Here is my code:
public class MainActivity extends AppCompatActivity {
AudioManager.OnAudioFocusChangeListener audioFocusChangeListener;
public void playTTSWithAudioFocus(String toSpeak) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(audioFocusChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request temporary focus.
AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null,null);
}
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playTTSWithAudioFocus("hello world");
}
}
Please help me.
I'm making an app where it uses intent to send data to another app. In case, the other app, which is supposed to receive data from my app, is not installed on users's device then it redirects user to play store with toast message asking user to install it. I used "if else" to achieve this. It worked all good until I found that if the other app is disabled by user (OEM installed app which can't be uninstalled), then my app crashes. In such a condition, I want to let user know that the app is disabled by them and ask them to enable it (through toast message). How can I achieve this?
Here is my complete code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Creates button view which is connected to a view in the XML layout, which gets triggered on touching the view.
TextView textView = (TextView) findViewById(R.id.location);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.google.android.apps.maps");
if(isAppInstalled){
Uri gmmIntentUri = Uri.parse("geo:00,0000,00,0000");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
} else {
Uri uri2 = Uri.parse("market://details?id=com.google.android.apps.maps");
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri2);
Toast.makeText(MainActivity.this, "Google Maps not Installed", Toast.LENGTH_SHORT).show();
startActivity(goToMarket);
}
}
});
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
}
I'm not sure if this will work for you, but since you know the package name, you could try this to do a check beforehand.
So I was trying to get my first app up and running and I ran in to a problem when I tried debugging it on my phone.
The error states
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.stuff.morestuff. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
at com.google.firebase.auth.FirebaseAuth.getInstance(Unknown Source:1)
at com.stuff.morestuff.MainActivity.<init>(MainActivity.java:13)
And I've tried using FirebaseApp.initializeApp(this) but it still throws the same error.
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I've tried using this too but it didn't change anything, still the same error
//FirebaseApp.initializeApp(this);
mAuth = FirebaseAuth.getInstance();
}
#Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
Intent startIntent = new Intent(MainActivity.this, StartActivity.class);
startActivity(startIntent);
}
}
}
Did I miss something?
This solution worked for me. **REMARKS Check the comments to the solution if you want to use 4.1.0
https://stackoverflow.com/a/52136056/7808468
I get an error on CALL_PHONE
If I remove that condition error of call permission is out
I also add permissions in manifest but android studio highlight the CALL_back and if I remove the condition of if then it gives an error
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imp_no);
button = (Button) findViewById(R.id.buttonCall);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9990884476"));
if (ActivityCompat.checkSelfPermission(imp_no.this, Manifest.permission.**CALL_PHONE**) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
you can also use dexter permission library for easy permission check
I'm trying to make my app print a list of available Bluetooth-enabled devices upon opening the activity.
First I enable Bluetooth on the device:
public class Home extends AppCompatActivity {
TextView textView3;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
textView3 = (TextView) findViewById(R.id.textView3);
if (btAdapter == null) {
textView3.append("\nBluetooth not supported. Aborting.");
}
}
then I attempt to add any found devices to the Adapter
private final BroadcastReceiver bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//add device to the adapter
btAdapter.add(device.getName(), device.getAddress());
}
}
};
I know there should be a startDiscovery() call somewhere, but I'm having trouble making sense of other answers online. Also, my btAdapter.add() isn't recognized for some reason.
Any help would be appreciated!
my btAdapter.add() isn't recognized for some reason.
Because there is no such method in BluetoothAdapter.
I know there should be a startDiscovery() call somewhere, but I'm having trouble making sense of other answers online.
you can call startDiscovery, once you determine that device supports Bluetooth and Bluetooth is ON from DeviceSettings.