Example usage for android.media AudioManager isBluetoothA2dpOn

List of usage examples for android.media AudioManager isBluetoothA2dpOn

Introduction

In this page you can find the example usage for android.media AudioManager isBluetoothA2dpOn.

Prototype

public boolean isBluetoothA2dpOn() 

Source Link

Document

Checks whether a Bluetooth A2DP audio peripheral is connected or not.

Usage

From source file:de.tubs.ibr.dtn.dtalkie.TalkieActivity.java

@SuppressWarnings("deprecation")
private void setAudioOutput() {
    AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    if (am.isBluetoothA2dpOn()) {
        // play without speaker
        am.setSpeakerphoneOn(false);/*from   w  ww.j  av  a2s  .c  o  m*/
    } else if (am.isWiredHeadsetOn()) {
        // play without speaker
        am.setSpeakerphoneOn(false);
    } else {
        // without headset, enable speaker
        am.setSpeakerphoneOn(true);
    }
}

From source file:fr.inria.ucn.collectors.SysStateCollector.java

@SuppressWarnings("deprecation")
private JSONObject getAudioState(Context c) throws JSONException {
    AudioManager am = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);

    JSONObject data = new JSONObject();

    data.put("is_bluetooth_a2dp_on", am.isBluetoothA2dpOn());
    data.put("is_microphone_mute", am.isMicrophoneMute());
    data.put("is_music_active", am.isMusicActive());
    data.put("is_speaker_phone_on", am.isSpeakerphoneOn());
    data.put("is_wired_headset_on", am.isWiredHeadsetOn());

    switch (am.getMode()) {
    case AudioManager.MODE_IN_CALL:
        data.put("mode", "in_call");
        break;//from   w  w  w  .j a v a 2  s  .  c  o  m
    case AudioManager.MODE_IN_COMMUNICATION:
        data.put("mode", "in_comm");
        break;
    case AudioManager.MODE_NORMAL:
        data.put("mode", "normal");
        break;
    case AudioManager.MODE_RINGTONE:
        data.put("mode", "ringtone");
        break;
    case AudioManager.MODE_INVALID:
    default:
        data.put("mode", "invalid");
        break;
    }

    switch (am.getRingerMode()) {
    case AudioManager.RINGER_MODE_VIBRATE:
        data.put("ringer_mode", "vibrate");
        break;
    case AudioManager.RINGER_MODE_SILENT:
        data.put("ringer_mode", "silent");
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        data.put("ringer_mode", "normal");
        break;
    default:
        data.put("ringer_mode", "invalid");
        break;
    }
    return data;
}

From source file:de.tubs.ibr.dtn.chat.service.ChatService.java

@SuppressWarnings("deprecation")
private void showNotification(Intent intent) {
    int defaults = 0;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("vibrateOnMessage", true)) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }/*from w  w w .ja  v  a  2  s  . com*/

    Long buddyId = intent.getLongExtra(EXTRA_BUDDY_ID, -1L);
    String displayName = intent.getStringExtra(EXTRA_DISPLAY_NAME);
    String textBody = intent.getStringExtra(EXTRA_TEXT_BODY);

    CharSequence tickerText = getString(R.string.new_message_from) + " " + displayName;
    CharSequence contentTitle = getString(R.string.new_message);
    CharSequence contentText = displayName + ":\n" + textBody;

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // forward intent to the activity
    intent.setClass(this, MainActivity.class);

    // Adds the intent to the main view
    stackBuilder.addNextIntent(intent);
    // Gets a PendingIntent containing the entire back stack
    PendingIntent contentIntent = stackBuilder.getPendingIntent(buddyId.intValue(),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(contentTitle);
    builder.setContentText(contentText);
    builder.setSmallIcon(R.drawable.ic_message);
    builder.setTicker(tickerText);
    builder.setDefaults(defaults);
    builder.setWhen(System.currentTimeMillis());
    builder.setContentIntent(contentIntent);
    builder.setLights(0xffff0000, 300, 1000);
    builder.setSound(
            Uri.parse(prefs.getString("ringtoneOnMessage", "content://settings/system/notification_sound")));
    builder.setAutoCancel(true);

    Notification notification = builder.getNotification();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(buddyId.toString(), MESSAGE_NOTIFICATION, notification);

    if (prefs.getBoolean("ttsWhenOnHeadset", false)) {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (am.isBluetoothA2dpOn() || am.isWiredHeadsetOn()) {
            // speak the notification
            Intent tts_intent = new Intent(this, TTSService.class);
            tts_intent.setAction(TTSService.INTENT_SPEAK);
            tts_intent.putExtra("speechText", tickerText + ": " + textBody);
            startService(tts_intent);
        }
    }
}

From source file:com.socialdisasters.other.service.ChatService.java

