java– 蓝牙传输App在使用InputStream.read()后没有错误就停止了

java– 蓝牙传输App在使用InputStream.read()后没有错误就停止了,第1张

概述我正在尝试使用以下来源制作文件传输蓝牙应用程序:http://developer.android.com/guideopics/connectivity/bluetooth.htmlhttps://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/当我尝试使用InputStream

我正在尝试使用以下来源制作文件传输蓝牙应用程序:

http://developer.android.com/guide/topics/connectivity/bluetooth.html

https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/

当我尝试使用inputStream.read()方法以这种方式获取inputStream字节时:

public class ConnectedThread extends Thread {...(some code here)public voID run(){        byte[] buffer = new byte[1024];        int bytes = -1;        //Keep Listening to the inputStream while connected        while (true){            try {                bytes = this.mmInStream.read(buffer);                //* this part is not reached                if (bytes==-1){                    Log.d("NoData:","-1");                  }            }            catch(Exception e){                Log.d("inStream exception:",e.getMessage());                break;            }        }    }...(some code here)}

永远不会到达代码的下一部分(在这种情况下为“if”部分),也不会出现Log.D调试输出或我放入的其他任何内容.我刚从LogCat收到此消息:

BluetoothSocket read in: androID.net.LocalStocketImpl$SocketinputStream@f7e                b08 len: 1024

要将数据从客户端传输到服务器,我这样做:

public class MainActivity extends Activity {...(some code here)@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    clIEntConnect();    //serverConnect();}...(some code here)public voID clIEntConnect(){        Set<BluetoothDevice> devices;        devices = bConfig.getPairedDevices();         if (devices == null){                               return;        }        if (devices.size() > 0) {                       BluetoothDevice device = devices.iterator().next();            ConnectThread connectTransmit = new ConnectThread(device,bConfig.getBluetoothAdapter(),BluetoothConfig.mUUID);            connectTransmit.start();            Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();            socket = connectTransmit.mmSocket;            ConnectedThread connectedThread = new ConnectedThread(socket);            //write file bytes to the connected thread, so the thread can receive its own input written bytes later            file file_to_transfer = new file(Environment.getExternalStorageDirectory() + "/txtTransfer.txt");                       //get bytes from our file            int size = (int) file_to_transfer.length();            byte[] bytes = new byte[size];            try {                //14b are read succesfully, the whole text file                 BufferedinputStream buf = new BufferedinputStream(new fileinputStream(file_to_transfer));                buf.read(bytes,0,bytes.length);                buf.close();                            }catch (fileNotFoundException e){                Log.d("fileNotFoundException:",e.getMessage());            }catch (IOException e){                 Log.d("IOException:",e.getMessage());            }            //send the data to the server            connectedThread.start();            connectedThread.write(bytes);            //connectedThread.cancel();        }    }...(some code here)}

AcceptThread(实现的服务器部分)工作,因为当我运行客户端部分连接然后传输数据时,在设备中进行调试时,服务器部分上的LogCat激活并到达线程的run方法,我调用ConnectedThread实现,但之后“显然”读取字节但它在LogCat上卡住而没有错误.

请让我知道如何完成读取字节以移动到流程的下一部分.

谢谢

解决方法:

您被阻止等待更多输入.

在流结束测试之后,标记为…的部分(这里的一些代码)应该在读取循环内.注意如果read()返回-1并不表示“没有数据”,则表示流结束,您应该关闭套接字并中断读取循环.否则,您应该继续处理您刚读过的数据.目前你只是读取并忽略所有输入,直到流结束,这是没有意义的.充其量只能处理最后一个部分缓冲区,你不知道它有多长.

总结

以上是内存溢出为你收集整理的java – 蓝牙传输App在使用InputStream.read()后没有错误就停止了全部内容,希望文章能够帮你解决java – 蓝牙传输App在使用InputStream.read()后没有错误就停止了所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1114834.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存