i trying send plain text via bluetooth, converted html somewhere.
the code i'm using this:
string content = "this test"; intent sendintent = new intent(intent.action_send); sendintent.putextra(intent.extra_text, content); sendintent.settype("text/plain"); string title = "share with…"; startactivity(intent.createchooser(sendintent, title));
when run code , choose bluetooth option, file pushed remote system name "bluetooth_content_share.html" , these contents:
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/></head><body>this test</body></html>
i've tried calling settype
before adding extra_text
no change in symptoms. other share actions ("add dropbox", example) data plain text. , have been able use other applications ("es file explorer", example) send plain text files via bluetooth.
how can data sent plain text asked?
i couldn't sleep decided take @ stackoverflow see if there interesting in android tag. question seemed simple enough, yet proved interesting since noted in question creates damn html file string content.
i assumed bluetooth communication wanted work files , android inferring our text html though stated plain text.
the solution came forcing app share text file opossed sharing test string
. i've been able test , code too, , able replicate magical creation of html file. should you.
update due op concerns leaving file in storage, , not being able use temporary file, i've updated code add fileobserver
file allows monitor when file being modified , type of action it's experiencing. in case, need monitor fileobserver.close_nowrite
action triggered when file being accessed send it, , after has finished working on it. eliminating file after it.
try { //create file , write string bufferedwriter out; final string filepath = environment.getexternalstoragedirectory().getpath() + "/wadus.txt"; filewriter filewriter = new filewriter(filepath); out = new bufferedwriter(filewriter); out.write("i know you'll love me finding solution"); out.close(); //access file , share through original intent file file = new file(filepath); intent sendintent = new intent(intent.action_send); sendintent.putextra(intent.extra_stream, uri.fromfile(file)); sendintent.settype("text/plain"); string title = "share with…"; //create file observer monitor access file fileobserver fobsv = new fileobserver(filepath) { @override public void onevent(int event, string path) { if (event == fileobserver.close_nowrite) { //the file written to, it's been sent , closed //we can safely delete it. file file = new file(filepath); file.delete(); } } }; fobsv.startwatching(); //launch sharing intent startactivity(intent.createchooser(sendintent, title)); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }
if wondering why setting fileobserver
far down code, avoid being triggered upon creation , edition of file itself. since added after file has been written to, triggering events required sending bluetooth (in case).