Flutter原生功能集成插件native_runchen的使用
Flutter原生功能集成插件native_runchen的使用
native_runchen
`Flutter与Android原生层交互的插件,内容还在持续扩展中. 现有方法包括:
- 音量控制(音量加、音量减、设置音量、获取当前音量、静音、取消静音)
- Toast显示方法
- 串口通讯(串口开启、串口发送、串口读取、串口关闭)
- I2C通讯接口(I2C写入、I2C读取)
- RFID模块读取(USB转串口)
USB2SerialPortManage
如果应用需要在设备连接时收到通知,可以在项目的res/xml/目录下添加device_filter.xml
文件,并在AndroidManifest.xml
中进行配置。
<activity
android:name="..."
...>
</activity>
Getting Started
本项目是一个用于Flutter的插件包,它包含了针对Android和/或iOS平台的特定实现代码。帮助你开始使用Flutter,请参阅我们的在线文档,该文档提供了教程、示例、移动开发指南以及完整的API引用。
示例代码
以下是一个简单的示例代码,展示了如何使用native_runchen
插件来实现音量控制、串口通讯和I2C通讯等功能。
示例代码:main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:native_runchen/native_runchen.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _deviceName = '/dev/ttyS4';
String _baudRate = '57600';
String _sendData = 'EF01FFFFFFFF01000831000103004A0088';
String _message = '';
String _i2cDeviceName = '';
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
_onReceiveEventData();
}
// 平台消息是异步的,因此我们在异步方法中初始化。
Future<void> initPlatformState() async {
String platformVersion;
// 平台消息可能失败,因此我们使用try/catch处理PlatformException。
// 我们还处理了消息可能返回null的情况。
try {
platformVersion =
await NativeRunchen.platformVersion ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 如果在异步平台消息仍在飞行时,小部件从树中被移除,我们希望丢弃回复而不是调用
// setState来更新我们不存在的外观。
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
void _onReceiveEventData() {
NativeRunchen.onListenStreamData((data) {
setState(() {
_message = data;
});
}, (error) {
print("event channel error : $error");
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
Text('Running on: $_platformVersion\n'),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('音量加'),
onPressed: () {
NativeRunchen.volumeUp;
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('音量减'),
onPressed: () {
NativeRunchen.volumeDown;
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('静音'),
onPressed: () {
NativeRunchen.muteOn;
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('取消静音'),
onPressed: () {
NativeRunchen.muteOff;
})
]),
TextField(
style: const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
controller: TextEditingController.fromValue(TextEditingValue(
text: _deviceName,
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: _deviceName.length)))),
decoration: InputDecoration(
counterText: '',
filled: true,
fillColor: Color(0xFF1A1A1A),
hintStyle:
const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
hintText: '请输入串口设备名称',
contentPadding:
EdgeInsets.symmetric(horizontal: 15, vertical: 10),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
),
onChanged: (v) {
_deviceName = v;
setState(() {});
}),
TextField(
style: const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
controller: TextEditingController.fromValue(TextEditingValue(
text: _baudRate,
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: _baudRate.length)))),
decoration: InputDecoration(
counterText: '',
filled: true,
fillColor: Color(0xFF1A1A1A),
hintStyle:
const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
hintText: '请输入波特率',
contentPadding:
EdgeInsets.symmetric(horizontal: 15, vertical: 10),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
),
onChanged: (v) {
_baudRate = v;
setState(() {});
}),
TextField(
style: const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
controller: TextEditingController.fromValue(TextEditingValue(
text: _sendData,
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: _sendData.length)))),
decoration: InputDecoration(
counterText: '',
filled: true,
fillColor: Color(0xFF1A1A1A),
hintStyle:
const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
hintText: '请输入发送内容',
contentPadding:
EdgeInsets.symmetric(horizontal: 15, vertical: 10),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
),
onChanged: (v) {
_sendData = v;
setState(() {});
}),
TextField(
style: const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
controller: TextEditingController.fromValue(TextEditingValue(
text: _message,
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: _message.length)))),
decoration: InputDecoration(
counterText: '',
filled: true,
fillColor: Color(0xFF1A1A1A),
hintStyle:
const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
hintText: '读取内容',
contentPadding:
EdgeInsets.symmetric(horizontal: 15, vertical: 10),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
),
onChanged: (v) {
_message = v;
setState(() {});
}),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('打开串口'),
onPressed: () {
NativeRunchen.openSerialPort(
_deviceName, _baudRate, 8, 'n', 1);
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('关闭串口'),
onPressed: () {
NativeRunchen.closeSerialPort(_deviceName);
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('发送数据'),
onPressed: () {
NativeRunchen.serialPortSend(_deviceName, _sendData);
})
],
),
TextField(
style: const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
controller: TextEditingController.fromValue(TextEditingValue(
text: _i2cDeviceName,
selection: TextSelection.fromPosition(TextPosition(
affinity: TextAffinity.downstream,
offset: _i2cDeviceName.length)))),
decoration: InputDecoration(
counterText: '',
filled: true,
fillColor: Color(0xFF1A1A1A),
hintStyle:
const TextStyle(color: Color(0xFFA7ABBB), fontSize: 15),
hintText: '请输入I2C设备名称',
contentPadding:
EdgeInsets.symmetric(horizontal: 15, vertical: 10),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(6),
borderSide: BorderSide.none),
),
onChanged: (v) {
_i2cDeviceName = v;
setState(() {});
}),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('设备写入'),
onPressed: () {
NativeRunchen.i2cWrite(_i2cDeviceName, "0xFF");
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('设备读取'),
onPressed: () {
NativeRunchen.i2cRead(_i2cDeviceName, 1);
})
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('USB串口配置'),
onPressed: () {
NativeRunchen.usb2SerialPortSetConfig(
"57600", "8", "1", "0");
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('USB串口打开'),
onPressed: () {
NativeRunchen.usb2SerialPortOpenDevice;
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('USB串口关闭'),
onPressed: () {
NativeRunchen.usb2SerialPortCloseDevice;
}),
MaterialButton(
color: Colors.blue,
textColor: Colors.white,
child: new Text('USB串口发送数据'),
onPressed: () {
NativeRunchen.usb2SerialPortWriteData(_sendData);
})
],
)
],
),
),
);
}
}
更多关于Flutter原生功能集成插件native_runchen的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter原生功能集成插件native_runchen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,关于如何在Flutter项目中集成并使用原生功能插件native_runchen
(请注意,native_runchen
是一个假设的插件名称,实际中可能并不存在这样一个具体命名的插件,但以下示例将展示如何集成和使用一个假设的原生插件的基本流程),以下是一个示例代码案例。
1. 创建Flutter插件
首先,假设我们已经有一个Flutter插件native_runchen
,该插件提供了某些原生功能。在真实场景中,你可能需要从pub.dev下载或自己创建这个插件。
2. 在Flutter项目中添加依赖
在你的pubspec.yaml
文件中添加对该插件的依赖:
dependencies:
flutter:
sdk: flutter
native_runchen: ^1.0.0 # 假设的版本号
然后运行flutter pub get
来安装依赖。
3. 配置原生代码(iOS和Android)
iOS
在ios/Runner/Info.plist
中可能需要添加一些权限配置,具体取决于插件所需的功能。例如,如果插件需要访问相机,你可能需要添加:
<key>NSCameraUsageDescription</key>
<string>App needs access to the camera</string>
然后,在ios/
目录下的Podfile
中,确保平台版本符合插件要求。
Android
在android/app/src/main/AndroidManifest.xml
中添加必要的权限。例如:
<uses-permission android:name="android.permission.CAMERA" />
同样,确保android/build.gradle
和android/app/build.gradle
文件中的编译SDK版本和targetSDK版本与插件要求一致。
4. 使用插件功能
在你的Dart代码中,你可以这样使用native_runchen
插件:
import 'package:flutter/material.dart';
import 'package:native_runchen/native_runchen.dart'; // 导入插件
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Native Runchen Example'),
),
body: Center(
child: ElevatedButton(
onPressed: _useNativeFunction,
child: Text('Call Native Function'),
),
),
),
);
}
void _useNativeFunction() async {
try {
// 调用插件提供的原生方法,假设这个方法叫做`someNativeMethod`
var result = await NativeRunchen.someNativeMethod();
print('Native method result: $result');
} catch (e) {
print('Error calling native method: $e');
}
}
}
5. 实现原生方法(示例)
iOS(Swift)
在ios/Runner/NativeRunchenPlugin.swift
中:
import Flutter
public class NativeRunchenPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterRegistrar) {
let channel = FlutterMethodChannel(name: "native_runchen", binaryMessenger: registrar.messenger())
let instance = NativeRunchenPlugin()
channel.setMethodCallHandler(instance.handle(_:result:))
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "someNativeMethod" {
// 执行原生逻辑,例如返回一个字符串
let resultString = "Hello from native iOS!"
result(resultString)
} else {
result(FlutterMethodNotImplemented)
}
}
}
Android(Kotlin)
在android/src/main/kotlin/com/example/native_runchen/NativeRunchenPlugin.kt
中:
package com.example.native_runchen
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import android.app.Activity
class NativeRunchenPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
private lateinit var channel: MethodChannel
private var activity: Activity? = null
override fun onAttachedToEngine(binding: FlutterPluginBinding) {
channel = MethodChannel(binding.binaryMessenger, "native_runchen")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(call: MethodCall, result: Result) {
if (call.method == "someNativeMethod") {
// 执行原生逻辑,例如返回一个字符串
result.success("Hello from native Android!")
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onDetachedFromActivityForConfigChanges() {
activity = null
}
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}
override fun onDetachedFromActivity() {
activity = null
}
}
请注意,上述代码示例是基于假设的插件native_runchen
,并且展示了如何在Flutter项目中集成和使用原生插件的基本流程。实际插件的集成可能会有所不同,具体取决于插件的功能和实现细节。