Flutter蓝牙信标检测插件beacondart的使用
Flutter蓝牙信标检测插件beacondart的使用
简介
beacondart
是一个实现 Tezos 蓝牙信标协议(TZIP-10)的 Dart 库。该库通过 Flutter 的方法通道(method channels)将 Android 和 iOS 平台上的原生 SDK(如 beacon-android-sdk
和 beacon-ios-sdk
)与 Dart 代码进行连接。
使用范围
该库专注于实现 Tezos 蓝牙信标的 钱包端 功能,允许用户通过蓝牙与 DApp(去中心化应用)进行交互。具体功能包括:
- 连接到 DApp
- QR Code 扫描
- 复制粘贴
- 深度链接
- 监听来自 DApp 的消息
- 权限请求/响应
- 签名请求/响应
- 操作请求/响应
- 广播请求/响应
注意:该库本身不包含生成 QR Code 或深度链接的功能,需要开发者自行集成外部库来完成这些任务。
功能列表
连接到 DApp
// 可以通过 QR Code、复制粘贴或深度链接等方式连接到 DApp
监听消息
// 监听并处理来自 DApp 的各种请求
实现细节
beacondart
提供了一个 Dart 单例类 BeaconWalletClient
,用于通过 Flutter 方法通道与 Android 和 iOS 原生 SDK 进行通信。
完整示例代码
以下是一个完整的示例代码,展示如何使用 beacondart
插件进行蓝牙信标检测。
import 'dart:convert';
import 'package:base_codecs/base_codecs.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:beacondart/beacondart.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> {
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 初始化 Beacondart 客户端
BeaconWalletClient bmw = BeaconWalletClient();
Future<void> initPlatformState() async {
// 初始化 Beacondart,传入应用名称、公钥和地址
bool inited = await bmw.init(
'Ejara',
'9ae0875d510904b0b15d251d8def1f5f3353e9799841c0ed6d7ac718f04459a0',
'tz1SkbBZg15BXPRkYCrSzhY6rq4tKGtpUSWv',
);
print("======================== Inited ===================================== $inited");
}
// 将 Base58 编码字符串解码为普通字符串
String b58ToString(String b58String) {
var b = base58CheckDecode(b58String);
return utf8.decode(b);
}
// 将 Base64 编码字符串解码为 JSON 对象
Map b64Decode(String b64) {
return jsonDecode(utf8.decode(base64.decode(b64)));
}
final TextEditingController _controller = TextEditingController();
bool isEmpty = false;
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('BeaconDart 示例'),
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// 输入框用于输入 Base58 编码的字符串
TextFormField(
controller: _controller,
maxLines: 7,
decoration: InputDecoration(
border: const OutlineInputBorder(),
hintText: '请输入 Base58 编码的字符串',
errorText: isEmpty ? '请输入内容' : null),
onChanged: (String value) {
if (value.isEmpty) {
setState(() {
isEmpty = true;
});
} else {
setState(() {
isEmpty = false;
});
}
},
),
ElevatedButton(
onPressed: () async {
if (_controller.text.isNotEmpty) {
debugPrint(_controller.text);
// 解码输入的 Base58 字符串
String conn = b58ToString(_controller.text);
Map<String, String> dApp = Map<String, String>.from(jsonDecode(conn));
// 监听来自 DApp 的请求
await bmw.onBeaconRequest((response) async {
debugPrint('onBeaconRequest = $response');
// 根据请求类型发送响应
switch (response['type']) {
case "TezosPermission":
bmw.sendResponse({
"id": response["id"],
"status": true,
}, (args) {});
break;
case "TezosOperation":
bmw.sendResponse({
"id": response["id"],
"status": true,
"transactionHash": "wow",
}, (args) {});
break;
case "TezosSignPayload":
bmw.sendResponse({
"id": response["id"],
"status": true,
"signature": "wow",
}, (args) {});
break;
case "TezosBroadcast":
bmw.sendResponse({
"id": response["id"],
"status": true,
"transactionHash": "wow",
}, (args) {});
break;
default:
}
});
// 添加对等节点
await bmw.addPeer(dApp);
// 获取对等节点列表
await bmw.getPeers((response) {
debugPrint('getPeers = $response');
});
// 清空输入框
setState(() {
isEmpty = false;
_controller.text = '';
});
} else {
setState(() {
isEmpty = true;
});
}
},
child: const Text('连接 DApp'),
)
],
),
),
),
);
}
}
更多关于Flutter蓝牙信标检测插件beacondart的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复