react-native-ble-nitro
Version:
High-performance React Native BLE library built on Nitro Modules - drop-in replacement for react-native-ble-plx
71 lines (70 loc) • 1.96 kB
JavaScript
/**
* Service Data compatibility layer
*
* Provides conversion between Nitro's structured ServiceDataEntry[] format
* and the original { [uuid: string]: Base64 } format from react-native-ble-plx
*/
/**
* Convert ServiceDataEntry array to the original index signature format
*/
export function serviceDataArrayToMap(entries) {
if (!entries || entries.length === 0) {
return null;
}
const result = {};
entries.forEach(entry => {
result[entry.uuid] = entry.data;
});
return result;
}
/**
* Convert the original index signature format to ServiceDataEntry array
*/
export function serviceDataMapToArray(map) {
if (!map || Object.keys(map).length === 0) {
return null;
}
return Object.entries(map).map(([uuid, data]) => ({
uuid: uuid,
data,
}));
}
/**
* Merge two service data maps (used in device updates)
*/
export function mergeServiceDataMaps(existing, updates) {
if (!existing && !updates)
return null;
if (!existing)
return updates;
if (!updates)
return existing;
return { ...existing, ...updates };
}
/**
* Merge two service data arrays (used in native updates)
*/
export function mergeServiceDataArrays(existing, updates) {
const existingMap = serviceDataArrayToMap(existing);
const updatesMap = serviceDataArrayToMap(updates);
const mergedMap = mergeServiceDataMaps(existingMap, updatesMap);
return serviceDataMapToArray(mergedMap);
}
/**
* Check if service data contains a specific service UUID
*/
export function hasServiceUUID(serviceData, uuid) {
return serviceData ? uuid in serviceData : false;
}
/**
* Get service data for a specific UUID
*/
export function getServiceData(serviceData, uuid) {
return serviceData?.[uuid] || null;
}
/**
* Get all service UUIDs from service data
*/
export function getServiceUUIDs(serviceData) {
return serviceData ? Object.keys(serviceData) : [];
}