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



This content originally appeared on DEV Community and was authored by 魔眼天王

​*

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)
  • ​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();  
}  

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');  
});  

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);  

4. Result Handling

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

​*

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]  

​*

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);  
  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']);  
  1. ​Error Handling​
   try {  
     await device.connect();  
   } catch (e) {  
     if (e.code === 'DEVICE_BUSY') {  
       showToast('Device busy, retry');  
     }  
   }  
  1. ​Optimization Tips​
  • Use shareData.compress() for content reduction

  • Chunk large files (≤500KB per fragment)


This content originally appeared on DEV Community and was authored by 魔眼天王