I have tried to connect using broadcast receiver and use Bluetooth adapter ack connect event but I can not get a disconnecting event. I can get connected, disconnected event but cannot get disconnecting event.
This is my code please help me with that.
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action != null) {
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
Log.e("STAGE ", "State On");
break;
case BluetoothAdapter.STATE_OFF:
Log.e("STAGE ", "State Off");
break;
case BluetoothAdapter.STATE_CONNECTED:
Log.e("STAGE ", "STATE_CONNECTED");
break;
case BluetoothAdapter.STATE_CONNECTING:
Log.e("STAGE ", "STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTED:
Log.e("STAGE ", "SSTATE_DISCONNECTED");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.e("STAGE ", "STATE_DISCONNECTING");
break;
}
}
}
}
in my code
case BluetoothAdapter.STATE_CONNECTING:
Log.e("STAGE ", "STATE_CONNECTING");
break;
case BluetoothAdapter.STATE_DISCONNECTING:
Log.e("STAGE ", "STATE_DISCONNECTING");
break;
not working, not getting any log.
I use this code to register the reciever
BluetoothReciever bluetoothReciever = new BluetoothReciever();
IntentFilter intentFilter = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
registerReceiver(bluetoothReciever, intentFilter);
Related
I am working on an app which determine a call is incoming or outgoing call. Not understanding why CALL_STATE_OFFHOOK is calling two times during a call.Any help please
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
startedCall=false;
log.info("state_idle");
// log.info("onCallStateChanged CALL_STATE_IDLE");
/* Intent idle = new Intent(context, BackgroundServices.class);
context.stopService(idle);*/
// Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
log.info("onCallStateChanged CALL_STATE_OFFHOOK");
if(!startedCall){
log.info("it is a outgoing call");
}
if (startedCall){
log.info("it is a incoming call");
}
/* Intent offHook = new Intent(context, BackgroundServices.class);
context.startService(offHook);*/
//Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
startedCall = true;
log.info("call state ringing");
//Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
}
}`
I am doing an application which speaks the caller name after 4 sec of the ring tone.The problem is it speaks the caller Name after two or three rings. The problem is it keeps speaking the caller name even if the call is in offhook or idle I don't want so.
the code in onreceive of broadcast receiver is
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals( TelephonyManager.CALL_STATE_OFFHOOK))
{
System.out.println("fjkerj");
}
else if(state.equals( TelephonyManager.CALL_STATE_IDLE))
{
}
else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Entered Receiver");
final String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (amanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
System.out.println("Silent");
break;
case AudioManager.RINGER_MODE_VIBRATE:
System.out.println("Vibrate");
break;
case AudioManager.RINGER_MODE_NORMAL:
amanager.setStreamVolume(AudioManager.STREAM_RING,1, 0);
Handler handler=new Handler();
Runnable r=new Runnable()
{
public void run()
{
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent IncomingNumberIntent = new Intent(context1,SpeakOut.class);
IncomingNumberIntent.putExtra("PhoneNumber", phonenumber);
context1.startService(IncomingNumberIntent);
}
}
};
handler.postDelayed(r, 3000);
If the call is in ringing and profile is normal, it will go to a service and talks the name but
the trouble that app speaks the name even if the call is offhook.
Change your code to
int state = bundle.getInt(TelephonyManager.EXTRA_STATE);
switch (state)
{
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("fjkerj");
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
// Your code goes here
}
Remove from the run() method the if statement
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
In the event of outgoing calls, are being throws automatically the state of OFF HOOK
Hi guys, firstly my apologies for my english.
Well, in my application I'm monitoring every call, before and after. But just after of the call start, so I don't matter with "ringing" state, however my application is triggering a fake "off hook" state. When I make a call (outgoing call), my app is setting the state off hook immediately following the ringing. With this, I'm monitoring a call that I don't should, because that call can do not be answered.
Did someone already had this problem?
AndroidManifest.xml
<receiver
android:name=".CallStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name=".OutgoingCallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
CallStateBroadcastReceiver:
CallStatePhoneStateListener phoneListener = new CallStatePhoneStateListener(context, intent);
TelephonyManager telephony = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
CallStatePhoneStateListener:
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// do something
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// do something
break;
case TelephonyManager.CALL_STATE_RINGING:
// do something
break;
}
OutgoingCallBroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null)
return;
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// just save the number
}
Take a Static String eg:- prev_state and store the state of phone in that String at each case
example if u want to know how the outgoing call is disconnected then should do something like this,
if(state==TelephonyManager.CALL_STATE_IDLE)
{ Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
{
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK))
{
prev_state=state;
// do something when the Call is ended
}
}
}
hope it helps :)
i hope this is work for u.
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("main", "incoming call receiver.");
PowerManager pm = (PowerManager) c
.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("idle state", "CALL_STATE_IDLE");
// CALL_STATE_IDLE;
if (ring == true && callReceived == false && CheckMissCall.isReject==true) {
Log.v("missed call", "Missed call from : " + incomingNumber);
if(CheckMissCall.isShown)
{
c.stopService(new Intent(c, Unlock_hud.class));
}
flag = true;
if (prefs.getBoolean("main_state", true) )
{
Intent inter = new Intent(c, MissCall.class);
inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
inter.putExtra("CellNumber", incomingNumber);
c.startActivity(inter);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
callReceived = true;
Log.v("call received", "CALL_STATE_OFFHOOK " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// CALL_STATE_RINGING
Log.v("call ringing", "CALL_STATE_RINGING " + incomingNumber);
Log.d("flags", "flags: " + flag);
if (flag) {
//cut = true;
//flag = false;
CheckMissCall call = new CheckMissCall(c);
call.setName(incomingNumber);
call.setNumber4Sms(incomingNumber);
call.setContactPhoto();
Log.d("main", "state ringing");
prefs = PreferenceManager.getDefaultSharedPreferences(c);
if (!prefs.getBoolean("main_state", false)) {
return;
}
if (CheckMissCall.isRunning) {
return;
}
else {
Log.d("main", "EEEEEEEEEEEEEEEE: Unlock hud");
Intent in = new Intent(c, Unlock_hud.class);
in.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
c.startService(in);
// c.stopService(new Intent(c, Unlock_hud.class));
}
}
break;
I am developing an app in which i am trying to check the service state of phone
using Service state: IN_SERVICE
if the phone is IN_SERVICE generate a Toast phone is in service if its not then generate a Toast phone is not in service
im trying this code
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
not able to get the Service state: IN_SERVICE Please help me
Thanks in advance
TelephonyManager telMng = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telMng.listen(new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
String phonestate;
switch(serviceState.getState()) {
case ServiceState.STATE_EMERGENCY_ONLY:
phonestate ="STATE_EMERGENCY_ONLY";
break;
case ServiceState.STATE_IN_SERVICE:
phonestate ="STATE_IN_SERVICE";
break;
case ServiceState.STATE_OUT_OF_SERVICE:
phonestate ="STATE_OUT_OF_SERVICE";
break;
case ServiceState.STATE_POWER_OFF:
phonestate ="STATE_POWER_OFF";
break;
default:
phonestate = "Unknown";
break;
}
}
}, PhoneStateListener.LISTEN_SERVICE_STATE);
I am trying to use PhoneStateListener to handle connection state changes. I've registered the listener inside onCreate method of a service. The overriden method onDataConnectionStateChanged(int state) of the listener is being called when I enable or disable wifi connection from the phone' settings, but in both cases the 'state' param is 0 and it always enters the first case of the switch.
Here's my implementation of the method:
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onDataConnectionStateChanged(int state) {
super.onDataConnectionStateChanged(state);
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
Toast.makeText(LocationService.this, "Data connection lost!", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.DATA_CONNECTED:
Toast.makeText(LocationService.this, "Data connection available!", Toast.LENGTH_LONG).show();
break;
}
}
};
The result is always a toast message saying: "Data connection lost!". I'm sure Im missing something simple here but I can't find out what the problem is.
Old question, but the code you show is for "data", not for Wifi.
I have the same code, and it's executed only when I connect with "LTE" (data)
private void registerDataListener(Context context) {
listener = new PhoneStateListener() {
#Override
public void onDataConnectionStateChanged(int state) {
switch (state) {
case TelephonyManager.DATA_DISCONNECTED:
Log.d(TAG, "Disconnected");
break;
case TelephonyManager.DATA_CONNECTED:
Log.d(TAG, "Connected");
break;
case TelephonyManager.DATA_CONNECTING:
Log.d(TAG, "Connecting");
break;
case TelephonyManager.DATA_SUSPENDED:
Log.d(TAG, "Disconnecting");
break;
}
}
};
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(listener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
Log.d(TAG, "Registering PhoneStateListener");
}
And don't forget to add permission in your manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />