Java 类android.bluetooth.BluetoothHeadset 实例源码

项目:lrs_android    文件:AgoraActivity.java   
private void optional() {
        HeadsetPlugManager.getInstance().registerHeadsetPlugListener(this);
        mHeadsetListener = new HeadsetBroadcastReceiver();
        registerReceiver(mHeadsetListener, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
        mBluetoothHeadsetBroadcastListener = new BluetoothHeadsetBroadcastReceiver();
        mBtAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBtAdapter != null && BluetoothProfile.STATE_CONNECTED == mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {
            // on some devices, BT is not supported
            boolean bt = mBtAdapter.getProfileProxy(getBaseContext(), mBluetoothHeadsetListener, BluetoothProfile.HEADSET);
            int connection = mBtAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
        }
        IntentFilter i = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
        i.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
        i.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
        registerReceiver(mBluetoothHeadsetBroadcastListener, i);
//避免对window添加ui修改参数
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

        setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
项目:Linphone4Android    文件:BluetoothManager.java   
public void initBluetooth() {
    if (!ensureInit()) {
        Log.w("[Bluetooth] Manager tried to init bluetooth but LinphoneService not ready yet...");
        return;
    }

    IntentFilter filter = new IntentFilter();
    filter.addCategory(BluetoothHeadset.VENDOR_SPECIFIC_HEADSET_EVENT_COMPANY_ID_CATEGORY + "." + BluetoothAssignedNumbers.PLANTRONICS);
    filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
    filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
    filter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);
    mContext.registerReceiver(this,  filter);
    Log.d("[Bluetooth] Receiver started");

    startBluetooth();
}
项目:Linphone4Android    文件:BluetoothManager.java   
public boolean isBluetoothHeadsetAvailable() {
    ensureInit();
    if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager != null && mAudioManager.isBluetoothScoAvailableOffCall()) {
        boolean isHeadsetConnected = false;
        if (mBluetoothHeadset != null) {
            List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
            mBluetoothDevice = null;
            for (final BluetoothDevice dev : devices) {    
                if (mBluetoothHeadset.getConnectionState(dev) == BluetoothHeadset.STATE_CONNECTED) {
                    mBluetoothDevice = dev;
                    isHeadsetConnected = true;
                    break;
                }
            }
            Log.d(isHeadsetConnected ? "[Bluetooth] Headset found, bluetooth audio route available" : "[Bluetooth] No headset found, bluetooth audio route unavailable");
        }
        return isHeadsetConnected;
    }

    return false;
}
项目:PeSanKita-android    文件:BluetoothStateManager.java   
public BluetoothStateManager(@NonNull Context context, @Nullable BluetoothStateListener listener) {
    this.context                     = context.getApplicationContext();
    this.bluetoothAdapter            = BluetoothAdapter.getDefaultAdapter();
    this.bluetoothScoReceiver        = new BluetoothScoReceiver();
    this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver();
    this.listener                    = listener;

    requestHeadsetProxyProfile();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        context.registerReceiver(bluetoothConnectionReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
    }

    Intent sticky = context.registerReceiver(bluetoothScoReceiver, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED));

    if (sticky != null) {
        bluetoothScoReceiver.onReceive(context, sticky);
    }

    handleBluetoothStateChange();
}
项目:Cable-Android    文件:BluetoothStateManager.java   
public BluetoothStateManager(@NonNull Context context, @Nullable BluetoothStateListener listener) {
  this.context                     = context.getApplicationContext();
  this.bluetoothAdapter            = BluetoothAdapter.getDefaultAdapter();
  this.bluetoothScoReceiver        = new BluetoothScoReceiver();
  this.bluetoothConnectionReceiver = new BluetoothConnectionReceiver();
  this.listener                    = listener;

  if (this.bluetoothAdapter == null)
    return;

  requestHeadsetProxyProfile();

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    context.registerReceiver(bluetoothConnectionReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
  }

  Intent sticky = context.registerReceiver(bluetoothScoReceiver, new IntentFilter(getScoChangeIntent()));

  if (sticky != null) {
    bluetoothScoReceiver.onReceive(context, sticky);
  }

  handleBluetoothStateChange();
}
项目:BluetoothRecord    文件:MainActivity.java   
public void onServiceConnected(int profile, BluetoothProfile proxy) {
    if (profile == BluetoothProfile.HEADSET) {
        mBluetoothHeadset = (BluetoothHeadset) proxy;
        List<BluetoothDevice> pairedDevices = mBluetoothHeadset.getConnectedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            startSCO();
            for (BluetoothDevice device : pairedDevices) {
                Log.e(TAG, "BT Device :"+device.getName()+ " , BD_ADDR:" + device.getAddress());       //Print out Headset name      
            }
        } else {
            Toast.makeText(mContext, "Could not find a connected Headset, please connect a headset", Toast.LENGTH_LONG).show();
            return;
        }
    }
}
项目:Telegram    文件:VoIPBaseService.java   
@Override
public void onReceive(Context context, Intent intent) {
    if (ACTION_HEADSET_PLUG.equals(intent.getAction())) {
        isHeadsetPlugged = intent.getIntExtra("state", 0) == 1;
        if (isHeadsetPlugged && proximityWakelock != null && proximityWakelock.isHeld()) {
            proximityWakelock.release();
        }
        isProximityNear = false;
        updateOutputGainControlState();
    } else if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        updateNetworkType();
    } else if(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())){
        //FileLog.e("bt headset state = "+intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0));
        updateBluetoothHeadsetState(intent.getIntExtra(BluetoothProfile.EXTRA_STATE, 0)==BluetoothProfile.STATE_CONNECTED);
    }else if(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED.equals(intent.getAction())){
        for (StateListener l : stateListeners)
            l.onAudioSettingsChanged();
    }else if(TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction())){
        String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        if(TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)){
            hangUp();
        }
    }
}
项目:Linphone4Android    文件:BluetoothManager.java   
private void startBluetooth() {
    if (isBluetoothConnected) {
        Log.e("[Bluetooth] Already started, skipping...");
        return;
    }

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
        if (mProfileListener != null) {
            Log.w("[Bluetooth] Headset profile was already opened, let's close it");
            mBluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, mBluetoothHeadset);
        }

        mProfileListener = new BluetoothProfile.ServiceListener() {
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                if (profile == BluetoothProfile.HEADSET) {
                    Log.d("[Bluetooth] Headset connected");
                    mBluetoothHeadset = (BluetoothHeadset) proxy;
                    isBluetoothConnected = true;
                }
            }
            public void onServiceDisconnected(int profile) {
                if (profile == BluetoothProfile.HEADSET) {
                    mBluetoothHeadset = null;
                    isBluetoothConnected = false;
                    Log.d("[Bluetooth] Headset disconnected");
                    LinphoneManager.getInstance().routeAudioToReceiver();
                }
            }
        };
        boolean success = mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.HEADSET);
        if (!success) {
            Log.e("[Bluetooth] getProfileProxy failed !");
        }
    } else {
        Log.w("[Bluetooth] Interface disabled on device");
    }
}
项目:AssistantBySDK    文件:AssistantService.java   
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
    Log.e("blueHeadsetListener", "onServiceConnected:" + profile);
    if (profile == BluetoothProfile.A2DP) {
        voiceMediator.setBluetoothA2dp((BluetoothA2dp) proxy);
    } else if (profile == BluetoothProfile.HEADSET) {
        voiceMediator.setBluetoothHeadset((BluetoothHeadset) proxy);
    }
}
项目:AssistantBySDK    文件:VoiceMediator.java   
@Override
public void startBluetoothSco() {
    if (isBlueToothHeadSet()) {
        BluetoothHeadset headset = getBluetoothHeadset();
        if (headset == null || headset.getConnectedDevices().isEmpty() || headset.isAudioConnected(headset.getConnectedDevices().get(0)))
            return;
        Log.e(TAG, "startBluetoothSco:device size=" + headset.getConnectedDevices().size());
        headset.startVoiceRecognition(headset.getConnectedDevices().get(0));
    }
}
项目:nc-android-webrtcpeer    文件:BluetoothManager.java   
@Override
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
public void onServiceConnected(int profile, BluetoothProfile proxy) {
    if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
        return;
    }
    Log.d(TAG, "BluetoothServiceListener.onServiceConnected: BT state=" + bluetoothState);
    // Android only supports one connected Bluetooth Headset at a time.
    bluetoothHeadset = (BluetoothHeadset) proxy;
    updateAudioDeviceState();
    Log.d(TAG, "onServiceConnected done: BT state=" + bluetoothState);
}
项目:lrs_android    文件:BluetoothHeadsetBroadcastReceiver.java   
@Override
public void onReceive(Context context, Intent intent) {

    BluetoothDevice mConnectedHeadset = null;

    String action = intent.getAction();
    int state = BluetoothHeadset.STATE_DISCONNECTED;
    int previousState = intent.getIntExtra(BluetoothHeadset.EXTRA_PREVIOUS_STATE, BluetoothHeadset.STATE_DISCONNECTED);
    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
        state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
        if (state == BluetoothHeadset.STATE_CONNECTED) {
            mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            log.debug("AudioManager ACTION_CONNECTION_STATE_CHANGED " + mConnectedHeadset + " " + state);
            HeadsetPlugManager.getInstance().notifyHeadsetPlugged(true, HeadsetPlugManager.BLUETOOTH, mConnectedHeadset);
        } else if (state == BluetoothHeadset.STATE_DISCONNECTED) {
            mConnectedHeadset = null;
            log.debug("AudioManager ACTION_CONNECTION_STATE_CHANGED " + " " + state);
            HeadsetPlugManager.getInstance().notifyHeadsetPlugged(false, HeadsetPlugManager.BLUETOOTH);
        }
    } else if (BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED.equals(action)) {
        state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
        log.debug("AudioManager ACTION_AUDIO_STATE_CHANGED " + " " + state);
        if (state == BluetoothHeadset.STATE_AUDIO_CONNECTED) {
            // bluetooth audio connected. you send audio stream to headset now!!!
        } else if (state == BluetoothHeadset.STATE_AUDIO_DISCONNECTED) {
        }
    } else if (AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED.equals(action)) {
        state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_DISCONNECTED);
        log.debug("AudioManager ACTION_SCO_AUDIO_STATE_UPDATED " + " " + state);
    }
}
项目:AppRTC-Android    文件:AppRTCBluetoothManager.java   
@Override
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
public void onServiceConnected(int profile, BluetoothProfile proxy) {
  if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
    return;
  }
  Log.d(TAG, "BluetoothServiceListener.onServiceConnected: BT state=" + bluetoothState);
  // Android only supports one connected Bluetooth Headset at a time.
  bluetoothHeadset = (BluetoothHeadset) proxy;
  updateAudioDeviceState();
  Log.d(TAG, "onServiceConnected done: BT state=" + bluetoothState);
}
项目:AndroidRTC    文件:AppRTCBluetoothManager.java   
@Override
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
public void onServiceConnected(int profile, BluetoothProfile proxy) {
    if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
        return;
    }
    Log.d(TAG, "BluetoothServiceListener.onServiceConnected: BT state=" + bluetoothState);
    // Android only supports one connected Bluetooth Headset at a time.
    bluetoothHeadset = (BluetoothHeadset) proxy;
    updateAudioDeviceState();
    Log.d(TAG, "onServiceConnected done: BT state=" + bluetoothState);
}
项目:react-native-incall-manager    文件:AppRTCBluetoothManager.java   
@Override
// Called to notify the client when the proxy object has been connected to the service.
// Once we have the profile proxy object, we can use it to monitor the state of the
// connection and perform other operations that are relevant to the headset profile.
public void onServiceConnected(int profile, BluetoothProfile proxy) {
  if (profile != BluetoothProfile.HEADSET || bluetoothState == State.UNINITIALIZED) {
    return;
  }
  Log.d(TAG, "BluetoothServiceListener.onServiceConnected: BT state=" + bluetoothState);
  // Android only supports one connected Bluetooth Headset at a time.
  bluetoothHeadset = (BluetoothHeadset) proxy;
  updateAudioDeviceState();
  Log.d(TAG, "onServiceConnected done: BT state=" + bluetoothState);
}
项目:PebbleAndroidCommons    文件:BluetoothHeadsetListener.java   
@Override
public void onReceive(Context context, Intent intent)
{
    int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);

    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putBoolean(SETTING_HEADSET_CONNECTED, state == BluetoothHeadset.STATE_CONNECTED);
    editor.apply();
}
项目:ring-client-android    文件:BluetoothWrapper.java   
public void onReceive(Context context, Intent intent) {
    Log.d(TAG, "BT state changed");
    String action = intent.getAction();
    if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
        int status = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0);
        if (status == 2) {
            Log.d(TAG, "BT device found");
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            if (btChangesListener != null) {
                btChangesListener.onBluetoothStateChanged(status);
            }
        }
    }
}
项目:opentok-android-sdk-samples    文件:CustomAudioDevice.java   
private void registerBtReceiver() {
    if (!scoReceiverRegistered) {
        context.registerReceiver(
                btStatusReceiver,
                new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)
        );
        scoReceiverRegistered = true;
    }
}
项目:opentok-android-sdk-samples    文件:CustomAudioDevice.java   
private void disableBluetoothEvents() {
    if (null != bluetoothProfile && bluetoothAdapter != null) {
        bluetoothAdapter.closeProfileProxy(BluetoothProfile.HEADSET, bluetoothProfile);
    }
    unregisterBtReceiver();
    /* force a shutdown of bluetooth: when a call comes in, the handler is not invoked by system */
    Intent intent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
    intent.putExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_DISCONNECTED);
    btStatusReceiver.onReceive(context, intent);
}
项目:AssistantBySDK    文件:VoiceMediator.java   
public void setBluetoothHeadset(BluetoothHeadset bluetoothHeadset) {
    this.bluetoothHeadset = bluetoothHeadset;
}
项目:AssistantBySDK    文件:VoiceMediator.java   
@Override
public BluetoothHeadset getBluetoothHeadset() {
    return bluetoothHeadset;
}
项目:nc-android-webrtcpeer    文件:BluetoothManager.java   
/**
 * Activates components required to detect Bluetooth devices and to enable
 * BT SCO (audio is routed via BT SCO) for the headset profile. The end
 * state will be HEADSET_UNAVAILABLE but a state machine has started which
 * will start a state change sequence where the final outcome depends on
 * if/when the BT headset is enabled.
 * Example of state change sequence when start() is called while BT device
 * is connected and enabled:
 * UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
 * SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
 * Note that the AppRTCAudioManager is also involved in driving this state
 * change.
 */
public void start() {
    ThreadUtils.checkIsOnMainThread();
    Log.d(TAG, "start");
    if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
        Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
        return;
    }
    if (bluetoothState != State.UNINITIALIZED) {
        Log.w(TAG, "Invalid BT state");
        return;
    }
    bluetoothHeadset = null;
    bluetoothDevice = null;
    scoConnectionAttempts = 0;
    // Get a handle to the default local Bluetooth adapter.
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        Log.w(TAG, "Device does not support Bluetooth");
        return;
    }
    // Ensure that the device supports use of BT SCO audio for off call use cases.
    if (!audioManager.isBluetoothScoAvailableOffCall()) {
        Log.e(TAG, "Bluetooth SCO audio is not available off call");
        return;
    }
    logBluetoothAdapterInfo(bluetoothAdapter);
    // Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
    // Hands-Free) proxy object and install a listener.
    if (!getBluetoothProfileProxy(
            apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET)) {
        Log.e(TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed");
        return;
    }
    // Register receivers for BluetoothHeadset change notifications.
    IntentFilter bluetoothHeadsetFilter = new IntentFilter();
    // Register receiver for change in connection state of the Headset profile.
    bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
    // Register receiver for change in audio connection state of the Headset profile.
    bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
    registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter);
    Log.d(TAG, "HEADSET profile state: "
               + stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
    Log.d(TAG, "Bluetooth proxy for headset profile has started");
    bluetoothState = State.HEADSET_UNAVAILABLE;
    Log.d(TAG, "start done: BT state=" + bluetoothState);
}
项目:AppRTC-Android    文件:AppRTCBluetoothManager.java   
/**
 * Activates components required to detect Bluetooth devices and to enable
 * BT SCO (audio is routed via BT SCO) for the headset profile. The end
 * state will be HEADSET_UNAVAILABLE but a state machine has started which
 * will start a state change sequence where the final outcome depends on
 * if/when the BT headset is enabled.
 * Example of state change sequence when start() is called while BT device
 * is connected and enabled:
 *   UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
 *   SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
 * Note that the AppRTCAudioManager is also involved in driving this state
 * change.
 */
