I am working in an Android application to receive Bluetooth data from an Hardware Bluetooth device. I have seen all the posts of stack and also used sample app to design my code.
The problem is, my "Bluetooth receiver" is not working, whenever i send data from hardware device, it works for the first time but exactly 2nd time it always failed to receive the data notification.
public class BluetoothDataReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ConfigClass.bluetoothDataReceive++;
if (ConfigClass.bluetoothDataReceive == 1) {
ConfigClass.showToast(context,
ConfigClass.MSG_RECEIVE_BLUETOOTH_DATA);
}else if (ConfigClass.bluetoothDataReceive ==2) {
ConfigClass.bluetoothDataReceive = 0;
}
}
}
Please help me out....I am struggling with this problem from a long time.
the BroadcastReceiver.onReceive() runs in different thread than your UI. UI is not thread safe. Doing Toast() within it is a bad idea, and supposedly leads to unexpected results...
Related
I have an app that enables people to send alarms to groups of other users and I have implemented support to trigger alarms from a BLE device. It all works as expected, except for when the app is in background.
I use a foreground service to listen for the device event and a local broadcast to trigger the actual alarm. The alarm get triggered as expected and all code runs (sending alarm to server, playing audio, etc) but the app stays in background.
I have tried several solutions but failed to come up with a working one. Either the answers are too old or they just do not work (as in unwanted results).
I want the exact same result as when tapping the foreground notification = resume the current activity without passing destroy.
I'm actually surprised how hard this seems to be, in my opinion it should be easy as "bringToFront()" since the activity is obviously running.
Any help would be much appreciated!
I don't think any of my code would help to understand my problem, but here's some anyways:
AlarmDeviceService.java - The service listening for BLE events:
private void sendAlarmTriggerBroadcastMessage(String deviceName){
Intent intent = new Intent();
intent.setAction(ALARM_DEVICE_TRIGGERED);
intent.putExtra(INTENT_EXTRA_SOURCE_NAME, deviceName);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
AlarmDeviceBroadcastReceiver.java:
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
Log.d(TAG, "Received: "+ action);
if(mReceiver != null){
if(action.equals(AlarmDeviceService.ALARM_DEVICE_TRIGGERED)) {
mReceiver.onDeviceAlarmBroadcast();
}
else if(action.equals(AlarmDeviceService.ALARM_DEVICE_CONNECTED)){
String mac = intent.getStringExtra(AlarmDeviceService.INTENT_EXTRA_SOURCE_MAC);
mReceiver.onAlarmDeviceConnected(mac);
}
else if(action.equals(AlarmDeviceService.ALL_DEVICES_CONNECTED)){
mReceiver.onAllDevicesConnected();
}
}
}
AlarmActivity.kt - The activity that should be brought to front:
override fun onDeviceAlarmBroadcast(){
if (alarmUtilities?.alarmSentHandler?.currentSentAlarm == null) {
Log.d(LOG, "Doing it!")
triggerDefaultAlarm()
}else{
alarmUtilities?.alarmSentHandler?.cancelAlarm()
}
}
If my question is poorly put, please excuse me - it's my first post.
To learn something new, I'm developing an Android APP (min SDK version API 23 and Target SDK Version API 28) that allows me and my family to create and share a virtual shopping list through HTTP requests and JSON responses on a free Web. Everything works fine, but I want to add a feature: I would like to get notified when someone makes a change even when the app is killed or has never been launched. I know what the task could do to compare the changes made on the list and I also know that it is something to be done once every 5 minute (for example), but I don't know how to perform background operations when the app is no longer running and it has been killed from the recent tasks list. I gave the Service class a try, but when the app is killed it stops. So I looked for a solution and I found the BroadcastReceiver and made it able to receive a message whenever the Service stops in order to restart it. But from Android API 26 the BroadcastReceiver must be (I guess..) contex-registered.
So this is what I my main Activity does when the onCreate method is called:
ReceiverCall receiver = new ReceiverCall();
registerReceiver(receiver, new IntentFilter("com.dadex.familyapp.startServiceRequest"));
My ReceiverCall which extends the BroadcastReceiver Class:
public class ReceiverCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
String action = intent.getAction();
if (action.equals("com.dadex.familyapp.startServiceRequest"))
context.startService(new Intent(context, CheckListService.class));
}
catch (Exception e){
Log.e("ERROR", e.getMessage());
}
}
}
And this is my CheckListService onDestroy method:
#Override
public void onDestroy() {
Intent intent = new Intent("com.dadex.familyapp.startServiceRequest");
sendBroadcast(intent);
}
It works fine when the app is launched, but as soon as I kill it, the receiver won't receive anything. So my question is: what is the best way to perform such background operations? Are there other classes I need to learn first? Thanks a lot!
You need a background service, with a notification to keep it alive.
startForegorund()
Search for startForegorund with notification and you will find what you need.
I have implemented the broadcast receiver and it counts the screen unlock events,but when i close the app it resets to 0.
I know that a service is required to be used but i don't know how it will update my variable in the Activity and keep it that way even if I close the application.
Following is the receiver.
Please help as I'm new to Android.
public class LockScreenStateReceiver extends BroadcastReceiver {
MainActivity inst = MainActivity.instance();
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// screen is turn off
//("Screen locked");
} else {
//Handle resuming events if user is present/screen is unlocked
inst.countUp();
//("Screen unlocked");
}
}
}
P.S. : Also, is it possible to do something like this in Ionic Framework?
I figured it out.
I used Shared Preferences for data saving and accessed them in the receiver as well.
Then to handle cases where the app is closed or just paused, I put some code in the try catch block.
However, I don't think this is a good way.
But it is way in which I didn't had to use a Service.
I am working on an Android application, having functionality like voicemail.
I am using BroadcastReceiver to get dialing events.
I have to get the event "WHEN CALL IS UNANSWERED (not picked after few rings) FROM RECEIVER".
I will do some actions on caller end against this event.
I am using AVD emulator, and
I do call from one instance to another instance and it calls perfectly,
but the problem is: It continuously calls until I reject or accept the call.
This way I cannot detect that "CALL IS UNANSWERED AFTER A NUMBER OF RINGS".
So I want the Caller emulator to drop the call after a number of rings (if unanswered) like a normal phone.
I can do it (drop the call after some time) by writing some code, but I need the natural functionality of phone in the emulator.
Can anyone please guide me? Is there any settings in the emulator? Or something else?
The code is shown below in case it helps:
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Bundle extras = intent.getExtras();
if (extras != null)
{
String state = "my call state = " + extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
}
}
Hi i think this is impossible with reference to the link:
http://android.bigresource.com/Track/android-sr1t1eagx/
Regards
Looking through forums, it seems that there is not a way to end calls, but on the Android Developers page, the permission PROCESS_OUTGOING_CALLS "Allows an application to monitor, modify, or abort outgoing calls." I can't find any documentation on how to end a call even with this permission. Is this possible, or is it just a mistake on the page?
http://developer.android.com/reference/android/Manifest.permission.html#PROCESS_OUTGOING_CALLS
The attention is not to end call that is already started, but to drop the call before that was started. The meaning is - you dial some number , press send , program receives the call request and aborts it , so the call never starts. It is also possible just to change the phone number (to extend short numbers to full one, etc.) and pass the call.
It works fine:
public class phoneReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
abortBroadcast();
if (getResultData()!=null) {
String number = null;
setResultData(number);
}
}
}
}