0

I am trying to implement MethodChannel in Flutter so that I can call Android Native Code within my Flutter application. The code for this purpose in main.dart is as follows:

class _MyHomePageState extends State<MyHomePage> {
  static const frequencyChannel =
      MethodChannel('com.somdipdey.eoptomizer/frequency');
  bool optimizing = false;
  String bodyText = 'Default';

  void _optimize() {
    setState(() {
      if (!optimizing) {
        optimizationOn();
      } else {
        defaultScheduler();
      }
    });
  }
void optimizationOn() {
    debugPrint('Optimizing battery and thermal behaviour');
    setFrequencyLevelCPU0Max();
    setFrequencyLevelCPU4Max();
    optimizing = true;
    bodyText = 'Optimization On';
  }
Future setFrequencyLevelCPU0Max() async {
    var sendMapMaxCPUFreq = <String, String>{
      "LITTLECPUMaxFreq": _LITTLECPUFreq.text,
      "bigCPUMaxFreq": _bigCPUFreq.text
    };
    debugPrint(_LITTLECPUFreq.text);
    final String frequencyLevelCPU0Max = await frequencyChannel.invokeMethod(
        'setFrequencyLevelCPU0Max', sendMapMaxCPUFreq);
    debugPrint('Optimizing CPU0 Max Frequency');
    debugPrint(frequencyLevelCPU0Max);
  }
Future setFrequencyLevelCPU4Max() async {
    var sendMapMaxCPUFreq = <String, String>{
      "LITTLECPUMaxFreq": _LITTLECPUFreq.text,
      "bigCPUMaxFreq": _bigCPUFreq.text
    };
    final String frequencyLevelCPU4Max = await frequencyChannel.invokeMethod(
        'setFrequencyLevelCPU4Max', sendMapMaxCPUFreq);
    debugPrint('Optimizing CPU4 Max Frequency');
    debugPrint(frequencyLevelCPU4Max);
  }
...
...
...

Note that I am sending the values from Flutter side using the map variable sendMapMaxCPUFreq to MethodChannel.

In the Kotlin side, in MainActivity.kt, the code is as follows:

class MainActivity: FlutterActivity() {


    private val BATTERY_CHANNEL = "com.somdipdey.eoptomizer/frequency"
    private lateinit var channel: MethodChannel

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, BATTERY_CHANNEL)
        channel.setMethodCallHandler { call, result ->
            if (call.method == "setFrequencyLevelCPU0Max" || call.method == "setFrequencyLevelCPU4Max") {
                val arguments = call.arguments() ?: {"LITTLECPUMaxFreq": "1766400", "bigCPUMaxFreq": "2649600"} as Map<String, String>
                val LITTLECPUMaxFreq = arguments["LITTLECPUMaxFreq"]
                val bigCPUMaxFreq = arguments["bigCPUMaxFreq"]
                setFrequencyLevelCPU0Max(LITTLECPUMaxFreq)
                setFrequencyLevelCPU4Max(bigCPUMaxFreq)
                result.success("Optimizing frequencies on CPU0, CPU4")
            }
  }
}
    fun setFrequencyLevelCPU0Max(freq: String): Unit {
        val process: java.lang.Process = java.lang.Runtime.getRuntime().exec("su -c echo $freq > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq")
        process.waitFor()
    }
    fun setFrequencyLevelCPU4Max(freq: String): Unit {
        val process: java.lang.Process = java.lang.Runtime.getRuntime().exec("su -c echo $freq > /sys/devices/system/cpu/cpu4/cpufreq/scaling_max_freq")
        process.waitFor()
    }
}

When I try to build the apk I am getting a couple of errors in MainActivity.kt.

The errors that I am getting are as follows along the lines of codes:

val arguments = call.arguments() ?: {"LITTLECPUMaxFreq": "1766400", "bigCPUMaxFreq": "2649600"} as Map<String, String> //ERROR: Unexpected tokens (use ';' to separate expressions on the same line)

Please keep in mind that I have not done Kotlin development before and I am coding based on the online materials I have found. Can you let me know how I can resolve the issues?

Somdip Dey
  • 3,346
  • 6
  • 28
  • 60

0 Answers0