App/handler for Tizen operations Share/Share_multi/Pick

Hello Tizen Developers,

When creating the application on the Gear/Galaxy watches I often encountered the situation when my app needs to send a file to phone or computer via Bluetooth but I can’t do it in some simple way. Instead, had to implement SAP API or use Tizen SDK Bluetooth API to create code to send data by SPP. Well, I have never liked it. It makes project too complex and takes a lot of valuable time.

I have published an app called Blueft (Bluetooth File Transfer). The app helps to transfer files via Bluetooth without any companion app. Blueft is fully standalone app and is free. Blueft registers itself in Tizen system as handlers for operations Share/Share_multi and Pick. You can use this app to share files produced by your app instead creating you own engine. Additionally, you can use the app to call an operation Pick by Tizen control. Operation Pick allows to choose a file from Tizen Filesystem.
Just tell your customers on app’s page about this tool when you app needs to share a file(s) or pick a file from Tizen. Blueft recognizes MIMEs. You can call ‘images/*’ to see only files with extensions common for pictures and others.

Pick up a file(s) by Tizen selector. Examples for Tizen Native and Web APP.
#include <app_control.h>
static
void _tools_file_pick_handler_response(app_control_h request, app_control_h reply, app_control_result_e result, void *user_data)
{
dlog_print(DLOG_DEBUG, LOG_TAG, “file picker response with result %d”, result);

if (result == APP_CONTROL_RESULT_FAILED ) {
	return;
}
if (result == APP_CONTROL_RESULT_CANCELED)
	return;

	
// Expected result APP_CONTROL_RESULT_SUCCEEDED.

bool is_array;
app_control_is_extra_data_array(reply, APP_CONTROL_DATA_SELECTED, &is_array);
if (is_array) {
	int i, count = 0;
	char **paths = NULL; // you can use user_data to store uris
	app_control_get_extra_data_array(reply, APP_CONTROL_DATA_SELECTED, &paths, &count);
	if (paths != NULL) {
		for(i=0; i<count; i++) {
			dlog_print(DLOG_DEBUG, LOG_TAG, "file selected : %s", paths[i]);
			// TODO save file uris
		}
		// paths contains picked files
		
		free(paths);
	} else {
		dlog_print(DLOG_ERROR, LOG_TAG, "file selection failed!");
	}
} else {
	// none files selected
}

}
void _tools_pick_file(app_control_reply_cb cb) {
app_control_h ac;
app_control_create(&ac);
app_control_set_operation(ac, APP_CONTROL_OPERATION_PICK);
app_control_set_uri(ac, “file://”);
app_control_set_mime(ac, “/”); // “/” show all files, “images/*” show only pictures like jpg, png etc
app_control_add_extra_data(ac, APP_CONTROL_DATA_SELECTION_MODE, “single”);
app_control_send_launch_request(ac, cb, NULL); // you can use last parameter to pass your paths array
app_control_destroy(ac);
}

// Request Tizen selector to handle APP_CONTROL_OPERATION_PICK.
_tools_pick_file(_tools_file_pick_handler_response);

// Example for Web App.

// helping members
var RESULT_SUCCESS = “succ”;
var RESULT_ERROR = “error”;

function tools_pick_file(callback) {

var uri  = null;
var mime = "*/*"; // "image/*", "application/*" etc
var category = null;
var applicationControlData = [ new tizen.ApplicationControlData("http://tizen.org/appcontrol/data/selection_mode", ["single"]) ];

var appControl = new tizen.ApplicationControl(
		"http://tizen.org/appcontrol/operation/pick", // APP_CONTROL_OPERATION_PICK
		uri, 
		mime, 
		category, 
		applicationControlData); 

var appControlReplyCallback = 
{
	onsuccess: function(data) 
	{
		console.info('selected files size ' + data.length, data);			
		for (var i = 0; i < data.length; i++) 
		{
			console.log('>>> idx ' + i + ", key " + data[i].key + ", value " + data[i].value[0]);
			if (data[i].key == APP_CONTROL_DATA_PATH) 
			{
				console.log('Selected file: ' + data[i].value[0]);
				if(callback)
					callback(_RESULT_SUCCESS_, data[i].value[0]);
				break; // exit for one file
			}
		}			
	},
	onfailure: function(e) {
		onError(e);
	}
}

try {
	tizen.application.launchAppControl(
			appControl, 
			null, // "tizen.filemanager"
			function() {
				console.info("launch application control SUCC!");
			},
			function(e) {
				console.info('launch application control FAIL! error msg ' + e.message);
				onError(e);
			}, 
			appControlReplyCallback
	);
} catch(e) {
	console.info('tizen.application.launchAppControl() trows EXCEPTION! msg ' + e.message);
	onError(e);
}

function onError(e) {
	if(callback)
		callback(_RESULT_ERROR_, e);
}

};

Share and multi_share a file(s) by Tizen selector. Examples for Web APP and Tizen Native.
required in config.xml privilege "http://tizen.org/privilege/application.launch"

function _tools_share_file(fileUri, callback){

console.info('[_tools_share_file] uri: ', fileUri);

var appControl         = null;
var file               = null;
var operation          = null;
var uri                = null;
var mime               = null;		
var category           = null;
var appControlData     = null; // ApplicationControlData[] (key/value) An array of attributes to store the data needed for an application control.	
		
operation  = "http://tizen.org/appcontrol/operation/share"; // APP_CONTROL_OPERATION_MULTI_SHARE
appControl = new tizen.ApplicationControl(operation, fileUri, mime, category, appControlData);

try {
	tizen.application.launchAppControl(appControl, null, succ, fail, null);
} catch(e) {
	fail(e);
}

function succ() {
	alert('share succ!')
	if (callback)
		callback(_RESULT_SUCCESS_);
}
function fail(e) {
	alert('share error ' + e.message + ", name: " + e.name); // usually returns NotFoundError
	if (callback)
		callback(_RESULT_ERROR_, e);

}

}

_tools_share_file(‘file:///opt/usr/media/Images/Twinky_foto.jpg’, function(result) { alert(result) });

function _tools_multishare_file(urisArray, callback){

var appControlData = [ new tizen.ApplicationControlData( "http://tizen.org/appcontrol/data/path", urisArray) ];

var uri 		= "file://";
var mime 		= "*/*"; // or null
var category 	= null;
var appControl = new tizen.ApplicationControl("http://tizen.org/appcontrol/operation/multi_share",
											uri, 
											mime, 
											category,
											appControlData);

