【问题标题】:Android Bluetooth File Transfer .apk安卓蓝牙文件传输.apk
【发布时间】: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


    【解决方案1】:

    我找到了将接收到的字节保存到 ByteArray,然后将其保存到文件的解决方案。在我的 Bluetooth ConnectedThread 中,我有以下 run() 方法:

    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();
            }
        }
    

    在我的活动中,我有以下处理程序 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;
            }
        }
    };
    

    使用此代码,.apk 将作为“app.apk”保存到常规下载文件夹中。

    希望这对任何人都有帮助。

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      相关资源
      最近更新 更多