public void start() {
  ThreadUtils.checkIsOnMainThread();
  Log.d(TAG, "start");
  if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
    Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
    return;
  }
  if (bluetoothState != State.UNINITIALIZED) {
    Log.w(TAG, "Invalid BT state");
    return;
  }
  bluetoothHeadset = null;
  bluetoothDevice = null;
  scoConnectionAttempts = 0;
  // Get a handle to the default local Bluetooth adapter.
  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if (bluetoothAdapter == null) {
    Log.w(TAG, "Device does not support Bluetooth");
    return;
  }
  // Ensure that the device supports use of BT SCO audio for off call use cases.
  if (!audioManager.isBluetoothScoAvailableOffCall()) {
    Log.e(TAG, "Bluetooth SCO audio is not available off call");
    return;
  }
  logBluetoothAdapterInfo(bluetoothAdapter);
  // Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
  // Hands-Free) proxy object and install a listener.
  if (!getBluetoothProfileProxy(
          apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET)) {
    Log.e(TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed");
    return;
  }
  // Register receivers for BluetoothHeadset change notifications.
  IntentFilter bluetoothHeadsetFilter = new IntentFilter();
  // Register receiver for change in connection state of the Headset profile.
  bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
  // Register receiver for change in audio connection state of the Headset profile.
  bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
  registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter);
  Log.d(TAG, "HEADSET profile state: "
          + stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
  Log.d(TAG, "Bluetooth proxy for headset profile has started");
  bluetoothState = State.HEADSET_UNAVAILABLE;
  Log.d(TAG, "start done: BT state=" + bluetoothState);
}
项目:AndroidRTC    文件:AppRTCBluetoothManager.java   
/**
 * Activates components required to detect Bluetooth devices and to enable
 * BT SCO (audio is routed via BT SCO) for the headset profile. The end
 * state will be HEADSET_UNAVAILABLE but a state machine has started which
 * will start a state change sequence where the final outcome depends on
 * if/when the BT headset is enabled.
 * Example of state change sequence when start() is called while BT device
 * is connected and enabled:
 * UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
 * SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
 * Note that the AppRTCAudioManager is also involved in driving this state
 * change.
 */
