【发布时间】:2017-03-23 18:01:15
【问题描述】:
我编写了一个在 BluetoothSocket 中写入的方法,但它并没有写入所有内容。
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
【问题讨论】:
-
你怎么知道不是所有的东西都是写的?此外,您应该在 finally 块中关闭事物,这样就不会在出现异常时泄漏流或套接字。
-
@Gray 因为我在 Socket 中得到了信息。
-
抛出了什么异常?
标签: java android sockets bluetooth bufferedwriter