ADHD-Closet

NFC Tag Support

This document describes the NFC tag functionality for tracking clothing items when removed from and returned to the closet.

Overview

The NFC feature allows you to:

NFC works in:

Web NFC API Support

This app now supports the Web NFC API, which enables NFC functionality directly in web browsers on Android without requiring a native app build!

Browser Compatibility

Browser Platform Support Version
Chrome Android ✅ Yes 89+
Edge Android ✅ Yes 89+
Safari iOS ❌ No -
Firefox Android ❌ No -
Desktop Any ❌ No -

Requirements for Web NFC

  1. HTTPS Connection: Web NFC requires a secure context (HTTPS), except for localhost
  2. Android Device: Must have NFC hardware
  3. NFC Enabled: NFC must be enabled in device settings
  4. Browser Permissions: User must grant NFC permission when prompted

How It Works

The app automatically detects the best NFC method available:

  1. Native Apps: Uses Capacitor NFC plugin (iOS/Android)
  2. Web Browser: Falls back to Web NFC API (Chrome/Edge on Android)
  3. Not Available: Shows friendly message if NFC not supported

No code changes needed - the app handles the fallback automatically!

Database Schema

NFCTag Model

Stores the association between NFC tags and items:

NFCEvent Model

Tracks all NFC scan events:

Setup

1. Database Migration

Run the following migration to add NFC support:

cd app
npx prisma migrate dev --name add_nfc_support
npx prisma generate

2. Using Web NFC (Chrome/Edge on Android)

No setup required! Just:

  1. Open the app in Chrome or Edge on your Android device
  2. Make sure you’re using HTTPS (or localhost for testing)
  3. Enable NFC in your Android device settings
  4. Grant NFC permission when prompted by the browser

The app will automatically use the Web NFC API.

3. Native App Setup (Optional - iOS/Android)

For iOS support or enhanced Android experience, you can build a native app. See MOBILE_APPS.md for building instructions.

iOS Permissions

Add to ios/App/App/Info.plist:

<key>NFCReaderUsageDescription</key>
<string>We need NFC access to scan clothing tags</string>

Android Permissions

Add to android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="false" />

Usage

From Item Detail Page

  1. Navigate to any item’s detail page
  2. Scroll to the “NFC Tag” section
  3. Tap “📡 Assign NFC Tag”
  4. Hold your phone near the NFC tag
  5. (Optional) Add a label like “Blue hanger”
  6. Tap “✓ Confirm Assignment”

Quick Scanning

  1. Navigate to /nfc-scan or use the NFC Scan shortcut
  2. Hold your phone near an NFC tag
  3. When item is detected, tap:
    • “👕 Removed from Closet” - when taking the item out
    • “✅ Returned to Closet” - when putting it back

What Happens on Scan

When marking as “Removed”:

When marking as “Returned”:

API Endpoints

POST /api/nfc/assign

Associate an NFC tag with an item.

Request:

{
  "tagId": "04:A1:B2:C3:D4:E5:F6",
  "itemId": "clx123...",
  "label": "Blue hanger (optional)"
}

Response:

{
  "success": true,
  "nfcTag": {
    "id": "...",
    "tagId": "04:A1:B2:C3:D4:E5:F6",
    "itemId": "...",
    "label": "Blue hanger",
    "item": { ... }
  }
}

DELETE /api/nfc/assign

Remove an NFC tag assignment.

Query params: ?tagId=... or ?itemId=...

Response:

{
  "success": true,
  "deletedCount": 1
}

POST /api/nfc/scan

Record an NFC tag scan event.

Request:

{
  "tagId": "04:A1:B2:C3:D4:E5:F6",
  "action": "removed",  // or "returned"
  "notes": "Going to work (optional)"
}

Response:

{
  "success": true,
  "event": { ... },
  "item": { ... },
  "message": "Blue Shirt marked as removed from closet"
}

GET /api/nfc/tags

List all NFC tags with their items and recent events.

Response:

{
  "tags": [
    {
      "id": "...",
      "tagId": "04:A1:B2:C3:D4:E5:F6",
      "label": "Blue hanger",
      "item": { ... },
      "recentEvents": [ ... ]
    }
  ],
  "total": 5
}

React Hooks

useNFC()

Custom hook for NFC functionality.

import { useNFC } from '@/app/lib/hooks/useCapacitor';

function MyComponent() {
  const nfc = useNFC();

  const scanTag = async () => {
    if (!nfc.isAvailable || !nfc.isEnabled) {
      alert('NFC not available');
      return;
    }

    const tagId = await nfc.startScanning();
    console.log('Scanned:', tagId);
  };

  return (
    <div>
      {nfc.isSupported ? (
        <button onClick={scanTag} disabled={nfc.isScanning}>
          {nfc.isScanning ? 'Scanning...' : 'Scan NFC Tag'}
        </button>
      ) : (
        <p>NFC not supported on this device</p>
      )}
    </div>
  );
}

Available Properties

Components

NFCScanner

Full-featured NFC scanner with UI.

import NFCScanner from '@/app/components/NFCScanner';

<NFCScanner
  onTagScanned={(tagId, itemInfo) => {
    console.log('Tag scanned:', tagId, itemInfo);
  }}
  onError={(error) => {
    console.error('Error:', error);
  }}
  autoScan={true}  // Start scanning automatically
/>

NFCAssign

Component for assigning NFC tags to items.

import NFCAssign from '@/app/components/NFCAssign';

<NFCAssign
  itemId={item.id}
  itemTitle={item.title}
  currentTag={nfcTag}
  onAssigned={(tag) => setNfcTag(tag)}
  onRemoved={() => setNfcTag(null)}
/>

Troubleshooting

NFC Not Working

  1. Check device and browser compatibility:
    • Web (Chrome/Edge on Android):
      • Chrome 89+ or Edge 89+ on Android
      • HTTPS connection required (or localhost)
      • NFC enabled in Android settings
      • Grant NFC permission when prompted
    • Native Apps:
      • iPhone 7 or later (iOS 11+)
      • Most Android phones (Android 4.0+)
      • NFC must be enabled in Settings
  2. Check app permissions:
    • Web (Chrome/Edge): Grant NFC permission when browser prompts
    • iOS Native: Settings > Your App > NFC
    • Android Native: Settings > Apps > Your App > Permissions
  3. Tag positioning:
    • Hold phone flat against tag (back of phone)
    • Keep steady for 1-2 seconds
    • Remove any metal cases that may interfere
  4. Tag compatibility:
    • Use NFC Forum Type 2 tags (NTAG213/215/216)
    • ISO/IEC 14443 Type A tags
    • Avoid metal surfaces near tags

Common Errors

“NFC is not available on this device”

“Please enable NFC in your device settings”

“NotAllowedError” in browser

“NFC tag is already assigned to another item”

Web NFC Specific Issues

“NDEFReader is not defined”

HTTPS Required Error

Permission Denied

Best Practices

  1. Label your tags: Use descriptive labels like “Blue hanger” or “Closet section A”

  2. Consistent placement: Always place tags in the same spot on hangers

  3. Tag quality: Use good quality NFC tags (NTAG213 recommended)

  4. Backup: NFC tags can fail - keep item info in the app too

  5. Regular checks: Periodically verify tag associations are still correct

Future Enhancements

Potential future features:

Resources


Quick Reference

Scanning an NFC Tag

  1. Tap the NFC icon in the app toolbar
  2. Hold your phone near the NFC tag (on hanger or item)
  3. Watch for feedback - vibration + success message
  4. Item updates automatically - status changes to “worn” or “available”

Best Practices

Troubleshooting Quick Fixes

Supported Devices