1

我在内部存储中创建了一个文本文件,我想通过蓝牙与配对设备共享该文件。

我怎样才能做到这一点?我尝试了以下代码,但无法正常工作

private void WriteFile(String text, Context context, String deviceAddress)
    {
        try {

            FileOutputStream fos = context.openFileOutput("command.txt", Context.MODE_WORLD_WRITEABLE);
            fos.write(text.getBytes());
            fos.flush();
            fos.close();

            File file = new File(context.getFilesDir(), "command.txt");
            if (file.exists())
               Toast.makeText(context, "command file created successfully", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(context, "command file not created.", Toast.LENGTH_LONG).show();

            ContentValues values = new ContentValues();
            values.put(BluetoothShare.URI, "content://" + Uri.fromFile(file));
            values.put(BluetoothShare.DESTINATION, deviceAddress);
            values.put(BluetoothShare.DIRECTION, BluetoothShare.DIRECTION_OUTBOUND);
            Long ts = System.currentTimeMillis();
            values.put(BluetoothShare.TIMESTAMP, ts);
            context.getContentResolver().insert(BluetoothShare.CONTENT_URI, values);


            Toast.makeText(context, "Command sent successfully", Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我收到文件未知,文件未发送错误。

我该怎么办?

任何帮助表示赞赏。

谢谢

4

0 回答 0