connect method

  1. @override
Future<bool> connect(
  1. String deviceId,
  2. int timeout,
  3. void onDisconnect(
    1. String address
    )?
)
override

Implementation

@override
Future<bool> connect(
  String deviceId,
  int timeout,
  void Function(String address)? onDisconnect,
  ) async {

    if (onDisconnect != null) {
      _connectStreams[deviceId] =
        _connectEventChannel.receiveBroadcastStream().listen((result) {
        if (result["value"] == null) {
          throw PlatformException(code: "connect(): error retrieving value");
        }
        final addr = result["value"] as String;
        _connectStreams[addr]?.cancel();
        _connectStreams.remove(addr);
        onDisconnect.call(addr);
      });
    }

    final result = await _methodChannel.invokeMethod<bool?>('connect', {
      'deviceId': deviceId,
      'timeout': timeout,
    });
    if (result == null) {
      throw PlatformException(code: "connect(): error retrieving value");
    }
    return result;
}