码迷,mamicode.com
首页 > 编程语言 > 详细

蓝牙连接后,启动子线程接收数据,主线程卡住了

时间:2014-10-18 23:54:14      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   java   sp   数据   on   art   问题   

============问题描述============


我看了下google的bluetoothchat的demo,它那里把蓝牙建立客户端连接的部分也放在子线程里执行的。

我目前的程序,蓝牙建立连接的步骤是在UI线程里,但是bluetoothSocket接收数据的部分是在子线程中的,但是现在碰到一个问题,开启子线程后,主线程不接着往下执行了。

根据我的日志记录,connectedThread.run();之后的日志方法LogHelper.Write("已经运行过子线程");就一直卡着,不会执行了。
应该来说子线程run了之后,主线程不是应该继续往下执行的吗?

代码如下
主线程类,是界面一个按钮的单击事件中触发这个Start()方法

public void Start()

{

	LogHelper.Write("开始连接bluetooth device");

	BluetoothAdapter bluetoothAdapter =  BluetoothAdapter.getDefaultAdapter();		

	BluetoothDevice device = bluetoothAdapter.getRemoteDevice(this.deviceAddress);		

	UUID deviceUUId=UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

	try

	{

		this.clientSocket=device.createRfcommSocketToServiceRecord(deviceUUId);

		this.clientSocket.connect();

		LogHelper.Write("连接成功");

		this.connectedThread=new ConnectedThread(clientSocket);

		byte[] buffer=new byte[]{0x3a,0x50,0x00,(byte) 0x8a};

		connectedThread.write(buffer);

		LogHelper.Write("已写入开始命令");

		connectedThread.run();

		LogHelper.Write("已经运行过子线程");

	}

	catch(IOException e)

	{

		LogHelper.Write(e.getMessage());

	}

}



子线程类

private class ConnectedThread extends Thread

	{

		private final BluetoothSocket mmSocket;

        private final InputStream mmInStream;

        private final OutputStream mmOutStream;



        public ConnectedThread(BluetoothSocket socket) 

        {           

            mmSocket = socket;

            InputStream tmpIn = null;

            OutputStream tmpOut = null;           

            try 

            {

                tmpIn = socket.getInputStream();

                tmpOut = socket.getOutputStream();

            } 

            catch (IOException e) 

            {

            	LogHelper.Write(e.getMessage());

            }

            mmInStream = tmpIn;

            mmOutStream = tmpOut;

        }



        public void run() 

        {            

            byte[] buffer = new byte[1024];

            int bytes;

            while (true) 

            {

                try 

                {                    

                    bytes = mmInStream.read(buffer);                    

                    //处理数据

                    writeToBuffer(buffer,seBuffer,readPointer,writePointer,bytes);

                    readBuffer(seBuffer,readPointer,writePointer);

                } 

                catch (IOException e) 

                {  

                	LogHelper.Write(e.getMessage());

                	break;

                }

            }

        }



        //往蓝牙通讯管道中写入数据

        public void write(byte[] buffer) 

        {

            try 

            {

                mmOutStream.write(buffer);

            } 

            catch (IOException e) 

            {}

        }

}

============解决方案1============



connectedThread.run();


应该写成

connectedThread.start();


run()只有在子线程结束时才会返回,而你的子线程逻辑是个while循环,那么Start当然阻塞了。

蓝牙连接后,启动子线程接收数据,主线程卡住了

标签:io   os   ar   java   sp   数据   on   art   问题   

原文地址:http://www.cnblogs.com/meizhenfen42/p/4033883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!