我写了一个写入BluetoothSocket buit的方法,它没有写出所有东西。
public static String BluetoothPrint(String cFichero, String cMAC){
String cFail = "Fail";
try{
BluetoothAdapter oBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice oDispositivo = oBluetoothAdapter.getRemoteDevice(cMAC);
cFail = "Fail socket";
Method oMethod = oDispositivo.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
BluetoothSocket oSocket = (BluetoothSocket) oMethod.invoke(oDispositivo, Integer.valueOf(1));
oSocket.connect();
cFail = "Fail getOutput";
BufferedWriter oStream = new BufferedWriter(new OutputStreamWriter(oSocket.getOutputStream(), "ISO-8859-1"));
cFail = "Fail write";
oStream.flush();
oStream.write(cFichero,0,cFichero.length());
oStream.close();
oSocket.close()
}catch(Exception e){return cFail;}
return "ok";}
此方法有任何大小限制吗?我尝试写的文件大小为5,37KB
答案 0 :(得分:0)
我终于找到了答案。问题是socket' oSocket'的缓冲区。 我重写了我的方法,以块的形式发送数据并等待。
public static String BluetoothPrint(String cFichero, String cMAC)
{
String cFail = "Fail";
byte oByte;
byte[] msg = cFichero.getBytes();
try{
BluetoothAdapter oBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice oDispositivo = oBluetoothAdapter.getRemoteDevice(cMAC);
cFail = "Fail socket";
Method oMethod = oDispositivo.getClass().getMethod("createRfcommSocket",new Class[] { int.class });
BluetoothSocket oSocket = (BluetoothSocket) oMethod.invoke(oDispositivo, Integer.valueOf(1));
oSocket.connect();
cFail = "Fail getOutput";
BufferedOutputStream(oSocket.getOutputStream());
BufferedOutputStream btoutputstream = new BufferedOutputStream(oSocket.getOutputStream(),512);
cFail = "Fail write";
int off = 0;
while(off < msg.length){
btoutputstream.write(msg,off,64);
btoutputstream.flush();
Thread.sleep(600);
off += 64;
}
btoutputstream.close();
oSocket.close();
}catch(Exception e){return cFail;}
return "ok";
}