Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
A
anchor_collect_flutter
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hywang
anchor_collect_flutter
Commits
d5948155
Commit
d5948155
authored
Dec 27, 2023
by
hywang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.
parent
eab9d7fb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
8 deletions
+84
-8
rfid_plugin.dart
lib/rfid/rfid_plugin.dart
+84
-8
No files found.
lib/rfid/rfid_plugin.dart
View file @
d5948155
import
'dart:async'
;
import
'dart:isolate'
;
import
'dart:ui'
;
import
'package:anchor_collect_flutter/utils/empty_utils.dart'
;
import
'package:flutter/foundation.dart'
;
...
...
@@ -10,11 +12,9 @@ import '../controllers/ble_service.dart';
typedef
void
EventCallback
(
args
,
args2
);
class
RfidPlugin
{
static
const
MethodChannel
_methodChannel
=
MethodChannel
(
'flutter_rfid_plugin'
);
static
const
MethodChannel
_methodChannel
=
MethodChannel
(
'flutter_rfid_plugin'
);
static
const
EventChannel
_eventChannel
=
EventChannel
(
'flutter_rfid_plugin/event'
);
static
const
EventChannel
_eventChannel
=
EventChannel
(
'flutter_rfid_plugin/event'
);
static
EventCallback
?
_callback
;
...
...
@@ -22,16 +22,28 @@ class RfidPlugin {
static
String
rfidTagTemp
=
''
;
late
Isolate
_isolat
;
late
ReceivePort
rootIsolateReceivePort
;
///子线程
late
SendPort
newIsolateSendPort
;
///主线程
late
SendPort
rootIsolateSendPort
;
RfidPlugin
()
{
_streamSubscription
=
_eventChannel
.
receiveBroadcastStream
().
listen
((
event
)
async
{
establishConn
();
_streamSubscription
=
_eventChannel
.
receiveBroadcastStream
().
listen
((
event
)
async
{
final
Map
<
dynamic
,
dynamic
>
map
=
event
;
switch
(
map
[
'key'
])
{
case
'BluetoothData'
:
if
(
map
[
'value'
]
!=
null
)
{
List
<
int
>
bluetoothData
=
map
[
'value'
];
print
(
'Received Byte List:
$bluetoothData
'
);
await
BleService
.
to
.
writeData
(
bluetoothData
);
if
(
kDebugMode
)
{
print
(
'Received Byte List:
$bluetoothData
'
);
}
rootIsolateSendPort
.
send
(
bluetoothData
);
// await BleService.to.writeData(bluetoothData);
}
break
;
case
'InventoryData'
:
...
...
@@ -41,6 +53,70 @@ class RfidPlugin {
},
onError:
errorEventListen
,
onDone:
doneEventListen
);
}
//特别需要注意:establishConn执行环境是rootIsolate
void
establishConn
()
async
{
//第1步: 默认执行环境下是rootIsolate,所以创建的是一个rootIsolateReceivePort
rootIsolateReceivePort
=
ReceivePort
();
//第2步: 获取rootIsolateSendPort
rootIsolateSendPort
=
rootIsolateReceivePort
.
sendPort
;
// 注册rootIsolateSendPort
IsolateNameServer
.
registerPortWithName
(
rootIsolateSendPort
,
'rootIsolateSendPort'
);
//第3步: 创建一个newIsolate实例,并把rootIsolateSendPort作为参数传入到newIsolate中,为的是让newIsolate中持有rootIsolateSendPort, 这样在newIsolate中就能向rootIsolate发送消息了
//注意createNewIsolateContext这个函数执行环境就会变为newIsolate, rootIsolateSendPort就是createNewIsolateContext回调函数的参数
_isolat
=
await
Isolate
.
spawn
(
createNewIsolateContext
,
rootIsolateSendPort
);
rootIsolateReceivePort
.
listen
((
message
)
{
if
(
kDebugMode
)
{
print
(
"主线程收到的消息
$message
"
);
}
if
(
message
is
SendPort
)
{
newIsolateSendPort
=
message
;
}
else
if
(
message
is
List
<
int
>)
{
newIsolateSendPort
.
send
(
message
);
}
});
//第7步: 通过rootIsolateReceivePort接收到来自newIsolate的消息,所以可以注意到这里是await 因为是异步消息
//只不过这个接收到的消息是newIsolateSendPort, 最后赋值给全局newIsolateSendPort,这样rootIsolate就持有newIsolate的SendPort
// var messageList = await rootIsolateReceivePort.first;
// //第8步,建立连接成功
// print(messageList[0] as String);
// newIsolateSendPort = messageList[1] as SendPort;
}
// 这个SendPort是前面主线程的
createNewIsolateContext
(
SendPort
sendPort
)
async
{
print
(
"子线程开始"
);
/// 子线程的ReceivePort创建
ReceivePort
subThreadPort
=
ReceivePort
();
/// 这是把子线程的ReceivePort的sendPort给了主线程 用于通信
sendPort
.
send
(
subThreadPort
.
sendPort
);
subThreadPort
.
listen
((
message
)
async
{
print
(
"子线程收到的消息
$message
"
);
if
(
message
is
String
)
{
/// 收到主线程让关闭接口的消息 就关闭 正确的话后面是在接受不到消息了
if
(
message
==
"close receiveport"
)
{
sendPort
.
send
(
'closed'
);
subThreadPort
.
close
();
}
}
else
if
(
message
is
List
<
int
>){
print
(
'message is List<int>'
);
await
BleService
.
to
.
writeData
(
message
);
}
});
///
//第4步: 注意callback这个函数执行环境就会变为newIsolate, 所以创建的是一个newIsolateReceivePort
// ReceivePort newIsolateReceivePort = ReceivePort();
// //第5步: 获取newIsolateSendPort, 有人可能疑问这里为啥不是直接让全局newIsolateSendPort赋值,注意这里执行环境不是rootIsolate
// SendPort newIsolateSendPort = newIsolateReceivePort.sendPort;
// //第6步: 特别需要注意这里,这里是利用rootIsolateSendPort向rootIsolate发送消息,只不过发送消息是newIsolate的SendPort, 这样rootIsolate就能拿到newIsolate的SendPort
// rootIsolateSendPort.send(['connect success from new isolate', newIsolateSendPort]);
}
static
Future
<
void
>
initRfid
()
async
{
await
_methodChannel
.
invokeMethod
(
'init_rfid'
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment