gpt4 book ai didi

android - Flutter Blue 设置通知

转载 作者:IT王子 更新时间:2023-10-29 07:07:12 31 4
gpt4 key购买 nike

我现在一直在使用 flutter Blue,但我坚持以下几点:我正在使用在 https://github.com/pauldemarco/flutter_blue 上下载的示例应用程序,通过这里的基本思想是,只要我连接到我的蓝牙设备,它就会开始检查服务“FFE0”是否存在,然后检查特征“FFE1”。此特性会吐出我的项目所需的随机字符串。

Image of screen with characteristic open

通过屏幕我可以看到上面的内容是正确的我只需要在它连接到蓝牙设备时以某种方式自动设置该特性的通知。

这是我正在 _Connect 函数中测试的一些当前代码。

_connect(BluetoothDevice d) async {
device = d;
// Connect to device
deviceConnection = _flutterBlue
.connect(device, timeout: const Duration(seconds: 4))
.listen(
null,
onDone: _disconnect,
);

// Update the connection state immediately
device.state.then((s) {
setState(() {
deviceState = s;
});
});

// Subscribe to connection changes
deviceStateSubscription = device.onStateChanged().listen((s) {
setState(() {
deviceState = s;
});
if (s == BluetoothDeviceState.connected) {
device.discoverServices().then((service) {
service.forEach((_service){
var characteristics = _service.characteristics;
_service.characteristics.map((c) {
print(c);
});
for(BluetoothCharacteristic _characteristic in characteristics) {
device.readCharacteristic(_characteristic).then((_value){
print(_value);
if (_value.contains("FFE0")) {
print("Found!!");
// do something
}
});
}
});
setState(() {
services = service;
});
_getServices();
});
}
});
}

我可能有人对如何解决我的问题有建议。

罗宾

最佳答案

我找到了 https://github.com/Sensirion/smart-gadget-flutter/tree/master/lib我能够使用以下代码解决我的问题:

      for(BluetoothService service in services) {
for(BluetoothCharacteristic c in service.characteristics) {
if(c.uuid == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb")) {
_setNotification(c);
} else {
print("Nope");
}
}
}

这是在 _connect 函数中添加的。

_connect(BluetoothDevice d) async {
device = d;
// Connect to device
deviceConnection = _flutterBlue
.connect(device, timeout: const Duration(seconds: 4))
.listen(
null,
onDone: _disconnect,
);

// Update the connection state immediately
device.state.then((s) {
setState(() {
deviceState = s;
});
});

// Subscribe to connection changes
deviceStateSubscription = device.onStateChanged().listen((s) {
setState(() {
deviceState = s;
});
if (s == BluetoothDeviceState.connected) {

device.discoverServices().then((s) {

services = s;

for(BluetoothService service in services) {
for(BluetoothCharacteristic c in service.characteristics) {
if(c.uuid == new Guid("0000ffe1-0000-1000-8000-00805f9b34fb")) {
_setNotification(c);
} else {
print("Nope");
}
}
}
setState(() {
services = s;
});
_getServices();
});
}
});
}

关于android - Flutter Blue 设置通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879548/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com