public void start() {
    ThreadUtils.checkIsOnMainThread();
    Log.d(TAG, "start");
    if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
        Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
        return;
    }
    if (bluetoothState != State.UNINITIALIZED) {
        Log.w(TAG, "Invalid BT state");
        return;
    }
    bluetoothHeadset = null;
    bluetoothDevice = null;
    scoConnectionAttempts = 0;
    // Get a handle to the default local Bluetooth adapter.
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        Log.w(TAG, "Device does not support Bluetooth");
        return;
    }
    // Ensure that the device supports use of BT SCO audio for off call use cases.
    if (!audioManager.isBluetoothScoAvailableOffCall()) {
        Log.e(TAG, "Bluetooth SCO audio is not available off call");
        return;
    }
    logBluetoothAdapterInfo(bluetoothAdapter);
    // Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
    // Hands-Free) proxy object and install a listener.
    if (!getBluetoothProfileProxy(
            apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET)) {
        Log.e(TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed");
        return;
    }
    // Register receivers for BluetoothHeadset change notifications.
    IntentFilter bluetoothHeadsetFilter = new IntentFilter();
    // Register receiver for change in connection state of the Headset profile.
    bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
    // Register receiver for change in audio connection state of the Headset profile.
    bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
    registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter);
    Log.d(TAG, "HEADSET profile state: "
            + stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
    Log.d(TAG, "Bluetooth proxy for headset profile has started");
    bluetoothState = State.HEADSET_UNAVAILABLE;
    Log.d(TAG, "start done: BT state=" + bluetoothState);
}
项目:AndroidRTC    文件:BluetoothManagerTest.java   
@Before
public void setUp() {
    ShadowLog.stream = System.out;
    context = ShadowApplication.getInstance().getApplicationContext();
    mockedAppRtcAudioManager = mock(AppRTCAudioManager.class);
    mockedAudioManager = mock(AudioManager.class);
    mockedBluetoothHeadset = mock(BluetoothHeadset.class);
    mockedBluetoothDevice = mock(BluetoothDevice.class);
    mockedBluetoothDeviceList = new LinkedList<BluetoothDevice>();

    // Simulate that bluetooth SCO audio is available by default.
    when(mockedAudioManager.isBluetoothScoAvailableOffCall()).thenReturn(true);

    // Create the test object and override protected methods for this test.
    bluetoothManager = new AppRTCBluetoothManager(context, mockedAppRtcAudioManager) {
        @Override
        protected AudioManager getAudioManager(Context context) {
            Log.d(TAG, "getAudioManager");
            return mockedAudioManager;
        }

        @Override
        protected void registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
            Log.d(TAG, "registerReceiver");
            if (filter.hasAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)
                    && filter.hasAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED)) {
                // Gives access to the real broadcast receiver so the test can use it.
                bluetoothHeadsetStateReceiver = receiver;
            }
        }

        @Override
        protected void unregisterReceiver(BroadcastReceiver receiver) {
            Log.d(TAG, "unregisterReceiver");
            if (receiver == bluetoothHeadsetStateReceiver) {
                bluetoothHeadsetStateReceiver = null;
            }
        }

        @Override
        protected boolean getBluetoothProfileProxy(
                Context context, BluetoothProfile.ServiceListener listener, int profile) {
            Log.d(TAG, "getBluetoothProfileProxy");
            if (profile == BluetoothProfile.HEADSET) {
                // Allows the test to access the real Bluetooth service listener object.
                bluetoothServiceListener = listener;
            }
            return true;
        }

        @Override
        protected boolean hasPermission(Context context, String permission) {
            Log.d(TAG, "hasPermission(" + permission + ")");
            // Ensure that the client asks for Bluetooth permission.
            return (permission == android.Manifest.permission.BLUETOOTH);
        }

        @Override
        protected void logBluetoothAdapterInfo(BluetoothAdapter localAdapter) {
            // Do nothing in tests. No need to mock BluetoothAdapter.
        }
    };
}
项目:AndroidRTC    文件:BluetoothManagerTest.java   
private void simulateBluetoothHeadsetConnected() {
    Intent intent = new Intent();
    intent.setAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
    intent.putExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_CONNECTED);
    bluetoothHeadsetStateReceiver.onReceive(context, intent);
}
项目:AndroidRTC    文件:BluetoothManagerTest.java   
private void simulateBluetoothScoConnectionConnected() {
    Intent intent = new Intent();
    intent.setAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
    intent.putExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_CONNECTED);
    bluetoothHeadsetStateReceiver.onReceive(context, intent);
}
项目:AndroidRTC    文件:BluetoothManagerTest.java   
private void simulateBluetoothScoConnectionDisconnected() {
    Intent intent = new Intent();
    intent.setAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
    intent.putExtra(BluetoothHeadset.EXTRA_STATE, BluetoothHeadset.STATE_AUDIO_DISCONNECTED);
    bluetoothHeadsetStateReceiver.onReceive(context, intent);
}
项目:react-native-incall-manager    文件:AppRTCBluetoothManager.java   
/**
 * Activates components required to detect Bluetooth devices and to enable
 * BT SCO (audio is routed via BT SCO) for the headset profile. The end
 * state will be HEADSET_UNAVAILABLE but a state machine has started which
 * will start a state change sequence where the final outcome depends on
 * if/when the BT headset is enabled.
 * Example of state change sequence when start() is called while BT device
 * is connected and enabled:
 *   UNINITIALIZED --> HEADSET_UNAVAILABLE --> HEADSET_AVAILABLE -->
 *   SCO_CONNECTING --> SCO_CONNECTED <==> audio is now routed via BT SCO.
 * Note that the AppRTCAudioManager is also involved in driving this state
 * change.
 */
public void start() {
  Log.d(TAG, "start");
  if (!hasPermission(apprtcContext, android.Manifest.permission.BLUETOOTH)) {
    Log.w(TAG, "Process (pid=" + Process.myPid() + ") lacks BLUETOOTH permission");
    return;
  }
  if (bluetoothState != State.UNINITIALIZED) {
    Log.w(TAG, "Invalid BT state");
    return;
  }
  bluetoothHeadset = null;
  bluetoothDevice = null;
  scoConnectionAttempts = 0;
  // Get a handle to the default local Bluetooth adapter.
  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if (bluetoothAdapter == null) {
    Log.w(TAG, "Device does not support Bluetooth");
    return;
  }
  // Ensure that the device supports use of BT SCO audio for off call use cases.
  if (!audioManager.isBluetoothScoAvailableOffCall()) {
    Log.e(TAG, "Bluetooth SCO audio is not available off call");
    return;
  }
  logBluetoothAdapterInfo(bluetoothAdapter);
  // Establish a connection to the HEADSET profile (includes both Bluetooth Headset and
  // Hands-Free) proxy object and install a listener.
  if (!getBluetoothProfileProxy(
          apprtcContext, bluetoothServiceListener, BluetoothProfile.HEADSET)) {
    Log.e(TAG, "BluetoothAdapter.getProfileProxy(HEADSET) failed");
    return;
  }
  // Register receivers for BluetoothHeadset change notifications.
  IntentFilter bluetoothHeadsetFilter = new IntentFilter();
  // Register receiver for change in connection state of the Headset profile.
  bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
  // Register receiver for change in audio connection state of the Headset profile.
  bluetoothHeadsetFilter.addAction(BluetoothHeadset.ACTION_AUDIO_STATE_CHANGED);
  registerReceiver(bluetoothHeadsetReceiver, bluetoothHeadsetFilter);
  Log.d(TAG, "HEADSET profile state: "
          + stateToString(bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)));
  Log.d(TAG, "Bluetooth proxy for headset profile has started");
  bluetoothState = State.HEADSET_UNAVAILABLE;
  Log.d(TAG, "start done: BT state=" + bluetoothState);
}
项目:ring-client-android    文件:BluetoothWrapper.java   
public void registerBtConnection() {
    Log.d(TAG, "Register BT connection");
    mContext.registerReceiver(mBtReceiver, new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED));
}
项目:Telegram    文件:VoIPBaseService.java   
@Override
public void onCreate() {
    super.onCreate();
    FileLog.d("=============== VoIPService STARTING ===============");
    AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER)!=null) {
        int outFramesPerBuffer = Integer.parseInt(am.getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER));
        VoIPController.setNativeBufferSize(outFramesPerBuffer);
    } else {
        VoIPController.setNativeBufferSize(AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) / 2);
    }
    updateServerConfig();
    try {
        controller = createController();
        controller.setConnectionStateListener(this);

        cpuWakelock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram-voip");
        cpuWakelock.acquire();

        btAdapter=am.isBluetoothScoAvailableOffCall() ? BluetoothAdapter.getDefaultAdapter() : null;

        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(ACTION_HEADSET_PLUG);
        if(btAdapter!=null){
            filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
            filter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
        }
        filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
        registerReceiver(receiver, filter);

        ConnectionsManager.getInstance().setAppPaused(false, false);

        soundPool = new SoundPool(1, AudioManager.STREAM_VOICE_CALL, 0);
        spConnectingId = soundPool.load(this, R.raw.voip_connecting, 1);
        spRingbackID = soundPool.load(this, R.raw.voip_ringback, 1);
        spFailedID = soundPool.load(this, R.raw.voip_failed, 1);
        spEndId = soundPool.load(this, R.raw.voip_end, 1);
        spBusyId = soundPool.load(this, R.raw.voip_busy, 1);

        am.registerMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));

        if(btAdapter!=null && btAdapter.isEnabled()){
            int headsetState=btAdapter.getProfileConnectionState(BluetoothProfile.HEADSET);
            updateBluetoothHeadsetState(headsetState==BluetoothProfile.STATE_CONNECTED);
            if(headsetState==BluetoothProfile.STATE_CONNECTED)
                am.setBluetoothScoOn(true);
            for (StateListener l : stateListeners)
                l.onAudioSettingsChanged();
        }

        NotificationCenter.getInstance().addObserver(this, NotificationCenter.appDidLogout);
    } catch (Exception x) {
        FileLog.e("error initializing voip controller", x);
        callFailed();
    }
}
项目:AssistantBySDK    文件:SystemVoiceMediator.java   
/**
 * 获取蓝牙耳机对象
 **/
BluetoothHeadset getBluetoothHeadset();