wifi控制链接到一个模块进行数据发送

圣剑 发布于 2013/01/30 11:29
阅读 2K+
收藏 1
视频输出的功能已经解决了,但是发送数据却有很大问题,我发送数据时只能发送final的数据,而不能对变量封装,请问这是怎么回事?先上代码:(如果吧mPrintWriterClient.print("f".getBytes());这个方法的参数改成一个变量就不能发送了
package wifivideo.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;

import wifivideo.surface.AceSurface;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener
{

	Button button0;
	Button buttonff;
	SeekBar seekBar;
	TextView textView;
	AceSurface aceSurface;

	Bitmap bmp;
	
	private boolean isConnecting = false;				//判断是否已经断开连接,如果断开了就不再发送数据
	private Thread mThreadClient = null;
	private Socket mSocketClient = null;
	static PrintWriter mPrintWriterClient = null;
	static BufferedReader mBufferedReaderClient = null;

	public static String CameraIp;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		init();

		CameraIp = "http://192.168.1.1:8080/?action=snapshot"; 	// 因为这是是测试,所以直接可以使用所知道的IP地址8080作为视频端口
		aceSurface.GetCameraIP(CameraIp);

		mThreadClient = new Thread(mRunnable);
		mThreadClient.start();

		button0.setOnTouchListener(new View.OnTouchListener()
		{
			public boolean onTouch(View v, MotionEvent event)
			{
				int action = event.getAction();

				switch (action)
				{
				case MotionEvent.ACTION_DOWN:
					mPrintWriterClient.print("0".getBytes());
					mPrintWriterClient.flush();
					break;
				}
				return false;
			}
		});

		buttonff.setOnTouchListener(new OnTouchListener()
		{

			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				// TODO Auto-generated method stub
				int action = event.getAction();
				switch (action)
				{
				case MotionEvent.ACTION_DOWN:
					// Toast.makeText(MainActivity.this,
					// "DOWNff", 1000).show();
					mPrintWriterClient.print("1".getBytes());
					mPrintWriterClient.flush();
					break;
				// case MotionEvent.ACTION_UP:
				// Toast.makeText(MainActivity.this, "UPff",
				// 1000).show();
				// mPrintWriterClient.print(0xf);
				// mPrintWriterClient.flush();
				}
				return false;
			}
		});
	}

	private Runnable mRunnable = new Runnable()
	{
		public void run()
		{
			String msgText = "192.168.1.1:2001";

			int start = msgText.indexOf(":"); // 找到端口号的标示符,控制端口号为2001
			String sIP = msgText.substring(0, start); // 得到链接的ip地址

			String sPort = msgText.substring(start + 1); // 找到端口号2001
			int port = Integer.parseInt(sPort);

			try
			{
				// 连接服务器
				mSocketClient = new Socket(sIP, port);
				// 取得输入输出流
				mBufferedReaderClient = new BufferedReader(new InputStreamReader(mSocketClient.getInputStream()));
				mPrintWriterClient = new PrintWriter(mSocketClient.getOutputStream(), true);
				Message msg = new Message();
				msg.what = 1;
				mHandler.sendMessage(msg);
				// break;
			}
			catch (Exception e)
			{
				Message msg = new Message();
				msg.what = 1;
				mHandler.sendMessage(msg);
				return;
			}

			char[] buffer = new char[256];

			while (isConnecting)
			{
				try
				{
					// if ( (recvMessageClient =
					// mBufferedReaderClient.readLine()) !=
					// null )
					if ((mBufferedReaderClient.read(buffer)) > 0)
					{
						Message msg = new Message();
						msg.what = 1;
						mHandler.sendMessage(msg);
					}
				}
				catch (Exception e)
				{
					Message msg = new Message();
					msg.what = 1;
					mHandler.sendMessage(msg);
				}
			}
		}
	};

	Handler mHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
		}
	};

	public void onDestroy()
	{
		super.onDestroy();
		if (isConnecting)
		{
			isConnecting = false;
			try
			{
				if (mSocketClient != null)
				{
					mSocketClient.close();
					mSocketClient = null;

					mPrintWriterClient.close();
					mPrintWriterClient = null;
				}
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			mThreadClient.interrupt();
		}
	}

	private void init()
	{
		// TODO Auto-generated method stub
		button0 = (Button) MainActivity.this.findViewById(R.id.button1);
		buttonff = (Button) MainActivity.this.findViewById(R.id.button2);
		aceSurface = (AceSurface) MainActivity.this.findViewById(R.id.aceSurfaceView);
		seekBar = (SeekBar) MainActivity.this.findViewById(R.id.seekBar1);
		textView = (TextView) MainActivity.this.findViewById(R.id.textView1);
		seekBar.setOnSeekBarChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
	{
		// TODO Auto-generated method stub
		MainActivity.this.textView.setText("当前值:" + progress);
		// byte[] bs = "ff".getBytes();

		seekBar.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event)
			{
				// TODO Auto-generated method stub
				int action = event.getAction();
				switch (action)
				{
				case MotionEvent.ACTION_MOVE:
					mPrintWriterClient.print("f".getBytes());
					mPrintWriterClient.flush();
					break;
				}
				return false;
			}
		});
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar)
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar)
	{
		// TODO Auto-generated method stub
	}
}

以下是问题补充:

@圣剑:QQ359019061 (2013/01/30 11:33)
加载中
返回顶部
顶部