Skip to content

Instantly share code, notes, and snippets.

@ljanecek
Created May 13, 2024 13:00
Show Gist options
  • Save ljanecek/31d3ef241ae1228586bae12e18f6c603 to your computer and use it in GitHub Desktop.
Save ljanecek/31d3ef241ae1228586bae12e18f6c603 to your computer and use it in GitHub Desktop.
Flutter Dart NFC Wifi Record Type
import 'dart:typed_data';
import 'package:ndef/ndef.dart' as ndef;
class WifiNdefRecord {
static ndef.NDEFRecord createWifiRecord(String ssid, String password) {
Uint8List ssidByte = Uint8List.fromList(ssid.codeUnits);
Uint8List passwordByte = Uint8List.fromList(password.codeUnits);
Uint8List ssidLength = Uint8List.fromList([(ssid.length >> 8) & 0xFF, ssid.length & 0xFF]);
Uint8List passwordLength = Uint8List.fromList([(password.length >> 8) & 0xFF, password.length & 0xFF]);
Uint8List credential = Uint8List.fromList([0x10, 0x0E]); // CREDENTIAL
Uint8List networkName = Uint8List.fromList([0x10, 0x45]); // NETWORK_NAME
Uint8List networkKey = Uint8List.fromList([0x10, 0x27]); // NETWORK_KEY
int totalLen = ssidByte.length + passwordByte.length + 4;
Uint8List totalLenBytes = Uint8List.fromList([(totalLen >> 8) & 0xFF, totalLen & 0xFF]);
Uint8List payload = _concat([
credential,
totalLenBytes,
networkName,
ssidLength,
ssidByte,
networkKey,
passwordLength,
passwordByte,
]);
Uint8List type = Uint8List.fromList('application/vnd.wfa.wsc'.codeUnits);
return ndef.NDEFRecord(
tnf: ndef.TypeNameFormat.media,
type: type,
payload: payload,
);
}
static Uint8List _concat(List<Uint8List> byteLists) {
return Uint8List.fromList(byteLists.expand((x) => x).toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment