我正在尝试使用以下代码将文件从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();
我将BLUETOOTH和BLUETOOTH_ADMIN添加到AndroidManifest.xml中的用户权限
但是文件没有传输,连接正在建立黑白设备
答案 0 :(得分: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();
答案 1 :(得分:0)