private boolean addDevicePreference(CachedBluetoothDevice cachedDevice) {
    ParcelUuid[] uuids = cachedDevice.getDevice().getUuids();
    BluetoothClass bluetoothClass = cachedDevice.getDevice().getBluetoothClass();

    switch (mFilterType) {
      case BluetoothDevicePicker.FILTER_TYPE_TRANSFER:
        if (uuids != null)
          if (BluetoothUuid.containsAnyUuid(uuids, LocalBluetoothProfileManager.OPP_PROFILE_UUIDS))
            return true;
        if (bluetoothClass != null && bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_OPP)) {
          return true;
        }
        break;
      case BluetoothDevicePicker.FILTER_TYPE_AUDIO:
        if (uuids != null) {
          if (BluetoothUuid.containsAnyUuid(uuids, LocalBluetoothProfileManager.A2DP_PROFILE_UUIDS))
            return true;

          if (BluetoothUuid.containsAnyUuid(
              uuids, LocalBluetoothProfileManager.HEADSET_PROFILE_UUIDS)) return true;
        } else if (bluetoothClass != null) {
          if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_A2DP)) return true;

          if (bluetoothClass.doesClassMatch(BluetoothClass.PROFILE_HEADSET)) return true;
        }
        break;
      default:
        return true;
    }
    return false;
  }
  private synchronized void getSdpRecordsAndConnect(BluetoothDevice device) {
    if (!device.equals(getCurrentDevice())) {
      // stale
      return;
    }

    // Check if incoming connection has already connected.
    if (mRemoteHeadsets.get(device).mState == BluetoothHeadset.STATE_CONNECTED) {
      return;
    }

    ParcelUuid[] uuids = device.getUuids();
    int type = BluetoothHandsfree.TYPE_UNKNOWN;
    if (uuids != null) {
      if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Handsfree)) {
        log("SDP UUID: TYPE_HANDSFREE");
        type = BluetoothHandsfree.TYPE_HANDSFREE;
        mRemoteHeadsets.get(device).mHeadsetType = type;
        int channel = device.getServiceChannel(BluetoothUuid.Handsfree);
        mConnectThread = new RfcommConnectThread(device, channel, type);
        if (mAdapter.isDiscovering()) {
          mAdapter.cancelDiscovery();
        }
        mConnectThread.start();
        if (getPriority(device) < BluetoothHeadset.PRIORITY_AUTO_CONNECT) {
          setPriority(device, BluetoothHeadset.PRIORITY_AUTO_CONNECT);
        }
        return;
      } else if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HSP)) {
        log("SDP UUID: TYPE_HEADSET");
        type = BluetoothHandsfree.TYPE_HEADSET;
        mRemoteHeadsets.get(device).mHeadsetType = type;
        int channel = device.getServiceChannel(BluetoothUuid.HSP);
        mConnectThread = new RfcommConnectThread(device, channel, type);
        if (mAdapter.isDiscovering()) {
          mAdapter.cancelDiscovery();
        }
        mConnectThread.start();
        if (getPriority(device) < BluetoothHeadset.PRIORITY_AUTO_CONNECT) {
          setPriority(device, BluetoothHeadset.PRIORITY_AUTO_CONNECT);
        }
        return;
      }
    }
    log("SDP UUID: TYPE_UNKNOWN");
    mRemoteHeadsets.get(device).mHeadsetType = type;
    setState(
        device,
        BluetoothHeadset.STATE_DISCONNECTED,
        BluetoothHeadset.RESULT_FAILURE,
        BluetoothHeadset.LOCAL_DISCONNECT);
    return;
  }
예제 #3
0
  public boolean connect(BluetoothDevice device) {
    enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");

    if (getPriority(device) == BluetoothProfile.PRIORITY_OFF) {
      return false;
    }
    ParcelUuid[] featureUuids = device.getUuids();
    if ((BluetoothUuid.containsAnyUuid(featureUuids, A2DP_SOURCE_UUID))
        && !(BluetoothUuid.containsAllUuids(featureUuids, A2DP_SOURCE_SINK_UUIDS))) {
      Log.e(TAG, "Remote does not have A2dp Sink UUID");
      return false;
    }

    int connectionState = mStateMachine.getConnectionState(device);
    if (connectionState == BluetoothProfile.STATE_CONNECTED
        || connectionState == BluetoothProfile.STATE_CONNECTING) {
      return false;
    }

    mStateMachine.sendMessage(A2dpStateMachine.CONNECT, device);
    return true;
  }
  synchronized List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
    List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();
    Set<BluetoothDevice> bondedDevices = mAdapter.getBondedDevices();
    int connectionState;

    for (BluetoothDevice device : bondedDevices) {
      ParcelUuid[] featureUuids = device.getUuids();
      if (!BluetoothUuid.isUuidPresent(featureUuids, BluetoothUuid.AudioSource)) {
        continue;
      }
      connectionState = getConnectionState(device);
      for (int i = 0; i < states.length; i++) {
        if (connectionState == states[i]) {
          deviceList.add(device);
        }
      }
    }
    return deviceList;
  }