switchToBluetooth method

Future<bool> switchToBluetooth()

Implementation

Future<bool> switchToBluetooth() async {
  Log().info('switchToBluetooth');
  final androidAudioManager =
  !kIsWeb && Platform.isAndroid ? AndroidAudioManager() : null;
  if (androidAudioManager != null) {
    await androidAudioManager
        .setMode(AndroidAudioHardwareMode.inCommunication);
    await androidAudioManager.startBluetoothSco();
    await androidAudioManager.setBluetoothScoOn(true);
    _currentInput = AudioDevice(id: "Bluetooth", name: "Bluetooth",
        isInput: false, isOutput: true,
        type: AudioDeviceType.bluetoothLe);
    return true;
  } else {
    final avAudioSession = !kIsWeb && Platform.isIOS ? AVAudioSession() : null;
    if (avAudioSession != null) {
      Future.delayed(const Duration(milliseconds: 1), () async {
        final result = await _switchToAnyIosPortIn({
          AVAudioSessionPort.bluetoothLe,
          AVAudioSessionPort.bluetoothHfp,
          AVAudioSessionPort.bluetoothA2dp,
        });
      });
      // if (result == true) {
      //   _currentInput = AudioDevice(id: "Bluetooth", name: "Bluetooth",
      //       isInput: false, isOutput: true,
      //       type: AudioDeviceType.bluetoothLe);
      // }
      // return result;
      _currentInput = AudioDevice(id: "Bluetooth",
          name: "Bluetooth",
          isInput: false,
          isOutput: true,
          type: AudioDeviceType.bluetoothLe);
      return true; // Always return true to avoid delay update ui
    }
  }

  return false;
}