电源开关、扫描开关、可发现开关、配对设备、取消配对设备、列出设备 powershell 命令在 Windows 中不起作用
const { exec } = require('child_process');
/**
* Function to execute a command
* Helper function to run a PowerShell command
*
* @param {*} command
* @param {*} callback
*/
function executeCommand(command, callback) {
// `runas /user:Administrator "powershell -Command \\"${command}\\""`
// `"${command}"`
exec(`${command}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing PowerShell command: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(error, stdout.trim(), stderr)
callback(error, stdout.trim(), stderr);
});
}
/**
* Enable Bluetooth
*
*/
function turnOn(callback) {
const command = `powershell -Command "Start-Service bthserv"`;
executeCommand(command, (error, stdout, stderr) => {
console.log('Bluetooth enabled');
callback(stdout);
});
}
/**
* Disable Bluetooth
*
*/
function turnOff(callback) {
const command = `powershell -Command "Stop-Service bthserv"`;
executeCommand(command, (error, stdout, stderr) => {
console.log('Bluetooth disabled');
callback(stdout);
});
}
/**
* Start Scanning
*
* @param {*} callback
*/
function scanOn(callback) {
const command = 'powershell -Command "Start-BluetoothScan"';
executeCommand(command, callback);
}
/**
* Stop Scanning
*
* @param {*} callback
*/
function scanOff(callback) {
const command = 'powershell -Command "Stop-BluetoothScan"';
executeCommand(command, callback);
}
/**
* Make Pairable On
*
* @param {*} callback
*/
function pairableOn(callback) {
const command = 'powershell -Command "Set-BluetoothPairable -On"';
executeCommand(command, callback);
}
/**
* Make Pairable Off
*
* @param {*} callback
*/
function pairableOff(callback) {
const command = `powershell -Command "Set-BluetoothPairable -Off"`;
executeCommand(command, callback);
}
我得到的错误如下:
// // // Example usage
// agentOn((result) => console.log('Agent turned on:', result));
// agentOff((result) => console.log('Agent turned off:', result));
// scanOn((result) => console.log('Scanning started:', result));
// scanOff((result) => console.log('Scanning stopped:', result));
// powerOn((result) => console.log('Power started:', result));
// powerOff((result) => console.log('Power stopped:', result));
Error executing PowerShell command: Command failed: powershell -Command "Set-BluetoothPairable -Off"
Set-BluetoothPairable : The term 'Set-BluetoothPairable' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Set-BluetoothPairable -Off
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-BluetoothPairable:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error executing PowerShell command: Command failed: powershell -Command "Stop-BluetoothScan"
Stop-BluetoothScan : The term 'Stop-BluetoothScan' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Stop-BluetoothScan
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Stop-BluetoothScan:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error executing PowerShell command: Command failed: powershell -Command "Start-BluetoothScan"
Start-BluetoothScan : The term 'Start-BluetoothScan' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Start-BluetoothScan
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Start-BluetoothScan:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error executing PowerShell command: Command failed: powershell -Command "Disable-BluetoothDevice -Agent"
Disable-BluetoothDevice : The term 'Disable-BluetoothDevice' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Disable-BluetoothDevice -Agent
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Disable-BluetoothDevice:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error executing PowerShell command: Command failed: powershell -Command "Enable-BluetoothDevice -Agent"
Enable-BluetoothDevice : The term 'Enable-BluetoothDevice' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Enable-BluetoothDevice -Agent
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Enable-BluetoothDevice:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Error executing PowerShell command: Command failed: powershell -Command "Set-BluetoothPairable -On"
Set-BluetoothPairable : The term 'Set-BluetoothPairable' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Set-BluetoothPairable -On
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-BluetoothPairable:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
问题在于 Windows 桌面上的标准 PowerShell 没有内置的 cmdlet 用于直接控制蓝牙功能,如电源开/关、扫描、配对等。这些功能通常通过 Windows 设置应用程序或专用蓝牙来处理管理软件。
为什么标准 PowerShell 命令不起作用 有限的蓝牙集成:PowerShell 主要关注系统管理和脚本自动化,而蓝牙控制是其标准模块中未广泛涵盖的领域。 Windows 设置应用程序:Windows 设置应用程序是设计用于管理蓝牙设备和设置的主要界面。 第三方工具:除了“设置”应用程序提供的基本功能之外,许多专用工具还提供更全面的蓝牙管理功能。