@SuppressWarnings("deprecation")
private void showNotification(Intent intent) {
    int defaults = 0;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (prefs.getBoolean("vibrateOnMessage", true)) {
        defaults |= Notification.DEFAULT_VIBRATE;
    }/* ww  w.  ja v  a2s.co  m*/

    Long buddyId = intent.getLongExtra(EXTRA_BUDDY_ID, -1L);
    String displayName = intent.getStringExtra(EXTRA_DISPLAY_NAME);
    String textBody = intent.getStringExtra(EXTRA_TEXT_BODY);

    CharSequence tickerText = getString(R.string.new_message_from) + " " + displayName;
    CharSequence contentTitle = getString(R.string.new_message);
    CharSequence contentText = displayName + ":\n" + textBody;

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // forward intent to the activity
    intent.setClass(this, MainActivity.class);

    // Adds the intent to the main view
    stackBuilder.addNextIntent(intent);

    // Gets a PendingIntent containing the entire back stack
    PendingIntent contentIntent = stackBuilder.getPendingIntent(buddyId.intValue(),
            PendingIntent.FLAG_UPDATE_CURRENT);

    // create a reply intent
    String replyLabel = getResources().getString(R.string.reply_label);
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(replyLabel).build();

    Intent replyIntent = new Intent(this, ReplyActivity.class);
    replyIntent.putExtra(EXTRA_BUDDY_ID, buddyId);
    PendingIntent replyPendingIntent = PendingIntent.getActivity(this, buddyId.intValue(), replyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply,
            getString(R.string.reply_label), replyPendingIntent).addRemoteInput(remoteInput).build();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(contentTitle);
    builder.setContentText(contentText);
    builder.setSmallIcon(R.drawable.ic_stat_message);
    builder.setTicker(tickerText);
    builder.setDefaults(defaults);
    builder.setWhen(System.currentTimeMillis());
    builder.setContentIntent(contentIntent);
    builder.setLights(0xffff0000, 300, 1000);
    builder.setSound(
            Uri.parse(prefs.getString("ringtoneOnMessage", "content://settings/system/notification_sound")));
    builder.extend(new WearableExtender().addAction(action));
    builder.setAutoCancel(false);

    Notification notification = builder.getNotification();

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(buddyId.toString(), MESSAGE_NOTIFICATION, notification);

    if (prefs.getBoolean("ttsWhenOnHeadset", false)) {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        if (am.isBluetoothA2dpOn() || am.isWiredHeadsetOn()) {
            // speak the notification
            Intent tts_intent = new Intent(this, TTSService.class);
            tts_intent.setAction(TTSService.INTENT_SPEAK);
            tts_intent.putExtra("speechText", tickerText + ": " + textBody);
            startService(tts_intent);
        }
    }
}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private int getCurrentAudioPath() {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (manager == null)
        return AUDIO_PATH_UNKNOWN;

    boolean result = manager.isBluetoothA2dpOn();
    if (result) {
        return AUDIO_PATH_A2DP;
    }//ww  w . j  av  a  2  s  .  co m

    // This method was deprecated in API level 14.
    // Use only to check is a headset is connected or not.
    result = manager.isWiredHeadsetOn();
    if (result) {
        return AUDIO_PATH_WIRED;
    }

    // setSpeakerphoneOn does not work well...
    return AUDIO_PATH_SPEAKER;
}

From source file:com.yohpapa.research.simplemusicplayer.PlaybackService.java

private int getCurrentAudioPathOtherThanWired() {
    AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (manager == null) {
        return AUDIO_PATH_UNKNOWN;
    }/*from  w w w .  j a  v  a  2s .  c o  m*/

    boolean result = manager.isBluetoothA2dpOn();
    if (result) {
        return AUDIO_PATH_A2DP;
    }

    return AUDIO_PATH_SPEAKER;
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void answerCall(boolean isSpeakon) {

    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // ??/*  w w  w .  j  av a2 s  . c  o m*/
    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

        /*TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method mthAnswerCall;
        try {
        mthAnswerCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
        mthAnswerCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthAnswerCall.invoke(telMag, (Object[]) null);
        iTel.answerRingingCall();
                
        } catch (Exception e) {
        e.printStackTrace();
        }*/
        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", "AudioManager answer mode:" + audioManager.getMode() + " speaker mod:"
                    + String.valueOf(audioManager.isSpeakerphoneOn()));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    } else {
        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

    }
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void dialNumber(String phoneNumber, boolean isSpeakon) {
    Context context = getApplicationContext();

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    Constants.log(TAG_NAME, String.format("Dial phone:%s", phoneNumber));
    // ??/*from ww  w  .ja v a2s  . c  o m*/
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse(String.format("tel:%s", phoneNumber)));
    callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    callIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
    startActivity(callIntent);

    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", String.format("AudioManager answer mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    }
}