Android Bluetooth File Transfer .apk -
intend:
i´m trying develop single-purpose app, can not exit. should run on phone "operating system". phone used further purposes. app not in play store , can´t leave need update otherwise if have newer versions. purpose i´ve written app call "updater-app". want update via bluetooth. prepared , it´s ready file transfer. phones can connect insecurerfcommsocket. managed select .apk file want send chooser on updater-app , i´m converting uri bytes in code:
sendupdatebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (mbtservice != null) { intent intent = new intent(intent.action_get_content); intent.settype("application/vnd.android.package-archive"); intent.addcategory(intent.category_openable); try { startactivityforresult(intent.createchooser(intent, "choose .apk"), file_select_code); } catch (android.content.activitynotfoundexception ex) { toast.maketext(getapplicationcontext(), "no file-explorer found", toast.length_short).show(); } } } }); and onactivityresult:
protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == file_select_code && resultcode == result_ok && data != null) { uri selectedfile = data.getdata(); try { byte[] sendingbyte = readbytes(selectedfile); mbtservice.send(sendingbyte); } catch (ioexception e) { e.printstacktrace(); toast.maketext(getapplicationcontext(), r.string.transmissionfailed, toast.length_short).show(); } } } the readbytes function:
public byte[] readbytes(uri uri) throws ioexception { inputstream inputstream = getcontentresolver().openinputstream(uri); bytearrayoutputstream bytebuffer = new bytearrayoutputstream(); int buffersize = 1024; byte[] buffer = new byte[buffersize]; int len = 0; while ((len = inputstream.read(buffer)) != -1) { bytebuffer.write(buffer, 0, len); } return bytebuffer.tobytearray(); } with mbtservice.send(sendingbyte) row following function called in connectedthread:
public void send(byte[] selectedfile) { try { mmoutstream.write(selectedfile); } catch (ioexception e) { e.printstacktrace(); } } problem:
in receiver phone i´ve got no idea how receive bytes , convert file/uri (no idea yet) .apk , save phone execute button not part of question.
question:
question to in code of receiver phone app manage finish intend? if missed post relevant code i´m sorry , add on request. have program both apps studies i´m thankful help.
i found solution save received bytes bytearray , save file. in bluetooth connectedthread have following run() method:
public void run() { log.i(tag, "begin mconnectedthread"); byte[] buffer = new byte[1024]; int len; bytearrayoutputstream bytebuffer = new bytearrayoutputstream(); try { while ((len = mminstream.read(buffer)) != -1) { bytebuffer.write(buffer, 0, len); mhandler.obtainmessage(constants.message_read, len, -1, bytebuffer.tobytearray()) .sendtotarget(); } } catch (ioexception e) { e.printstacktrace(); connectionlost(); } } and in activity have following handler message_read:
private final handler mhandler = new handler() { @override public void handlemessage(message msg) { switch (msg.what) { case constants.message_state_change: ... break; case constants.message_read: byte[] buffer = (byte[]) msg.obj; try { string filepath = environment.getexternalstoragedirectory() + "/download/app.apk"; file file = new file(filepath); file.getparentfile().mkdirs(); if (file.exists()) { file.delete(); file.createnewfile(); } else file.createnewfile(); bufferedoutputstream objectout = new bufferedoutputstream(new fileoutputstream(file)); objectout.write(buffer); objectout.close(); } catch (exception e) { e.printstacktrace(); } break; } } }; with code .apk saved regular download folder "app.apk".
hope helpful anyone.
Comments
Post a Comment