Android学习之Bluetooth开发总结<续2>

public void run() {
    while (true) {
        if (socket != null) { 
            try {                                
                int read = -1;
                byte buffer[] = new byte[1024];
                InputStream inputStream = BluetoothService.socket.getInputStream(); 
                for (; (read = inputStream.read(bytes)) > -1;) {  
                    final int count = read; 
                    synchronized (BluetoothService.receiveData) {
                        for (int i = 0; i < count; i++){
                            BluetoothService.receiveData.add(buffer[i]);
                        }
                    }
                }                
            } catch (Exception e) {
                BluetoothService.connected = false;
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2013-07-18 18:35  Shuaiwen8023  阅读(718)  评论(0编辑  收藏  举报