【问题标题】:Send file to other Bluetooth device from android从 android 发送文件到其他蓝牙设备
【发布时间】:2012-02-29 14:41:07
【问题描述】:

我正在尝试使用以下代码将文件从 android 发送到另一台设备

        socket = device.createRfcommSocketToServiceRecord(uuid);
        socket.connect();
        OutputStream os = socket.getOutputStream();
        File f = new File(strPath);
        byte [] buffer = new byte[(int)f.length()];
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(buffer,0,buffer.length);
        os.write(buffer,0,buffer.length);
        os.flush();
        os.close();
        socket.close();

我在 AndroidManifest.xml 中为用户权限添加了 BLUETOOTH 和 BLUETOOTH_ADMIN

但文件没有传输,连接正在建立黑白设备

【问题讨论】:

  • 您能否发布您的完整源代码,以便其他人容易理解。谢谢。

标签: android bluetooth


【解决方案1】:

毗湿奴请到以下链接

这是关于你的问题,我想你可能会从 thisthisthis 那里得到帮助。

【讨论】:

  • 抱歉,这些链接对我没有帮助:-(
  • Igot Exception 说 02-08 12:55:37.659: W/System.err(20167): java.io.IOException: Service discovery failed at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket .java:410) 在 com.myapp.bts.BTSActivity.onClick(BTSActivity.java:83) 在 android.view.View.performClick(View.java: 2485) 在 android.os.Handler.handleCallback(Handler.java:587) 在 android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:123)
  • 这意味着您无法连接到其他设备。你确定你的uuid和mac地址是正确的吗?我也将 socket.connect 放在 for 循环中尝试 3 次。
【解决方案2】:

我不知道为什么你的方法不起作用,如果有人知道答案,请发布我想知道的。但下面是我的工作方式,我基本上是以 1024 字节的块发送文件。

/*Transmit*/
private OutputStream mOut;
byte[] mBuffer = byte[1024]
mBtSocket = _socket;
mOut = mBtSocket.getOutputStream();
InputStream inFile = new FileInputStream(file);
while((mLen = inFile.read(mBuffer, 0, 1024)) > 0){
         mOut.write(mBuffer, 0, mLen);
}

/*Receive*/
private InputStream mIn;
byte[] mBuffer = byte[1024]
File file = new File(fileName);
OutputStream outFile = new FileOutputStream(file);
long bytesReceived = 0;
while (bytesReceived < fileSize) {  // I send fileSize as msg prior to this file transmit
    mLen = mIn.read(mBuffer);
if(mLen > 0) {
    bytesReceived+=mLen;
    outFile.write(mBuffer, 0, mLen);
} else {
    Log.d(TAG,"Read received -1, breaking");
    break;
}
}
outFile.close();

【讨论】:

  • 感谢重播。是否可以通过传输方法将文件发送到其他手机(不是安卓手机)?
  • 可以,只要你们俩都在做 rfcomm
  • 您能否发布您的完整源代码,以便其他人容易理解。谢谢。
  • 如果用户发送超过1个文件,如何重置InputStream从零调用read(byte[])? (蓝牙插座的InputStream)。
猜你喜欢
  • 2013-08-31
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
相关资源
最近更新 更多