⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpsreader.java~41~

📁 一个用于手机蓝牙通讯的程序
💻 JAVA~41~
字号:
package gps;

import gps.datatypes.Record;
import gps.parser.Parser;

import java.io.IOException;
import java.io.InputStreamReader;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class GPSReader implements Runnable {

	private String url = "";
	private Record record = null;
	private boolean isRunning = false;


	private GPSRecord gpsRecorder = null;

	private StreamConnection connection;
	private InputStreamReader reader;

	Thread runner;
	private boolean flag = false;
	/*
	 * The url to the bluetooth device is the String parameter.
	 */
	public void Init() {
          gpsRecorder = new GPSRecord();
	}
	public GPSReader(String u) {
		Init();
		url = u; // Connection string to the bluetooth device.
	}
	 // Starts receving of data (if not yet started).
	public void start() {
		isRunning = true;
		runner = new Thread(this);
		runner.start();
	}
	private synchronized void connect() throws IOException {
		connection = (StreamConnection) Connector.open(url, Connector.READ);
		reader = new InputStreamReader(connection.openInputStream());
	}

	private synchronized void disconnect() {
		try {
			if (reader != null)
				reader.close();
			if (connection != null)
				connection.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
		reader = null;
		connection = null;
	}

	public synchronized void setRunning(boolean Running) {
		this.isRunning = Running;
	}

	public synchronized boolean isConnected() {
		return connection != null && reader != null;
	}

	public synchronized boolean getRunning() {
		return isRunning;
	}

	/**
	 * 主函数,与GPS建立连接
	 */
	public synchronized void run() {
          Record rec = new Record();
          String s = "";
          while (isRunning) {
            try{
              while (!isConnected()) {
                GPSCanvas.s = "notConnected!";
                connect();
              }
              int i = 0;
              char c;
              // Start reading the data from the GPS
              while ( (i = reader.read()) != 13) {
                // Every sentence starts
                s += (char) i;
              }
              //s="$GPGGA,000050.996,0010.0000,N,00000.0000,E,1,05,50.0,0.0,M,0.0,M,0.0,0000*7D";
              s = s.substring(1, s.length());
              Thread.sleep(500);
              if (Parser.parse(s, rec)) {
                GPSRecord.putRecord(rec);
                Thread.sleep(100);
                System.gc();
              }
              s = "";
            }
            catch (Exception e) {
              System.out.println(e);
	    }
	 }
       }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -