batteries enimeration issue (windows) wip

This commit is contained in:
Sebastian Hildebrandt 2025-12-31 12:43:50 +01:00
parent eefd7dbd76
commit 3b0eae7462
2 changed files with 219 additions and 140 deletions

View File

@ -17,19 +17,20 @@ const exec = require('child_process').exec;
const fs = require('fs'); const fs = require('fs');
const util = require('./util'); const util = require('./util');
let _platform = process.platform; const _platform = process.platform;
const _linux = (_platform === 'linux' || _platform === 'android'); const _linux = _platform === 'linux' || _platform === 'android';
const _darwin = (_platform === 'darwin'); const _darwin = _platform === 'darwin';
const _windows = (_platform === 'win32'); const _windows = _platform === 'win32';
const _freebsd = (_platform === 'freebsd'); const _freebsd = _platform === 'freebsd';
const _openbsd = (_platform === 'openbsd'); const _openbsd = _platform === 'openbsd';
const _netbsd = (_platform === 'netbsd'); const _netbsd = _platform === 'netbsd';
const _sunos = (_platform === 'sunos'); const _sunos = _platform === 'sunos';
function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) { function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
const result = {}; const result = {};
let status = util.getValue(lines, 'BatteryStatus', ':').trim(); let status = parseInt(util.getValue(lines, 'BatteryStatus', ':').trim(), 10) || 0;
// let status = util.getValue(lines, 'BatteryStatus', ':').trim();
// 1 = "Discharging" // 1 = "Discharging"
// 2 = "On A/C" // 2 = "On A/C"
// 3 = "Fully Charged" // 3 = "Fully Charged"
@ -47,11 +48,11 @@ function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
result.hasBattery = true; result.hasBattery = true;
result.maxCapacity = fullChargeCapacity || parseInt(util.getValue(lines, 'DesignCapacity', ':') || 0); result.maxCapacity = fullChargeCapacity || parseInt(util.getValue(lines, 'DesignCapacity', ':') || 0);
result.designedCapacity = parseInt(util.getValue(lines, 'DesignCapacity', ':') || designedCapacity); result.designedCapacity = parseInt(util.getValue(lines, 'DesignCapacity', ':') || designedCapacity);
result.voltage = parseInt(util.getValue(lines, 'DesignVoltage', ':') || 0) / 1000.0; result.voltage = (parseInt(util.getValue(lines, 'DesignVoltage', ':'), 10) || 0) / 1000;
result.capacityUnit = 'mWh'; result.capacityUnit = 'mWh';
result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', ':') || 0); result.percent = parseInt(util.getValue(lines, 'EstimatedChargeRemaining', ':'), 10) || 0;
result.currentCapacity = parseInt(result.maxCapacity * result.percent / 100); result.currentCapacity = parseInt((result.maxCapacity * result.percent) / 100);
result.isCharging = (statusValue >= 6 && statusValue <= 9) || statusValue === 11 || ((statusValue !== 3) && (statusValue !== 1) && result.percent < 100); result.isCharging = (statusValue >= 6 && statusValue <= 9) || statusValue === 11 || (statusValue !== 3 && statusValue !== 1 && result.percent < 100);
result.acConnected = result.isCharging || statusValue === 2; result.acConnected = result.isCharging || statusValue === 2;
result.model = util.getValue(lines, 'DeviceID', ':'); result.model = util.getValue(lines, 'DeviceID', ':');
} else { } else {
@ -61,9 +62,8 @@ function parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity) {
return result; return result;
} }
module.exports = function (callback) { module.exports = (callback) =>
new Promise((resolve) => {
return new Promise((resolve) => {
process.nextTick(() => { process.nextTick(() => {
let result = { let result = {
hasBattery: false, hasBattery: false,
@ -105,22 +105,24 @@ module.exports = function (callback) {
} }
if (battery_path) { if (battery_path) {
fs.readFile(battery_path + 'uevent', function (error, stdout) { fs.readFile(battery_path + 'uevent', (error, stdout) => {
if (!error) { if (!error) {
let lines = stdout.toString().split('\n'); let lines = stdout.toString().split('\n');
result.isCharging = (util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging'); result.isCharging = util.getValue(lines, 'POWER_SUPPLY_STATUS', '=').toLowerCase() === 'charging';
result.acConnected = acConnected || result.isCharging; result.acConnected = acConnected || result.isCharging;
result.voltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_NOW', '='), 10) / 1000000.0; result.voltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_NOW', '='), 10) / 1000000.0;
result.capacityUnit = result.voltage ? 'mWh' : 'mAh'; result.capacityUnit = result.voltage ? 'mWh' : 'mAh';
result.cycleCount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10); result.cycleCount = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CYCLE_COUNT', '='), 10);
result.maxCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '=', true, true), 10) / 1000.0 * (result.voltage || 1)); result.maxCapacity = Math.round((parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL', '=', true, true), 10) / 1000.0) * (result.voltage || 1));
const desingedMinVoltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN', '='), 10) / 1000000.0; const desingedMinVoltage = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_VOLTAGE_MIN_DESIGN', '='), 10) / 1000000.0;
result.designedCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '=', true, true), 10) / 1000.0 * (desingedMinVoltage || result.voltage || 1)); result.designedCapacity = Math.round(
result.currentCapacity = Math.round(parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0 * (result.voltage || 1)); (parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_FULL_DESIGN', '=', true, true), 10) / 1000.0) * (desingedMinVoltage || result.voltage || 1)
);
result.currentCapacity = Math.round((parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_CHARGE_NOW', '='), 10) / 1000.0) * (result.voltage || 1));
if (!result.maxCapacity) { if (!result.maxCapacity) {
result.maxCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '=', true, true), 10) / 1000.0; result.maxCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL', '=', true, true), 10) / 1000.0;
result.designedCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL_DESIGN', '=', true, true), 10) / 1000.0 | result.maxCapacity; result.designedCapacity = (parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_FULL_DESIGN', '=', true, true), 10) / 1000.0) | result.maxCapacity;
result.currentCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10) / 1000.0; result.currentCapacity = parseInt('0' + util.getValue(lines, 'POWER_SUPPLY_ENERGY_NOW', '='), 10) / 1000.0;
} }
const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '='); const percent = util.getValue(lines, 'POWER_SUPPLY_CAPACITY', '=');
@ -133,41 +135,47 @@ module.exports = function (callback) {
if (result.maxCapacity && result.currentCapacity) { if (result.maxCapacity && result.currentCapacity) {
result.hasBattery = true; result.hasBattery = true;
if (!percent) { if (!percent) {
result.percent = 100.0 * result.currentCapacity / result.maxCapacity; result.percent = (100.0 * result.currentCapacity) / result.maxCapacity;
} }
} }
if (result.isCharging) { if (result.isCharging) {
result.hasBattery = true; result.hasBattery = true;
} }
if (energy && power) { if (energy && power) {
result.timeRemaining = Math.floor(energy / power * 60); result.timeRemaining = Math.floor((energy / power) * 60);
} else if (current && charge) { } else if (current && charge) {
result.timeRemaining = Math.floor(charge / current * 60); result.timeRemaining = Math.floor((charge / current) * 60);
} else if (current && result.currentCapacity) { } else if (current && result.currentCapacity) {
result.timeRemaining = Math.floor(result.currentCapacity / current * 60); result.timeRemaining = Math.floor((result.currentCapacity / current) * 60);
} }
result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '='); result.type = util.getValue(lines, 'POWER_SUPPLY_TECHNOLOGY', '=');
result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '='); result.model = util.getValue(lines, 'POWER_SUPPLY_MODEL_NAME', '=');
result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '='); result.manufacturer = util.getValue(lines, 'POWER_SUPPLY_MANUFACTURER', '=');
result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '='); result.serial = util.getValue(lines, 'POWER_SUPPLY_SERIAL_NUMBER', '=');
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
} else { } else {
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
} }
}); });
} else { } else {
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
} }
} }
if (_freebsd || _openbsd || _netbsd) { if (_freebsd || _openbsd || _netbsd) {
exec('sysctl -i hw.acpi.battery hw.acpi.acline', function (error, stdout) { exec('sysctl -i hw.acpi.battery hw.acpi.acline', (error, stdout) => {
let lines = stdout.toString().split('\n'); let lines = stdout.toString().split('\n');
const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10); const batteries = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.units'), 10);
const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10); const percent = parseInt('0' + util.getValue(lines, 'hw.acpi.battery.life'), 10);
result.hasBattery = (batteries > 0); result.hasBattery = batteries > 0;
result.cycleCount = null; result.cycleCount = null;
result.isCharging = util.getValue(lines, 'hw.acpi.acline') !== '1'; result.isCharging = util.getValue(lines, 'hw.acpi.acline') !== '1';
result.acConnected = result.isCharging; result.acConnected = result.isCharging;
@ -175,13 +183,17 @@ module.exports = function (callback) {
result.currentCapacity = null; result.currentCapacity = null;
result.capacityUnit = 'unknown'; result.capacityUnit = 'unknown';
result.percent = batteries ? percent : null; result.percent = batteries ? percent : null;
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
}); });
} }
if (_darwin) { if (_darwin) {
exec('ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|DesignCapacity|MaxCapacity|CurrentCapacity|DeviceName|BatterySerialNumber|Serial|TimeRemaining|Voltage"; pmset -g batt | grep %', function (error, stdout) { exec(
'ioreg -n AppleSmartBattery -r | egrep "CycleCount|IsCharging|DesignCapacity|MaxCapacity|CurrentCapacity|DeviceName|BatterySerialNumber|Serial|TimeRemaining|Voltage"; pmset -g batt | grep %',
(error, stdout) => {
if (stdout) { if (stdout) {
let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n'); let lines = stdout.toString().replace(/ +/g, '').replace(/"+/g, '').replace(/-/g, '').split('\n');
result.cycleCount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10); result.cycleCount = parseInt('0' + util.getValue(lines, 'cyclecount', '='), 10);
@ -203,8 +215,8 @@ module.exports = function (callback) {
} }
} }
if (parts && parts[1]) { if (parts && parts[1]) {
result.isCharging = (parts[1].trim() === 'charging'); result.isCharging = parts[1].trim() === 'charging';
result.acConnected = (parts[1].trim() !== 'discharging'); result.acConnected = parts[1].trim() !== 'discharging';
} else { } else {
result.isCharging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes'; result.isCharging = util.getValue(lines, 'ischarging', '=').toLowerCase() === 'yes';
result.acConnected = result.isCharging; result.acConnected = result.isCharging;
@ -212,18 +224,23 @@ module.exports = function (callback) {
if (result.maxCapacity && result.currentCapacity) { if (result.maxCapacity && result.currentCapacity) {
result.hasBattery = true; result.hasBattery = true;
result.type = 'Li-ion'; result.type = 'Li-ion';
result.percent = percent !== null ? percent : Math.round(100.0 * result.currentCapacity / result.maxCapacity); result.percent = percent !== null ? percent : Math.round((100.0 * result.currentCapacity) / result.maxCapacity);
if (!result.isCharging) { if (!result.isCharging) {
result.timeRemaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10); result.timeRemaining = parseInt('0' + util.getValue(lines, 'TimeRemaining', '='), 10);
} }
} }
} }
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
}); }
);
} }
if (_sunos) { if (_sunos) {
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
} }
if (_windows) { if (_windows) {
@ -232,30 +249,30 @@ module.exports = function (callback) {
workload.push(util.powerShell('Get-CimInstance Win32_Battery | select BatteryStatus, DesignCapacity, DesignVoltage, EstimatedChargeRemaining, DeviceID | fl')); workload.push(util.powerShell('Get-CimInstance Win32_Battery | select BatteryStatus, DesignCapacity, DesignVoltage, EstimatedChargeRemaining, DeviceID | fl'));
workload.push(util.powerShell('(Get-WmiObject -Class BatteryStaticData -Namespace ROOT/WMI).DesignedCapacity')); workload.push(util.powerShell('(Get-WmiObject -Class BatteryStaticData -Namespace ROOT/WMI).DesignedCapacity'));
workload.push(util.powerShell('(Get-CimInstance -Class BatteryFullChargedCapacity -Namespace ROOT/WMI).FullChargedCapacity')); workload.push(util.powerShell('(Get-CimInstance -Class BatteryFullChargedCapacity -Namespace ROOT/WMI).FullChargedCapacity'));
util.promiseAll( util.promiseAll(workload).then((data) => {
workload
).then((data) => {
if (data) { if (data) {
let parts = data.results[0].split(/\n\s*\n/); const parts = data.results[0].split(/\n\s*\n/);
let batteries = []; const batteries = [];
const hasValue = value => /\S/.test(value); const hasValue = (value) => /\S/.test(value);
for (let i = 0; i < parts.length; i++) { for (let i = 0; i < parts.length; i++) {
if (hasValue(parts[i]) && (!batteries.length || !hasValue(parts[i - 1]))) { // if (hasValue(parts[i]) && (!batteries.length || !hasValue(parts[i - 1]))) {
batteries.push([]); // batteries.push([]);
} // }
if (hasValue(parts[i])) { if (hasValue(parts[i])) {
batteries[batteries.length - 1].push(parts[i]); // batteries[batteries.length - 1].push(parts[i]);
batteries.push(parts[i]);
} }
} }
let designCapacities = data.results[1].split('\r\n').filter(e => e); const designCapacities = data.results[1].split('\r\n').filter((e) => e);
let fullChargeCapacities = data.results[2].split('\r\n').filter(e => e); const fullChargeCapacities = data.results[2].split('\r\n').filter((e) => e);
if (batteries.length) { if (batteries.length) {
let first = false; let first = false;
let additionalBatteries = []; const additionalBatteries = [];
for (let i = 0; i < batteries.length; i++) { for (let i = 0; i < batteries.length; i++) {
let lines = batteries[i][0].split('\r\n'); // let lines = batteries[i][0].split('\r\n');
const designedCapacity = designCapacities && designCapacities.length >= (i + 1) && designCapacities[i] ? util.toInt(designCapacities[i]) : 0; const lines = batteries[i].split('\r\n');
const fullChargeCapacity = fullChargeCapacities && fullChargeCapacities.length >= (i + 1) && fullChargeCapacities[i] ? util.toInt(fullChargeCapacities[i]) : 0; const designedCapacity = designCapacities && designCapacities.length >= i + 1 && designCapacities[i] ? util.toInt(designCapacities[i]) : 0;
const fullChargeCapacity = fullChargeCapacities && fullChargeCapacities.length >= i + 1 && fullChargeCapacities[i] ? util.toInt(fullChargeCapacities[i]) : 0;
const parsed = parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity); const parsed = parseWinBatteryPart(lines, designedCapacity, fullChargeCapacity);
if (!first && parsed.status > 0 && parsed.status !== 10) { if (!first && parsed.status > 0 && parsed.status !== 10) {
result.hasBattery = parsed.hasBattery; result.hasBattery = parsed.hasBattery;
@ -270,8 +287,7 @@ module.exports = function (callback) {
result.model = parsed.model; result.model = parsed.model;
first = true; first = true;
} else if (parsed.status !== -1) { } else if (parsed.status !== -1) {
additionalBatteries.push( additionalBatteries.push({
{
hasBattery: parsed.hasBattery, hasBattery: parsed.hasBattery,
maxCapacity: parsed.maxCapacity, maxCapacity: parsed.maxCapacity,
designedCapacity: parsed.designedCapacity, designedCapacity: parsed.designedCapacity,
@ -286,8 +302,7 @@ module.exports = function (callback) {
type: '', type: '',
manufacturer: '', manufacturer: '',
serial: '' serial: ''
} });
);
} }
} }
if (!first && additionalBatteries.length) { if (!first && additionalBatteries.length) {
@ -299,14 +314,17 @@ module.exports = function (callback) {
} }
} }
} }
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
}); });
} catch (e) { } catch {
if (callback) { callback(result); } if (callback) {
callback(result);
}
resolve(result); resolve(result);
} }
} }
}); });
}); });
};

