DEV Community

魔眼天王
魔眼天王

Posted on

Core Implementation Principles and Development Steps of HarmonyOS "Tap-to-Share" Feature

​*

I. Technical Principles

HarmonyOS "Tap-to-Share" leverages ​NFC + Distributed Soft Bus​ dual engines, with core architecture:

Physical Layer (NFC Chip) ↔ Protocol Layer (HMS Core) ↔ Distributed Layer (Soft Bus) 
Enter fullscreen mode Exit fullscreen mode
  • ​NFC Trigger: Integrated NFC sensing area on device top, communicating via ISO/IEC 14443 protocol within 10cm range

  • ​Distributed Communication: Establishes secure channels through RPC framework with AES-256-CBC end-to-end encryption

  • ​ArkUI Rendering: Dynamically generates share cards using declarative UI framework for cross-device synchronization

​*

II. Development Implementation

1. Environment Configuration

// Check device compatibility if (canIUse('SystemCapability.Collaboration.HarmonyShare')) { // Initialize distributed capabilities await distributedData.init(); } 
Enter fullscreen mode Exit fullscreen mode

2. Event Registration

// Register tap-to-share listener harmonyShare.on('knockShare', (event) => { const target = event.sharableTarget; handleShare(target); }); // Remove listener on page unload onPageHide(() => { harmonyShare.off('knockShare'); }); 
Enter fullscreen mode Exit fullscreen mode

3. Data Construction & Trigger

// Build share data object const shareData = new ShareData({ type: ShareType.LINK, content: 'https://example.com', thumbnail: fileUri.getUriFromPath('/images/share.png'), title: 'Project Proposal', description: '2025 Technical Roadmap' }); // Initiate tap-to-share const targetDevice = await findNearbyDevice(); targetDevice.share(shareData); 
Enter fullscreen mode Exit fullscreen mode

4. Result Handling

// Monitor transfer status distributedData.on('transferStatus', (status) => { if (status === 'SUCCESS') { showToast('Transfer completed'); } else { showError('Transfer failed'); } }); 
Enter fullscreen mode Exit fullscreen mode

​*

III. Technical Advantages

  1. ​Millisecond Device Discovery​
  • Utilizes NFC ATR protocol for device fingerprinting (<80ms response)
  1. ​Secure Transmission​
  • Device fingerprint mutual authentication (TEE-based)

  • Fragmented encryption (128B chunks with dynamic key rotation)

  1. ​Smart Context Recognition​
 graph LR A[Tap] --> B{Content Identification} B -->|Image| C[Launch Gallery] B -->|Text| D[Launch Notes] B -->|File| E[Launch File Manager] 
Enter fullscreen mode Exit fullscreen mode

​*

IV. Performance Metrics

Scenario Transfer Speed Success Rate Latency
1MB Image 24 MB/s 99.8% 120ms
10MB Document 18 MB/s 99.5% 150ms
100MB Video 12 MB/s 98.2% 210ms

​*

V. Use Cases

  1. ​Cross-Device Sharing​
  • Phone → Tablet: Design files with layer preservation

  • PC → Phone: Meeting notes auto-sync to Notes app

  1. ​Smart Device Control​
 // Connect to smart lighting const device = await findDeviceByType('LIGHT'); await device.executeCommand('setBrightness', 80); 
Enter fullscreen mode Exit fullscreen mode
  1. ​Contactless Payments​
  • Phone tap POS for payments (no app launch)

  • Public transit card tap for instant recharge

​*

VI. Comparative Advantages

Aspect Traditional NFC HarmonyOS Tap-to-Share
Interaction Requires dedicated APP System-native support
Efficiency Protocol-dependent Direct device connection
Scalability Single-function Dynamic capability expansion
Security Device-level auth App+User dual-factor

​*

VII. Implementation Notes

  1. ​Permission Management​
 // Request necessary permissions requestPermissions(['ohos.permission.NFC', 'ohos.permission.INTERNET']); 
Enter fullscreen mode Exit fullscreen mode
  1. ​Error Handling​
 try { await device.connect(); } catch (e) { if (e.code === 'DEVICE_BUSY') { showToast('Device busy, retry'); } } 
Enter fullscreen mode Exit fullscreen mode
  1. ​Optimization Tips​
  • Use shareData.compress() for content reduction

  • Chunk large files (≤500KB per fragment)

Top comments (0)