Launch APP search and play songs

This example uses the Meizu MX4 mobile phone, start QQ music APP search "My Hometown", automatically close QQ music after playing for two minutes (currently using absolute coordinates).

Programming language: Node JS

Note:

1. You need to install QQ Music APP on your mobile phone to open the login once.

2. Nodejs install request module (npm install --save request)

3. TC must turn on sigma input method

Steps:

1. Get Token

2. Get device name

3. Start QQ music

4. Search for songs "My hometown"

5. Play "My hometown"

6. Close QQ music after 2 minutes

7. Execute: node app.js

Source code

/*
 * Start QQ music app to search for a song to play, close the app after two minutes of play
 * 
 * Take Meizu MX4 mobile phone as an example
 * Need to install QQ music
 */

//Install request module: npm install --save request
const request = require('request');

const deviceUrlBase = 'http://127.0.0.1:8090/TotalControl/v1';
const runAppName = 'com.tencent.qqmusic'; //The package name to run the APP (the second step method is obtained)

//REST API account: sigma  password: jiehua
/*
var _buf = new Buffer('sigma:jiehua');
var _str = b.toString('base64');
 */
//console.log(_str);
//Result:c2lnbWE6amllaHVh

//Start 
request({
	url: deviceUrlBase + '/login',
    method: "GET",
    json: true,
    headers: {
        "Authorization": "c2lnbWE6amllaHVh",
    }
}, function (error, response, body) {
	if (!error && response.statusCode == 200) {
		var _token = body.value.token;
		console.log('Get Token: '+_token);
		getDevice(_token);
		
	}
});
 

//1. Get the device name
function getDevice(token) {
	console.log('1. Get the device name');
	//1. Get the device name
	request.get(deviceUrlBase + '/devices/main?token='+token, {json: true}, function (error, response, body) {
		if (!error && response.statusCode == 200) {
			//Device name
			var device = body.id;
			console.log(device);
			
			console.log('2. Get the package name of the currently running APP on the phone');
			//2. Get the package name of the currently running APP on the phone
			/*
			request.get(deviceUrlBase + '/devices/'+device+'/apps?q=foreground_app', {json: true}, function (error, response, body) {
				if (!error && response.statusCode == 200) {
					console.log(JSON.stringify(body));
					
					//Result:{"status":true,"value":"com.tencent.qqmusic"}
				}
		    });
		    */
		   //Use the above method to get the APP package name com.tencent.qqmusic
		   
		   
		   startApp(device,token);
	  	}
	});
	
}

//3. Start QQ music
function startApp(device, token) {
	var runAppUrl = deviceUrlBase + '/devices/' + device + '/apps/' + runAppName + '?state=active&token='+token;
	//Parameter
	var requestData="";
	console.log('3. Start QQ music');
	//3. Start QQ music
	request.post({
	    url: runAppUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: requestData
	},
	function(error, response, body) {
	    if (!error && response.statusCode == 200) {
	    	//Successful startup
	    	if (body.status == true) {
	    		console.log( device +'Successful startup' + runAppName);
	    		
		   		 //Delay 10 seconds to execute, avoiding QQ music opening ads
	    		setTimeout(function() {
	    			clickInputBox(device, token);
	    		}, 10000);
	    	} else {
	    		//TODO
	    		//Startup failed
	    		console.log('Startup failed');
	    	}
	    }
	});		
	    		
}

//4. Get the input box focus
function clickInputBox(device, token) {
	var clickInputUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
	//Clicked coordinates
	var clickParam = {
		"token":token,
		"x":"613",//0.5321
		"y":"247",//0.1234
		"state":"press"
	};
	console.log('4. Get the input box focus');
	console.log(clickInputUrl+'---'+ JSON.stringify(clickParam));
	
	request.post({
	    url: clickInputUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: clickParam
	},
	function(error, response, body) {
		console.log(body);
	    if (!error && response.statusCode == 200) {
	    	if (body.status == true) {
	    		console.log('Get the input box focus successfully---');
	    		setTimeout(function() {
	    			inputText(device, token);
	    		}, 2000);
	    	} else {
	    		console.log('Failed to get input box focus');
	    	}
	    }
	});
	
}

//5. Input text
function inputText(device, token) {
	var TextUrl = deviceUrlBase + '/devices/' + device + '/screen/texts';
	var textParam = {
		"token":token,
		"text":"my hometown"
	};
	console.log('5. Input text');
	console.log(TextUrl+'---'+JSON.stringify(textParam));
	
	request.post({
	    url: TextUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: textParam
	},
	function(error, response, body) {
	    if (!error && response.statusCode == 200) {
	    	if (body.status == true) {
	    		console.log('Enter text successfully');
	    		setTimeout(function() {
	    			selectSong(device, token);
	    		}, 2000);
	    	
	    	} else {
	    		console.log('Input text failed');
	    	}
	    }
	});
}

//6. Select song
function selectSong(device, token) {
	var selectUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
	//Clicked coordinates
	var selectParam = {
		"token":token,
		"x":"274",//0.2352
		"y":"387",//0.1984
		"state":"press"
	};
	console.log('6. Select song');
	console.log(selectUrl+'---'+ JSON.stringify(selectParam));
	
	request.post({
	    url: selectUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: selectParam
	},
	function(error, response, body) {
	    if (!error && response.statusCode == 200) {
	    	if (body.status == true) {
	    		console.log('Choose song success---');
	    		setTimeout(function() {
	    			sing(device, token);
	    		}, 2000);
	    	} else {
	    		console.log('Choosing a song failed');
	    	}
	    }
	});
}

//7. Play song
function sing(device, token) {
	var clickUrl = deviceUrlBase + '/devices/' + device + '/screen/inputs';
	//Clicked coordinates
	var clickParam = {
		"token":token,
		"x":"111",//0.1024
		"y":"675",//0.4417
		"state":"press"
	};
	console.log('7. Play song');
	console.log(clickUrl+'---'+ JSON.stringify(clickParam));
	
	request.post({
	    url: clickUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: clickParam
	},
	function(error, response, body) {
	    if (!error && response.statusCode == 200) {
	    	if (body.status == true) {
	    		console.log('Play the song successfully---');
	    		console.log('Close the app after two minutes---');
	    		setTimeout(function() {
	    			closeApp(device, token);
	    		}, 120000);
	    		
	    	} else {
	    		console.log('Failed to play song');
	    	}
	    }
	});
}

//8. Close QQ music
function closeApp(device, token) {
	var runAppUrl = deviceUrlBase + '/devices/' + device + '/apps/' + runAppName + '?state=inactive&token='+token;
	//Parameter
	var requestData="";
	
	console.log('8. Close QQ music');
	request.post({
	    url: runAppUrl,
	    method: "POST",
	    json: true,
	    headers: {
	        "content-type": "application/json",
	    },
	    body: requestData
	},
	function(error, response, body) {
	    if (!error && response.statusCode == 200) {
	    	if (body.status == true) {
	    		console.log('Close APP success');
	    	} else {
	    		//TODO
	    		console.log('Failed to launch APP');
	    	}
	    }
	});		
}