View File

@ -22,28 +22,50 @@ const fs = require('fs');
let _platform = process.platform; let _platform = process.platform;
const _linux = (_platform === 'linux' || _platform === 'android'); const _linux = _platform === 'linux' || _platform === 'android';
const _darwin = (_platform === 'darwin'); const _darwin = _platform === 'darwin';
const _windows = (_platform === 'win32'); const _windows = _platform === 'win32';
const _freebsd = (_platform === 'freebsd'); const _freebsd = _platform === 'freebsd';
const _openbsd = (_platform === 'openbsd'); const _openbsd = _platform === 'openbsd';
const _netbsd = (_platform === 'netbsd'); const _netbsd = _platform === 'netbsd';
const _sunos = (_platform === 'sunos'); const _sunos = _platform === 'sunos';
function parseBluetoothType(str) { function parseBluetoothType(str) {
let result = ''; let result = '';
if (str.indexOf('keyboard') >= 0) { result = 'Keyboard'; } if (str.indexOf('keyboard') >= 0) {
if (str.indexOf('mouse') >= 0) { result = 'Mouse'; } result = 'Keyboard';
if (str.indexOf('trackpad') >= 0) { result = 'Trackpad'; } }
if (str.indexOf('speaker') >= 0) { result = 'Speaker'; } if (str.indexOf('mouse') >= 0) {
if (str.indexOf('headset') >= 0) { result = 'Headset'; } result = 'Mouse';
if (str.indexOf('phone') >= 0) { result = 'Phone'; } }
if (str.indexOf('macbook') >= 0) { result = 'Computer'; } if (str.indexOf('trackpad') >= 0) {
if (str.indexOf('imac') >= 0) { result = 'Computer'; } result = 'Trackpad';
if (str.indexOf('ipad') >= 0) { result = 'Tablet'; } }
if (str.indexOf('watch') >= 0) { result = 'Watch'; } if (str.indexOf('speaker') >= 0) {
if (str.indexOf('headphone') >= 0) { result = 'Headset'; } result = 'Speaker';
}
if (str.indexOf('headset') >= 0) {
result = 'Headset';
}
if (str.indexOf('phone') >= 0) {
result = 'Phone';
}
if (str.indexOf('macbook') >= 0) {
result = 'Computer';
}
if (str.indexOf('imac') >= 0) {
result = 'Computer';
}
if (str.indexOf('ipad') >= 0) {
result = 'Tablet';
}
if (str.indexOf('watch') >= 0) {
result = 'Watch';
}
if (str.indexOf('headphone') >= 0) {
result = 'Headset';
}
// to be continued ... // to be continued ...
return result; return result;
@ -52,13 +74,27 @@ function parseBluetoothType(str) {
function parseBluetoothManufacturer(str) { function parseBluetoothManufacturer(str) {
let result = str.split(' ')[0]; let result = str.split(' ')[0];
str = str.toLowerCase(); str = str.toLowerCase();
if (str.indexOf('apple') >= 0) { result = 'Apple'; } if (str.indexOf('apple') >= 0) {
if (str.indexOf('ipad') >= 0) { result = 'Apple'; } result = 'Apple';
if (str.indexOf('imac') >= 0) { result = 'Apple'; } }
if (str.indexOf('iphone') >= 0) { result = 'Apple'; } if (str.indexOf('ipad') >= 0) {
if (str.indexOf('magic mouse') >= 0) { result = 'Apple'; } result = 'Apple';
if (str.indexOf('magic track') >= 0) { result = 'Apple'; } }
if (str.indexOf('macbook') >= 0) { result = 'Apple'; } if (str.indexOf('imac') >= 0) {
result = 'Apple';
}
if (str.indexOf('iphone') >= 0) {
result = 'Apple';
}
if (str.indexOf('magic mouse') >= 0) {
result = 'Apple';
}
if (str.indexOf('magic track') >= 0) {
result = 'Apple';
}
if (str.indexOf('macbook') >= 0) {
result = 'Apple';
}
// to be continued ... // to be continued ...
return result; return result;
@ -86,7 +122,9 @@ function parseLinuxBluetoothInfo(lines, macAddr1, macAddr2) {
function parseDarwinBluetoothDevices(bluetoothObject, macAddr2) { function parseDarwinBluetoothDevices(bluetoothObject, macAddr2) {
const result = {}; const result = {};
const typeStr = ((bluetoothObject.device_minorClassOfDevice_string || bluetoothObject.device_majorClassOfDevice_string || bluetoothObject.device_minorType || '') + (bluetoothObject.device_name || '')).toLowerCase(); const typeStr = (
(bluetoothObject.device_minorClassOfDevice_string || bluetoothObject.device_majorClassOfDevice_string || bluetoothObject.device_minorType || '') + (bluetoothObject.device_name || '')
).toLowerCase();
result.device = bluetoothObject.device_services || ''; result.device = bluetoothObject.device_services || '';
result.name = bluetoothObject.device_name || ''; result.name = bluetoothObject.device_name || '';
@ -116,7 +154,6 @@ function parseWindowsBluetooth(lines) {
} }
function bluetoothDevices(callback) { function bluetoothDevices(callback) {
return new Promise((resolve) => { return new Promise((resolve) => {
process.nextTick(() => { process.nextTick(() => {
let result = []; let result = [];
@ -152,11 +189,17 @@ function bluetoothDevices(callback) {
} }
if (_darwin) { if (_darwin) {
let cmd = 'system_profiler SPBluetoothDataType -json'; let cmd = 'system_profiler SPBluetoothDataType -json';
exec(cmd, function (error, stdout) { exec(cmd, (error, stdout) => {
if (!error) { if (!error) {
try { try {
const outObj = JSON.parse(stdout.toString()); const outObj = JSON.parse(stdout.toString());
if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_title'] && outObj.SPBluetoothDataType[0]['device_title'].length) { if (
outObj.SPBluetoothDataType &&
outObj.SPBluetoothDataType.length &&
outObj.SPBluetoothDataType[0] &&
outObj.SPBluetoothDataType[0]['device_title'] &&
outObj.SPBluetoothDataType[0]['device_title'].length
) {
// missing: host BT Adapter macAddr () // missing: host BT Adapter macAddr ()
let macAddr2 = null; let macAddr2 = null;
if (outObj.SPBluetoothDataType[0]['local_device_title'] && outObj.SPBluetoothDataType[0].local_device_title.general_address) { if (outObj.SPBluetoothDataType[0]['local_device_title'] && outObj.SPBluetoothDataType[0].local_device_title.general_address) {
@ -173,8 +216,17 @@ function bluetoothDevices(callback) {
} }
}); });
} }
if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_connected'] && outObj.SPBluetoothDataType[0]['device_connected'].length) { if (
const macAddr2 = outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address ? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':') : null; outObj.SPBluetoothDataType &&
outObj.SPBluetoothDataType.length &&
outObj.SPBluetoothDataType[0] &&
outObj.SPBluetoothDataType[0]['device_connected'] &&
outObj.SPBluetoothDataType[0]['device_connected'].length
) {
const macAddr2 =
outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address
? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':')
: null;
outObj.SPBluetoothDataType[0]['device_connected'].forEach((element) => { outObj.SPBluetoothDataType[0]['device_connected'].forEach((element) => {
const obj = element; const obj = element;
const objKey = Object.keys(obj); const objKey = Object.keys(obj);
@ -187,8 +239,17 @@ function bluetoothDevices(callback) {
} }
}); });
} }
if (outObj.SPBluetoothDataType && outObj.SPBluetoothDataType.length && outObj.SPBluetoothDataType[0] && outObj.SPBluetoothDataType[0]['device_not_connected'] && outObj.SPBluetoothDataType[0]['device_not_connected'].length) { if (
const macAddr2 = outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address ? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':') : null; outObj.SPBluetoothDataType &&
outObj.SPBluetoothDataType.length &&
outObj.SPBluetoothDataType[0] &&
outObj.SPBluetoothDataType[0]['device_not_connected'] &&
outObj.SPBluetoothDataType[0]['device_not_connected'].length
) {
const macAddr2 =
outObj.SPBluetoothDataType[0].controller_properties && outObj.SPBluetoothDataType[0].controller_properties.controller_address
? outObj.SPBluetoothDataType[0].controller_properties.controller_address.toLowerCase().replace(/-/g, ':')
: null;
outObj.SPBluetoothDataType[0]['device_not_connected'].forEach((element) => { outObj.SPBluetoothDataType[0]['device_not_connected'].forEach((element) => {
const obj = element; const obj = element;
const objKey = Object.keys(obj); const objKey = Object.keys(obj);