tizen.application.launchAppControl(appControl, null, function() {
	console.log('launch application control succeed');
	if (callback)
		callback(_RESULT_SUCCESS_);
}, function(e) {
	console.log('launch application control failed. reason: ' + e.message);
	if (callback)
		callback(_RESULT_ERROR_);
}, null);

}
_tools_multishare_file([“path1”, “path2”, …], null);

required in tizen-manifest: http://tizen.org/privilege/appmanager.launch

#include <app_control.h>
// Share one file.
void _tools_share_file(char * fileURI) {
app_control_h service;
app_control_create(&service);
app_control_set_operation(service, APP_CONTROL_OPERATION_SHARE);
app_control_set_mime(service, “/”);
app_control_set_uri(service, fileURI);
app_control_send_launch_request(service, NULL, NULL);
app_control_destroy(service);
}
_tools_share_file( “file:///opt/usr/media/Images/Twinky_foto.jpg” );

include <app_control.h>

// Share more files at once.
void _tools_multi_share_file(char * files, int count) {
app_control_h service;
app_control_create(&service);
app_control_set_operation(service, APP_CONTROL_OPERATION_MULTI_SHARE);
app_control_set_mime(service, “/”);
app_control_add_extra_data_array(service, APP_CONTROL_DATA_PATH, files, count);
app_control_set_launch_mode(service, APP_CONTROL_LAUNCH_MODE_GROUP);
app_control_send_launch_request(service, NULL, NULL);
app_control_destroy(service);
}

char* files = {“file:///opt/usr/media/Images/Twinky_foto.jpg”, “file:///opt/usr/media/Images/BeepBeep.mp3”};
_tools_multi_share_file( files, 2 ); // share 2 files

Blueft is FREE and published on Samsung Galaxy Apps store.

Thanks Ron for permission to share this news and tech guide.

3 Likes

Thank you for sharing.

You are always welcome :grinning:

1 Like