addNotifyListener method

void addNotifyListener(
  1. Uuid service,
  2. Uuid characteristic,
  3. NotifyListener listener
)

添加通知监听

Implementation

void addNotifyListener(
    Uuid service, Uuid characteristic, NotifyListener listener) {
  String key = service.toString().toLowerCase() +
      "-" +
      characteristic.toString().toLowerCase();

  late _NotifyData notifyData;
  if (notifyMap.containsKey(key)) {
    notifyData = notifyMap[key]!;
  } else {
    notifyData = _NotifyData(service, characteristic);
    notifyMap[key] = notifyData;
  }
  notifyData.addListener(listener);

  //如果已经建立连接了则直接建立订阅
  if (_device.connected) {
    //没注册过就注册进去一个
    if (notifyData.streamSubscription == null) {
      StreamSubscription<Uint8List> stream = _device
          .subscribeToCharacteristic(service, characteristic)
          .listen((data) {
        notifyData.callAll(_self, data);
      });
      notifyData.streamSubscription = stream;
    }
  }
}