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
0611ffc4
Commit
0611ffc4
authored
Dec 19, 2023
by
18600395998
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加蓝牙读写功能
parent
a7e09934
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
0 deletions
+80
-0
ble_service.dart
lib/controllers/ble_service.dart
+80
-0
No files found.
lib/controllers/ble_service.dart
View file @
0611ffc4
...
@@ -12,6 +12,14 @@ import '../models/dict_entity.dart';
...
@@ -12,6 +12,14 @@ import '../models/dict_entity.dart';
import
'../models/user_model.dart'
;
import
'../models/user_model.dart'
;
class
BleService
extends
GetxService
{
class
BleService
extends
GetxService
{
// 添加一个 StreamController
final
_readController
=
StreamController
<
List
<
int
>>();
Stream
<
List
<
int
>>
get
readStream
=>
_readController
.
stream
;
// 添加一个标志,用于控制读取循环是否应该继续
bool
_shouldContinueReading
=
false
;
static
BleService
get
to
=>
Get
.
find
();
static
BleService
get
to
=>
Get
.
find
();
var
isScanning
=
false
.
obs
;
var
isScanning
=
false
.
obs
;
...
@@ -104,18 +112,87 @@ class BleService extends GetxService {
...
@@ -104,18 +112,87 @@ class BleService extends GetxService {
connection
.
listen
((
update
)
async
{
connection
.
listen
((
update
)
async
{
// 如果连接状态为 connected,则显示连接成功的提示
// 如果连接状态为 connected,则显示连接成功的提示
if
(
update
.
connectionState
==
DeviceConnectionState
.
connected
)
{
if
(
update
.
connectionState
==
DeviceConnectionState
.
connected
)
{
// 发现服务和特性
final
services
=
await
ble
.
discoverServices
(
device
.
id
);
for
(
final
service
in
services
)
{
print
(
'Discovered service
${service.serviceId}
'
);
for
(
final
characteristic
in
service
.
characteristics
)
{
print
(
'Discovered characteristic
${characteristic.characteristicId}
of service
${service.serviceId}
'
);
}
}
// 设置最大传输单元
await
ble
.
requestMtu
(
deviceId:
device
.
id
,
mtu:
512
);
await
Future
.
delayed
(
Duration
(
seconds:
1
));
// 添加延迟
await
Future
.
delayed
(
Duration
(
seconds:
1
));
// 添加延迟
deviceStatus
.
value
=
'Connected'
;
// 更新设备的连接状态
deviceStatus
.
value
=
'Connected'
;
// 更新设备的连接状态
Get
.
snackbar
(
'Connected'
,
'Connected to
${device.name}
'
);
Get
.
snackbar
(
'Connected'
,
'Connected to
${device.name}
'
);
// Navigator.of(context).pop();
// Navigator.of(context).pop();
// 关闭对话框
// 关闭对话框
DialogUtils
.
dismissDialog
();
DialogUtils
.
dismissDialog
();
}
else
{
}
else
{
deviceStatus
.
value
=
'Disconnected'
;
// 更新设备的连接状态
deviceStatus
.
value
=
'Disconnected'
;
// 更新设备的连接状态
}
}
});
});
}
// 读取数据
Future
<
List
<
int
>>
readData
(
Uuid
serviceUuid
,
Uuid
characteristicUuid
)
async
{
if
(
selectedDevice
.
value
==
null
)
{
throw
Exception
(
'No device connected'
);
}
final
characteristic
=
QualifiedCharacteristic
(
deviceId:
selectedDevice
.
value
!.
id
,
serviceId:
serviceUuid
,
characteristicId:
characteristicUuid
,
);
final
result
=
await
ble
.
readCharacteristic
(
characteristic
);
return
result
;
}
// 写入数据
Future
<
void
>
writeData
(
Uuid
serviceUuid
,
Uuid
characteristicUuid
,
List
<
int
>
data
)
async
{
if
(
selectedDevice
.
value
==
null
)
{
throw
Exception
(
'No device connected'
);
}
final
characteristic
=
QualifiedCharacteristic
(
deviceId:
selectedDevice
.
value
!.
id
,
serviceId:
serviceUuid
,
characteristicId:
characteristicUuid
,
);
await
ble
.
writeCharacteristicWithResponse
(
characteristic
,
value:
data
);
}
// 启动循环读取数据
Future
<
void
>
startReading
(
Uuid
serviceUuid
,
Uuid
characteristicUuid
)
async
{
_shouldContinueReading
=
true
;
while
(
_shouldContinueReading
)
{
// 读取数据
final
data
=
await
readData
(
serviceUuid
,
characteristicUuid
);
// 将数据添加到 Stream 中
_readController
.
add
(
data
);
// 等待一段时间,然后再次读取数据
await
Future
.
delayed
(
Duration
(
seconds:
1
));
}
}
}
// 停止循环读取数据
void
stopReading
()
{
_shouldContinueReading
=
false
;
}
@override
void
onClose
()
{
_readController
.
close
();
super
.
onClose
();
}
// 检查权限
// 检查权限
Future
<
bool
>
requestBlePermissions
()
async
{
Future
<
bool
>
requestBlePermissions
()
async
{
var
isLocationGranted
=
await
Permission
.
locationWhenInUse
.
request
();
var
isLocationGranted
=
await
Permission
.
locationWhenInUse
.
request
();
...
@@ -135,4 +212,7 @@ class BleService extends GetxService {
...
@@ -135,4 +212,7 @@ class BleService extends GetxService {
isBleConnectGranted
==
PermissionStatus
.
granted
&&
isBleConnectGranted
==
PermissionStatus
.
granted
&&
isBleAdvertiseGranted
==
PermissionStatus
.
granted
;
isBleAdvertiseGranted
==
PermissionStatus
.
granted
;
}
}
}
}
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