connect method

Future<bool> connect()

Initializes HIDAPI and opens the DualSense connected over USB.

Returns true if the connection succeeded (and starts the background read loop), false if no controller was found.

Implementation

Future<bool> connect() async {
  _bindings.hidInit();
  _device = _bindings.hidOpen(0x054C, 0x0CE6, ffi.nullptr);

  if (_device == ffi.nullptr) {
    return false;
  } else {
    // MANUAL C-STRING PARSER (native WChar to Dart String)
    final ffi.Pointer<ffi.WChar> stringBuffer = pkg_ffi.calloc<ffi.WChar>(255);
    final int result = _bindings.hidGetProductString(_device, stringBuffer, 255);
    if (result == 0) {
      final List<int> codes = [];
      int i = 0;
      while (stringBuffer[i] != 0) {
        codes.add(stringBuffer[i]);
        i++;
      }
      _deviceName = String.fromCharCodes(codes);
    }
    pkg_ffi.calloc.free(stringBuffer);

    _startListening();
    return true;
  }
}