• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Huawei Hongmeng Mobile Office Practical Operation: "Seamless Collaboration Tips" between WLAN P2P and Bluetooth SPP

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
hello!I am Xiao L, the female programmer who "treads needles" in Hongmengduo Equipment Cooperation~ Have you ever been tortured by "slow file transmission" in a meeting?When sending a mobile phone to a computer, you need to scan the code, plug the cable, or even turn on a hot spot?Today I will teach you how to use Hongmeng’s WLAN P2P and Bluetooth SPP to create a mobile office experience of “second transfer + second connection”, so that passing files between devices is as fast as “face-to-face delivery notes”!

1. Scenario analysis: "Transmission Pain Points" of Mobile Office

(I) "Efficiency Bottleneck" in Typical Scenarios

  1. Large File Transfer
  2. Share a 1GB demonstration video during the meeting, use WeChat to wait for 5 minutes, and use the data cable to find the interface~

  3. Hongmeng Solution: WLAN P2P direct connection, transmission is completed within 10 seconds!


  4. Real-time state synchronization


  5. Voting results and meeting schedule updates, multiple devices need to be synchronized in real time, and Bluetooth pop-up confirmation is too troublesome~


  6. Hongmeng Solution: Real-time push of Bluetooth SPP, millisecond response!


  7. Cross-platform collaboration


  8. Passing files between mobile phones (Hongmeng), tablets (Android), and computers (Windows), poor compatibility~


  9. Hongmeng Solution: Automatic adaptation protocol, one-click transmission across systems!
2. Architecture design: "fast and slow" transmission combination punch

(I) WLAN P2P: "Highway" with large files

  • Technical Highlights:
  • Direct connection does not depend on the router, the transmission rate can reach 300Mbps (100 times faster than Bluetooth!)

  • Supports multi-device networking (1 master and 8 slaves), suitable for "one-to-many" sharing in meetings


  • Application Scenarios:

Large file transfer: video, CAD drawings, virtual machine images
Real-time collaborative editing: Multiple people modify cloud documents at the same time (simultaneous synchronization through P2P)
(II) Bluetooth SPP: "light trail" for small data

  • Technical Highlights:
  • Low power consumption (standby power consumption <0.01mW), suitable for long-term background operation

  • Fast connection (after first pairing, reconnection takes only 0.5 seconds)


  • Application Scenarios:

Status synchronization: device online status, power, transmission progress
Control commands: remote control page turn, conference voting, device wake-up
Small file transfer: meeting minutes (<1MB), contact business card
3. Core implementation: the entire process from "building a company" to "transmitting data"

(I) WLAN P2P connection optimization: "3-second fast pairing"


import { wifiManager } from '@ohos.wifiManager';

async function Quickly connect to P2P (target device Id: string) {
// 1. Turn on P2P and search the device (optimized search interval is 500ms)
await wifiManager.startP2p({ scanInterval: 500 });

// 2. Pre-connection: Send fast pairing request (skip complex authentication)
const pairing information = {
security: 'none', // Encryption can be turned off in temporary meeting scenarios
timeout: 3000 // Timeout time is set to 3 seconds
};
await wifiManager.connectP2pDevice (target device Id, pairing information);

// 3. Get Socket and set buffer (improve large file transfer efficiency)
const socket = await wifiManager.getP2pSocket({
receiveBufferSize: 1024*1024, // 1MB buffer
sendBufferSize: 1024*1024
});
return socket;
}
(II) Bluetooth SPP data channel: "Low power heartbeat mechanism"


import { socket } from '@kit.ConnectivityKit';

// 1. Establish an encrypted connection (automatic memory pairing device)
let socketID: number;
function Connect to Bluetooth device (Bluetooth address: string) {
socket.sppConnect(Bluetooth address, {
uuid: '00001101-0000-1000-8000-00805f9b34fb',
secure: true, // Enable AES-128 encryption
autoConnect: true // Automatically reconnect after disconnection
}, (code, id) => {
if (code) return;
socketID = id;
Start heartbeat(); // Start state synchronization after successful connection
});
}

// 2. Heartbeat mechanism: Synchronize device status once every second
function starts heartbeat() {
setInterval(() => {
const status data = {
deviceId: 'Current device ID',
status: 'ready',
battery: 85 // Power percentage
};
const data = new TextEncoder().encode(JSON.stringify(status data));
socket.sppWrite(socketID, data.buffer);
}, 1000);
}
(III) Intelligent routing: Automatically select the "optimal transmission channel"


function Select the transfer method (file size: number) {
if (file size > 5*1024*1024) { // Use P2P above 5MB
return 'WLAN_P2P';
} else if (file size > 1024) { // 1KB-5MB Bluetooth SPP
return 'BLUETOOTH_SPP';
} else { // Use NFC to pass with less than 1KB (requires hardware support)
return 'NFC';
}
}

