蓝颤动设置通知 [英] Flutter Blue Setting Notifications
问题描述
我一直在使用flutter Blue,但我坚持以下几点: 我使用的是我在 https://github.com/pauldemarco/flutter_blue 上下载的示例应用程序,通过这里的基本思想是,一旦我连接到蓝牙设备,它就会开始检查服务"FFE0"是否存在,然后检查特征"FFE1". 这个特性吐出了我的项目所需的随机字符串.
I've been using flutter Blue for some now and I'm stuck on the following: I'm using the example App I downloaded on https://github.com/pauldemarco/flutter_blue, through here the basic idea is that as soon as I connect to my bluetooth device it starts checking if the service "FFE0" exists and then the characteristic "FFE1". This Characteristic spits out random strings I need for my project.
通过屏幕,我可以看到上面的内容是正确的,我只需要在连接到蓝牙设备后就以某种方式自动为该特性设置通知.
Through the screen I can see the above is true I just need to somehow automatically set notifications for the characteristic as soon as it connects to the bluetooth device.
这是我正在_Connect函数中测试的一些当前代码.
This is some current code i'm testing out in the _Connect Function.
_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();
});
}
});
}
我也许有人对如何解决我的问题提出了建议.
I maybe someone has a suggestion on how to approach my problem.
罗宾
推荐答案
I found https://github.com/Sensirion/smart-gadget-flutter/tree/master/lib and I was able to fix my problem using the following code:
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函数中添加的.
This was added in the _connect function.
_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();
});
}
});
}
这篇关于蓝颤动设置通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!