Android蓝牙文件传输.apk
意愿:Android蓝牙文件传输.apk,android,file,bluetooth,apk,Android,File,Bluetooth,Apk,意愿: 我正在尝试开发一个单一用途的应用程序,你不能退出。它应该作为“操作系统”在手机上运行。此电话将用于其他用途。该应用程序将不在play store中,您也不能离开它,因此如果我有新版本,我需要更新它。为此,我编写了另一个应用程序,你可以称之为“更新程序应用程序”。我想通过蓝牙进行此更新。我已经准备好了一切,可以进行文件传输了。手机可以通过不安全的通讯插座连接。我还设法选择了.apk文件,我想在updater应用程序上使用选择器发送该文件,我正在将uri转换为以下代码中的字节: sendUp
我正在尝试开发一个单一用途的应用程序,你不能退出。它应该作为“操作系统”在手机上运行。此电话将用于其他用途。该应用程序将不在play store中,您也不能离开它,因此如果我有新版本,我需要更新它。为此,我编写了另一个应用程序,你可以称之为“更新程序应用程序”。我想通过蓝牙进行此更新。我已经准备好了一切,可以进行文件传输了。手机可以通过不安全的通讯插座连接。我还设法选择了.apk文件,我想在updater应用程序上使用选择器发送该文件,我正在将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并将其保存到我的电话中,用另一个按钮执行它,这不是问题的一部分 问题:
因此,我的问题是,在接收器手机应用程序的代码中,要管理什么来完成我的计划?如果我没有发布任何相关代码,我很抱歉,并将在您的请求中添加它。由于我必须为我的研究编写这两个应用程序,我非常感谢您的帮助。我找到了一个解决方案,将收到的字节保存到字节数组,然后保存到文件中。在Bluetooth ConnectedThread中,我有以下run()方法: 在我的活动中,我看到以下处理程序消息:
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;
}
}
};
使用此代码,.apk将作为“app.apk”保存到常规下载文件夹中
希望这对任何人都有帮助。我找到了一个解决方案,将收到的字节保存到字节数组,然后保存到文件中。在Bluetooth ConnectedThread中,我有以下run()方法: 在我的活动中,我看到以下处理程序消息:
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;
}
}
};
使用此代码,.apk将作为“app.apk”保存到常规下载文件夹中
希望这对任何人都有帮助