【问题标题】:Send Files via Bluetooth Socket Based on BluetoothChat Example基于 BluetoothChat 示例通过蓝牙套接字发送文件
【发布时间】:2011-12-23 17:50:50
【问题描述】:

大家好,根据我之前提出的一个问题,我已经能够将文件转换为字节数组,以使用 write 方法:

public void sendFile(){
        Log.d(TAG, "sending data");
        InputStream inputStream = null;

      Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "" +    filesID.get(0));
        Log.d(TAG, "obtained input stream here in recentDevices Activity");
        try {

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

            Log.d(TAG, "sending data to connected thread");
            bluetooth_service.write(byteBuffer.toByteArray());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        finally{
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }


    }

从 BluetoothChatService 示例中,我在连接的线程中有这个写入方法:

   public void write(byte[] out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            r = connectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }


/* Call this from the main Activity to send data to the remote device */
        public void write(byte[] bytes) {

            try {
                mmOutStream.write(bytes); // basically stuck here. nothing happens here
            } catch (IOException e) { }

            mHandler.sendEmptyMessage(RecentDevices.TRANSFER_COMPLETED);

        }

我基本上卡在连接线程的写入方法中,那里似乎没有发生任何事情。在处理程序发送 TRANSFER_COMPLETED 方法后,我放置了一个进度对话框以将其关闭,但程序只是卡在这里。已尝试发送到 pc 或其他 android 设备。没运气!任何人都取得了任何成功或解决方案?

【问题讨论】:

    标签: android


    【解决方案1】:

    你的读取输入流方法呢?

    基于 SO 上的许多帖子,我使用此代码来读取输入流:

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        int bufferSize = 4096;
        byte[] buffer = new byte[bufferSize];
        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                int bytesRead = -1;
                String message = "";
                if (mmInStream.available() > 0) {
                    bytesRead = mmInStream.read(buffer);
                    if (bytesRead > 0) {
                        while ((bytesRead == bufferSize) && (buffer[bufferSize - 1] != 0)) {
                            message = message + new String(buffer, 0, bytesRead);
                            bytesRead = mmInStream.read(buffer);
                        }
                        if((buffer[bytesRead - 1] != 0)) {
                            message = message + new String(buffer, 0, bytesRead); 
                        } else {
                            message = message + new String(buffer, 0, bytesRead - 1);    
                        }
                        mHandler.obtainMessage(ViewContactList.MESSAGE_READ, message.getBytes().length, -1, message).sendToTarget();
                    } 
                }
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost(e.getMessage());
                break;
            }
        }
    }
    

    ConnectedThreadBluetoothChatService 中尝试它。

    【讨论】:

    • 我正在使用 BluetoothChat 示例作为在设备之间传输文件的指南。我并没有尝试实现聊天服务,只是尝试将文件发送到其他设备,所以我不知道我是否真的需要那里的 readInputStream,但我的代码中也有示例中显示的那个。
    • BluetoothChat 应用程序允许两个 Android 设备通过蓝牙执行双向 byteArray。此应用程序建立 RFCOMM 通道/套接字,然后连接到远程设备以启用通过蓝牙传输数据。根据我的理解,侦听 RFCOMM 蓝牙套接字(AcceptThread 中的listenUsingRfcommWithServiceRecord)是接收写入方法中写入的字节所必需的。使用文件传输配置文件 (OBEX FTP) 和对象推送配置文件 (OBEX OPP) 可以实现您想要的解决方案。
    • 会检查这些并告诉你进展如何。