在编写本机插件时,如何将Broadcastreceiver变成流汉?
我以前从未写过 android 代码。现在我想写一个收集Wi-Fi数据的Flutter插件。
按照此指南并在 YouTube 上找到了一个视频,但作者使用了 StreamHandler,但我找不到实现StreamHandler相关代码的方法。
到目前为止,在 kotlin 端编写了这些代码,据我了解,我必须使用 wifiScanReceiver 作为 StreamHandler,但它是一个 BroadcastReceiver。因此 dataChannel!!.setStreamHandler(wifiScanReceiver)
给出错误。
class MainActivity(wifiManager: WifiManager) : FlutterActivity() {
private val METHOD_CHANNEL_NAME = "com.baran.collect_wifi/method"
private val DATA_CHANNEL_NAME = "com.baran.collect_wifi/data"
private var methodChannel : MethodChannel? = null
private lateinit var wifiManager: WifiManager
private var dataChannel : EventChannel? = null
private var eventSink: EventChannel.EventSink? = null
@RequiresApi(Build.VERSION_CODES.M)
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
//Setup channels
setupChannels(this, flutterEngine.dartExecutor.binaryMessenger)
}
override fun onDestroy() {
teardownChannels()
super.onDestroy()
}
private val wifiScanReceiver = object : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.M)
override fun onReceive(context: Context, intent: Intent) {
val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
if (success) {
scanSuccess()
} else {
scanFailure()
}
}
}
private fun scanSuccess() {
}
private fun scanFailure() {
// handle failure: new scan did NOT succeed
// consider using old scan results: these are the OLD results!
val results = wifiManager.scanResults
}
@RequiresApi(Build.VERSION_CODES.M)
private fun setupChannels(context: Context, messenger: BinaryMessenger){
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val intentFilter = IntentFilter()
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
context.registerReceiver(wifiScanReceiver, intentFilter)
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
methodChannel!!.setMethodCallHandler{
call, _ ->
if(call.method == "startScan"){
val success = wifiManager.startScan()
if (!success) {
// scan failure handling
scanFailure()
}
}
}
dataChannel = EventChannel(messenger, DATA_CHANNEL_NAME)
dataChannel!!.setStreamHandler(wifiScanReceiver)
}
private fun teardownChannels() {
methodChannel!!.setMethodCallHandler(null)
}
}
这是到目前为止的颤振代码,
import 'dart:async';
import 'package:flutter/services.dart';
class CollectWifi {
static const _channel = MethodChannel('com.baran.collect_wifi/method');
static const _dataChannel = EventChannel('com.baran.collect_wifi/data');
}
我不知道我是否正确
I've never written android code before. Now I want to write a Flutter plugin that collects Wi-Fi data.
Following this guide and found a video on YouTube but author uses StreamHandler, but I couldn't find a way to implement StreamHandler related code.
Wrote these code so far in the kotlin side, as my understanding I have to use wifiScanReceiver
as StreamHandler but it is a BroadcastReceiver. Therefore the dataChannel!!.setStreamHandler(wifiScanReceiver)
gives error.
class MainActivity(wifiManager: WifiManager) : FlutterActivity() {
private val METHOD_CHANNEL_NAME = "com.baran.collect_wifi/method"
private val DATA_CHANNEL_NAME = "com.baran.collect_wifi/data"
private var methodChannel : MethodChannel? = null
private lateinit var wifiManager: WifiManager
private var dataChannel : EventChannel? = null
private var eventSink: EventChannel.EventSink? = null
@RequiresApi(Build.VERSION_CODES.M)
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
//Setup channels
setupChannels(this, flutterEngine.dartExecutor.binaryMessenger)
}
override fun onDestroy() {
teardownChannels()
super.onDestroy()
}
private val wifiScanReceiver = object : BroadcastReceiver() {
@RequiresApi(Build.VERSION_CODES.M)
override fun onReceive(context: Context, intent: Intent) {
val success = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false)
if (success) {
scanSuccess()
} else {
scanFailure()
}
}
}
private fun scanSuccess() {
}
private fun scanFailure() {
// handle failure: new scan did NOT succeed
// consider using old scan results: these are the OLD results!
val results = wifiManager.scanResults
}
@RequiresApi(Build.VERSION_CODES.M)
private fun setupChannels(context: Context, messenger: BinaryMessenger){
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val intentFilter = IntentFilter()
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
context.registerReceiver(wifiScanReceiver, intentFilter)
methodChannel = MethodChannel(messenger, METHOD_CHANNEL_NAME)
methodChannel!!.setMethodCallHandler{
call, _ ->
if(call.method == "startScan"){
val success = wifiManager.startScan()
if (!success) {
// scan failure handling
scanFailure()
}
}
}
dataChannel = EventChannel(messenger, DATA_CHANNEL_NAME)
dataChannel!!.setStreamHandler(wifiScanReceiver)
}
private fun teardownChannels() {
methodChannel!!.setMethodCallHandler(null)
}
}
And this is the flutter code so far
import 'dart:async';
import 'package:flutter/services.dart';
class CollectWifi {
static const _channel = MethodChannel('com.baran.collect_wifi/method');
static const _dataChannel = EventChannel('com.baran.collect_wifi/data');
}
I don't know if I'm going correctly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我更新了源代码中的一些点,如下所示
I updated some point in your source code as below