【发布时间】:2018-10-18 17:32:04
【问题描述】:
我现在一直在使用颤振蓝色,但我坚持以下几点: 我正在使用我在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();
});
}
});
}
我也许有人对如何解决我的问题有建议。
罗宾
【问题讨论】:
-
罗宾,欢迎来到 SO。请您写下您自己问题的答案并解释“如何”。您是如何解决的,您在示例中更改了哪些代码等?这将有助于整个社区。谢谢。
标签: android ios bluetooth dart flutter