// Example: Dynamically switch according to file type
const file = await Get the selected file();
const Transfer method = Select the transfer method (file.size);
if (transmission method === 'WLAN_P2P') {
await Quickly connect to P2P (receive device Id).then(socket => Transfer large files (socket, file));
} else {
Connect to Bluetooth device (receive device ID).then(() => Transfer small data (socketID, file));
}
4. Performance optimization: Make transmission "fast and stable"

(I) "Throughput Optimization" of WLAN P2P

  1. Blocked transfer + parallel processing

async function Transfer large files (socket: Socket, file: File) {
const block size = 10*1024*1024; // 10MB block
const Total number of blocks = Math.ceil(file.size/block size);
const parallel number = 4; // 4 blocks are passed at the same time

for (let i=0; i<total number of blocks; i+=parallel number) {
const block range = Array.from({length:parallel number}, (_, idx) => i+idx)
.filter(idx => idx <Total number of blocks);

await Promise.all(block range.map(async (idx) => {
const data = await file.slice(idx*block size, (idx+1)*block size);
await socket.write(data);
}));
}
}
  1. Error retransmission mechanism
  2. For every block received by the receiver, return to ACK confirmation
  3. The sending end does not receive ACK, and it will be retransmitted after 3 seconds (up to 3 retry)
(II) "Power Consumption Optimization" of Bluetooth SPP

  1. Sleep mode
  2. When there is no data transmission, it will automatically enter sleep (power consumption is reduced to 0.001mW)

socket.setPowerMode(socketID, 'sleep'); // Sleep mode
  1. Data compression
  2. Automatic compression when transmitting text data (compression rate can reach 70%)

const compressed data = await compress(text data, 'deflate');
socket.sppWrite(socketID, compressed data);
5. Practical cases: "Conference Scene" full process demonstration

Scenario: Share 1.5GB of demonstration video in a 10-person meeting

Step 1: Create a P2P Group


// The speaker device (notebook) is used as Group Owner
await wifiManager.createP2pGroup();
const groupInfo = await wifiManager.getP2pGroupInfo();
console.log('Group IP:', groupInfo.groupAddress);
Step 2: Quickly join the participating devices (mobile phone/tablet)


// After the participating devices search for the group, they will automatically connect (no password required)
const device = await wifiManager.searchP2pDevices().then(devices =>
devices.find(d => d.isGroupOwner)
);
await wifiManager.connectP2pDevice(device.deviceId, { security: 'none' });
Step 3: Transfer files in parallel (speaker → all participating devices)


// The speaker turns on multicast transmission
const socket = await wifiManager.getP2pMulticastSocket();
for (const participating equipment of participating equipment list) {
socket.addMember(participating device.deviceId); // Add recipient
}
await socket.sendFile('presentation.zip'); // Send compressed packet
Effect:

  • 1.5GB file transfer time: 18 seconds (Traditional Bluetooth takes 20 minutes, and normal WiFi direct connection takes 1 minute and 30 seconds)
  • Influence of power consumption: The speaker's equipment consumes 3% of its power, and the participating equipment consumes an average of 1.5%
6. Pit avoidance guide: Key details of "zero stutter" transmission

(I) WLAN P2P connection failed

  • Reason 1: The equipment distance is too far (>50 meters)

  • Solution: Turn on the device "Signal Enhancement Mode":

    wifiManager.setP2pTxPower('high'); // High power mode, transmission distance is increased to 80 meters

  • Reason 2: Frequency band conflict (2.4G/5G interference)


  • Solution: Forced use of 5G bands:

    wifiManager.connectP2pDevice(device Id, { band: '5GHz' });
(II) Bluetooth SPP data loss

  • Reason: The device goes to sleep during transmission

  • Solution: Keep the device awake during transmission:

    import { powerManager } from '@ohos.powerManager';
    powerManager.acquireWakeLock('PARTIAL_WAKE_LOCK'); // Keep CPU awake
(III) Cross-system compatibility issues

  • Scenario: Hongmeng device communicates with iOS/Android device
  • solve:
  • WLAN P2P: Use the common SMB protocol to transfer files
  • Bluetooth SPP: iOS-compatible GATT protocol, automatically converts data formats
Last chat


The combination of WLAN P2P and Bluetooth SPP is like installing a dual-way network of "highway + country road" on the device - large files go on the highway, fast and stable; small data goes on the trail, flexible and low consumption.Through intelligent scheduling, Hongmeng completely frees mobile office from the "wire constraints" and "transmission waiting".Next time, let’s talk about “how to use Hongmeng’s distributed file system to achieve “device-free mutual access”, such as directly accessing the mobile phone folder by computer – remember to follow!?

(Please indicate the source and original author Xiao L when reprinting. Violators... Let your device "can't find the device" every time you send a file! It's kidding~)


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу