diff --git a/lib/tools/adb-commands.js b/lib/tools/adb-commands.js index 700d5e18..3e1305a2 100644 --- a/lib/tools/adb-commands.js +++ b/lib/tools/adb-commands.js @@ -881,6 +881,26 @@ methods.setBluetoothOn = async function setBluetoothOn (on) { await this.shell(['cmd', 'bluetooth_manager', on ? 'enable' : 'disable']); }; +/** + * Change the state of the NFC service on the device under test. + * + * @this {import('../adb.js').ADB} + * @param {boolean} on - True to enable NFC service and false to disable it. + * @throws {Error} If there was an error while changing the service state + */ +methods.setNfcOn = async function setNfcOn (on) { + const {stdout, stderr} = await this.shell(['svc', 'nfc', on ? 'enable' : 'disable'], { + outputFormat: 'full' + }); + const output = stderr || stdout; + log.debug(output); + if (output.includes('null NfcAdapter')) { + throw new Error( + `Cannot turn ${on ? 'on' : 'off'} the NFC adapter. Does the device under test have it?` + ); + } +}; + /** * Broadcast the state of Airplane mode on the device under test. * This method should be called after {@link #setAirplaneMode}, otherwise