【发布时间】:2017-09-12 14:52:27
【问题描述】:
意图:
我正在尝试开发一个您无法退出的单一用途应用程序。它应该作为“操作系统”在手机上运行。该电话将用于其他目的。该应用程序不会出现在 Play 商店中,您也不能离开它,所以如果我有更新版本,我需要更新它。为此,我编写了另一个应用程序,您可以将其称为“updater-app”。我想通过蓝牙进行此更新。我已经准备好了一切,它已经准备好进行文件传输了。电话可以通过 InsecureRfcommSocket 连接。我还设法选择了要在更新程序应用程序上使用选择器发送的 .apk 文件,并且我正在将 uri 转换为此代码中的字节:
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();
}
}
}
});
和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();
}
}
}
readBytes 函数:
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();
}
mBtService.send(sendingByte) 行在 ConnectedThread 中调用以下函数:
public void send(byte[] selectedFile) {
try {
mmOutStream.write(selectedFile);
} catch (IOException e) {
e.printStackTrace();
}
}
问题:
在接收器电话中,我不知道如何接收字节并将其转换回文件/uri(还不知道).apk 并将其保存到我的手机以使用另一个不属于此问题的按钮执行它.
问题:
所以我的问题是在接收器电话应用程序的代码中要管理什么来完成我的意图?如果我错过了发布任何相关代码,我很抱歉,并将其添加到您的请求中。因为我必须为我的学习编写这两个应用程序,所以我很感谢你的帮助。
【问题讨论】:
标签: android file bluetooth apk