需要Flutter开发HarmonyOS原生plugin和so动态库打包的官方教程文档

xx应用使用flutter开发,兼容HarmonyOS需要重新开发自己的蓝牙插件,需要提供Flutter开发HarmonyOS原生plugin和so动态库打包的官方教程文档,插件主要功能是执行蓝牙发包与调用so库方法加密指令。

HarmonyOS
2024-08-23 10:28:12
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

1.dart代码中执行 MethodChannel#invokeMethod(String) 方法,传递要执行的方法名,并获取对应的返回值。

2.ets代码中使用 MethodChannel#setMethodCallHandler(MethodCallHandler) 方法注册对应的实现。

替换 lib/main.dart 中的代码

import 'dart:async'; 
import 'package:flutter/material.dart'; 
import 'package:flutter/services.dart'; 
 
void main() { 
  runApp(const MyApp()); 
} 
 
class MyApp extends StatelessWidget { 
  const MyApp({super.key}); 
 
  @override 
  Widget build(BuildContext context) { 
  return MaterialApp( 
  title: 'Flutter Demo', 
  theme: ThemeData( 
  primarySwatch: Colors.blue, 
  ), 
  home: const MyHomePage(title: 'Flutter Demo Home Page'), 
  ); 
} 
} 
 
class MyHomePage extends StatefulWidget { 
  const MyHomePage({super.key, required this.title}); 
 
  final String title; 
 
  @override 
  State<MyHomePage> createState() => _MyHomePageState(); 
} 
 
class _MyHomePageState extends State<MyHomePage> { 
  // 注册通道名称。 
  // 一个应用中所使用的所有通道名称必须是唯一的;使用唯一的 域前缀 为通道名称添加前缀 
  static const platform = MethodChannel('samples.flutter.dev/battery'); 
  String _batteryLevel = 'Unknown battery level.'; 
 
  Future<void> _getBatteryLevel() async { 
  String batteryLevel; 
  try { 
  // 调用需要在平台中实现的方法 
  final result = await platform.invokeMethod<int>('getBatteryLevel'); 
  batteryLevel = 'Battery level at $result % .'; 
} on PlatformException catch (e) { 
  batteryLevel = "Failed to get battery level: '${e.message}'."; 
} 
 
setState(() { 
  _batteryLevel = batteryLevel; 
}); 
} 
 
// 以字符串形式显示电池状态、并包含一个用于刷新该值的按钮的小型用户界面。 
@override 
Widget build(BuildContext context) { 
  return Material( 
    child: Center( 
    child: Column( 
    mainAxisAlignment: MainAxisAlignment.spaceEvenly, 
  children: [ 
  ElevatedButton( 
    onPressed: _getBatteryLevel, 
  child: const Text('Get Battery Level'), 
  ), 
  Text(_batteryLevel), 
  ], 
  ), 
  ), 
  ); 
} 
}

修改 EntryAbility.ets,注册插件 BatteryPlugin

import { FlutterAbility } from '@ohos/flutter_ohos' 
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant'; 
import FlutterEngine from '@ohos/flutter_ohos/src/main/ets/embedding/engine/FlutterEngine'; 
import BatteryPlugin from './BatteryPlugin'; 
 
export default class EntryAbility extends FlutterAbility { 
  configureFlutterEngine(flutterEngine: FlutterEngine) { 
    super.configureFlutterEngine(flutterEngine) 
    GeneratedPluginRegistrant.registerWith(flutterEngine) 
    // 注册插件 
    this.addPlugin(new BatteryPlugin()) 
  } 
}

实现 BatteryPlugin

import { FlutterPlugin, Log, Any, MethodChannel, MethodCall } from '@ohos/flutter_ohos'; 
import { FlutterPluginBinding } from '@ohos/flutter_ohos/src/main/ets/embedding/engine/plugins/FlutterPlugin'; 
import { batteryInfo } from '@kit.BasicServicesKit'; 
import { MethodResult } from '@ohos/flutter_ohos/src/main/ets/plugin/common/MethodChannel'; 
 
const TAG = "BatteryPluginTag"; 
const CHANNEL = "samples.flutter.dev/battery"; 
 
export default class BatteryPlugin implements FlutterPlugin { 
  onAttachedToEngine(binding: FlutterPluginBinding): void { 
    new MethodChannel(binding.getBinaryMessenger(), CHANNEL).setMethodCallHandler({ 
      onMethodCall(call: MethodCall, result: MethodResult) { 
        if (call.method == "getBatteryLevel") { 
          let level: number = getBatteryLevel(); 
          Log.i(TAG, "level=" + level); 
          let wrapped: Map<String, Any> = new Map<String, Any>(); 
          if (level >= 0) { 
            result.success(level); 
          } else { 
            Log.i(TAG, "getBatteryLevel else"); 
            wrapped.set("UNAVAILABLE", "Battery level not available."); 
            result.error("UNAVAILABLE", "Battery level not available.", null) 
          } 
          Log.i(TAG, "getBatteryLevel finish"); 
        } 
      } 
    }) 
  } 
 
  onDetachedFromEngine(binding: FlutterPluginBinding): void { 
    Log.i(TAG, "onDetachedFromEngine"); 
  } 
 
  getUniqueClassName(): string { 
    return "BatteryPlugin"; 
  } 
} 
 
function getBatteryLevel(): number { 
  return batteryInfo.batterySOC; 
}

运行和测试

flutter run -d $DEVICE --local-engine=$ENGINE_DEBUG --debug

DevEco Studio的C/C++ 项目,支持将“.so”库文件打包到HAP。

在项目文件的libs目录下,根据设备使用的cpu架构,创建相应的目录“libs/arm64-v8a”、“libs/armeabi-v7a”、“libs/x86”、或“libs/x86_64”。

C++“ .so”库文件拷贝粘贴在对应的目录下。点击“Build>Build APP(s)/Hap(s)>Build Debug Hap(s)”,将相应的“.so”库文件打包到HAP中以便调试。

分享
微博
QQ
微信
回复
2024-08-23 15:57:14
相关问题
如何查看官方xml文档
10716浏览 • 2回复 待解决
ArkTSNative如何动态加载、卸载so
1850浏览 • 1回复 待解决
DevEco Studio开发如何引入.so
10455浏览 • 1回复 待解决
HarmonyOS原生如何给flutter传值
221浏览 • 1回复 待解决
ArkTS侧与Native侧分别如何动态加载SO
2268浏览 • 1回复 待解决
Flutter 开发资料相关问题
260浏览 • 1回复 待解决
HarmonyOS 如何适配自己so
373浏览 • 1回复 待解决
Hi3516按照官方文档无法进入ohos shell
7459浏览 • 3回复 待解决
基于原生应用主题开发
390浏览 • 1回复 待解决
有关深色模式开发文档
1583浏览 • 1回复 待解决
如何在ArkTS中动态加载so
250浏览 • 1回复 待解决