title: Flutter Kotlin Get Data from Broadcast receiver
body: <p>I'm trying to have my own bluetooth kotlin implementation, but am running into a problem. </p>
  
  <p>I am starting a broadcast receiver for scanning bluetooth devices by following the kotlin tutorial <a href="https://developer.android.com/guide/topics/connectivity/bluetooth#kotlin" rel="nofollow noreferrer">here</a>. </p>
  
  <p>The problem is that when i try to add each discovered device to a main activity wide mutable list from the broadcast receiver so that i can send the list to the flutter side, i always get a null. </p>
  
  <p>Since im new to kotlin and android i cant really understand where exactly im going wrong, and which concepts do i need to understand to do what i need to do. </p>
  
  <p><strong>Kotlin MainActivity.kt</strong></p>
  
  <pre><code>
  class MainActivity : FlutterActivity() {
  
      private val CHANNEL = "bluetooth.channel"
       var deviceList: MutableList&lt;BluetoothDevice&gt;?=null;
  
      override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
  
          GeneratedPluginRegistrant.registerWith(flutterEngine);
          MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result -&gt;
  
              when (call.method) {
                  "getBlue" -&gt; bluetoothWrapper(result)
                  "discoverBlue" -&gt; discoverDevices(deviceList,result)
              }
          }
      }
  
      private fun bluetoothWrapper(result: MethodChannel.Result) {
          val defaultAdapter: BluetoothAdapter? = BluetoothAdapter.getDefaultAdapter()
          checkAdapter(result, defaultAdapter);
          enableAdapter(defaultAdapter!!);
      }
  
  
      private fun checkAdapter(result: MethodChannel.Result, defaultAdapter: BluetoothAdapter?) {   // check if adapter exists
          if (defaultAdapter == null) {
              result.error("Bluetooth adapter doesn't exist on this device", null, null)
          } else {
              result.success("bluetooth adapter exists on device")
          }
  
  
      }
  
      // check if adapter is enabled if it exists, enable it if it isnt
  
      @SuppressLint("MissingPermission")
      private fun enableAdapter(bluetoothAdapter: BluetoothAdapter) {
          val requestEnableBt = 1;
          if (!bluetoothAdapter.isEnabled) {
              val enableIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
              startActivityForResult(enableIntent, requestEnableBt)
          }
      }
  
      // register broadcast receiver in order to discover available devices
  
      private fun discoverDevices(deviceList: MutableList&lt;BluetoothDevice&gt;?, result: MethodChannel.Result) {
          val filter = IntentFilter(BluetoothDevice.ACTION_FOUND)
  
          registerReceiver(receiver, filter);
  
          result.success(deviceList)
      }
      private val receiver = object : BroadcastReceiver() {
          override fun onReceive(context: Context, intent: Intent) {
              when (intent.action) {
                  BluetoothDevice.ACTION_FOUND -&gt; {
                      val device: BluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                      deviceList?.add(device)
  
                      println("device found has selected parameters inside the broadcast receivver function $device")
                  }
                  "" -&gt; println("broadcast receiver intent.action has no attribute")
                  null -&gt; println("broadcast receiver intent.action was null")
              }
          }
      }
  }
  
  
  </code></pre>
  
  <p><strong>Flutter Main.Dart</strong></p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter/services.dart';
  
  void main() =&gt; runApp(MaterialApp(home: HomePage()));
  
  class HomePage extends StatelessWidget {
    static const platform = const MethodChannel('bluetooth.channel');
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text('Bluetooth Native'),
          centerTitle: true,
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: &lt;Widget&gt;[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: &lt;Widget&gt;[
                RaisedButton(
                  color: Colors.blue[500],
                  onPressed: () {
                    printMethod();
                  },
                  child: Text('connect to Devices'),
                )
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: &lt;Widget&gt;[
                RaisedButton(
                  color: Colors.blue[500],
                  onPressed: () {
                    discoverBlue();
                  },
                  child: Text('Discover devices'),
                )
              ],
            )
          ],
        ),
      );
    }
  
    void printMethod() async {
      String value;
      try {
        value = await platform.invokeMethod("getBlue");
      } catch (e) {
        print(e);
      }
      print('printing from dart: $value');
    }
  
    void discoverBlue() async {
      Future&lt;List&gt; list;
      list = platform.invokeListMethod("discoverBlue", list);
      list.then((val) {
        print("got values from kotlin $val");
      });
    }
  }
  
  </code></pre>