藍牙通信模塊

package com.example.b2;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;


public class BluetoothService {
	public static final int INFO = 1;


	static final String NAME = "BluetoothChat";
	static final UUID MY_UUID = UUID
			.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");


	private final BluetoothAdapter mAdapter;
	private final Handler mHandler;


	DataTransferThread dataThread = null;
	ConnectThread connThread = null;
	ServerThread serThread = null;


	public BluetoothService(Handler handler) {
		mHandler = handler;
		mAdapter = BluetoothAdapter.getDefaultAdapter();
	}


	public synchronized void startServer() {
		serThread = new ServerThread();
		serThread.start();


	}


	public synchronized void connect(BluetoothDevice device) {
		connThread = new ConnectThread(device);
		connThread.start();
	}


	public synchronized void connected(BluetoothSocket socket) {
		dataThread = new DataTransferThread(socket);
		dataThread.start();


	}


	public void write(byte[] out) {
		if (dataThread != null)
			dataThread.write(out);
	}


	// 服務端線程
	private class ServerThread extends Thread {
		// 服務端套接字
		private final BluetoothServerSocket mmServerSocket;


		public ServerThread() {
			BluetoothServerSocket tmp = null;
			try {
				tmp = mAdapter
						.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
			} catch (IOException e) {
			}
			mmServerSocket = tmp;
		}


		@Override
		public void run() {
			// 服務端端套接字
			BluetoothSocket socket = null;
			do {
				try {
					// 監聽到一個連接
					socket = mmServerSocket.accept();
				} catch (IOException e) {
					mHandler.obtainMessage(INFO, 1, 1, "監聽連接失敗,繼續監聽...".getBytes())
							.sendToTarget();
				}
			} while (socket == null);
			// 連接設備
			connected(socket);
		}


		public void cancel() {
			try {
				mmServerSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


	}


	// 客戶端線程
	private class ConnectThread extends Thread {
		private final BluetoothSocket mmSocket;
		private final BluetoothDevice mmDevice;


		public ConnectThread(BluetoothDevice device) {
			mmDevice = device;
			BluetoothSocket tmp = null;
			try {
				tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
			} catch (IOException e) {
			}
			mmSocket = tmp;
		}


		public void run() {
			// 停止掃描設備
			mAdapter.cancelDiscovery();
			try {
				mmSocket.connect();
			} catch (IOException e) {
				mHandler.obtainMessage(INFO, 1, 1, "嘗試連接失敗,請再試一次!".getBytes())
						.sendToTarget();
				try {
					mmSocket.close();
				} catch (IOException e1) {
				}
			}
			// 成功連接,開始交換數據
			connected(mmSocket);


		}


		public void cancel() {
			try {
				mmSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	// 交換數據線程
	private class DataTransferThread extends Thread {
		private final BluetoothSocket socket;
		private final InputStream in;
		private final OutputStream out;


		// 構造方法
		public DataTransferThread(BluetoothSocket socket) {
			this.socket = socket;
			InputStream tmpIn = null;
			OutputStream tmpOut = null;
			try {
				tmpIn = socket.getInputStream();
				tmpOut = socket.getOutputStream();
			} catch (IOException e) {
				e.printStackTrace();
			}
			in = tmpIn;
			out = tmpOut;
		}


		@Override
		public void run() {
			byte[] buffer = new byte[1024];
			int bytes = 0;
			try {
				bytes = in.read(buffer);
				mHandler.obtainMessage(INFO, bytes, -1, buffer).sendToTarget();
			} catch (IOException e) {
				mHandler.obtainMessage(INFO, 1, 1, "失去連接".getBytes()).sendToTarget();
			}
		}


		public void write(byte[] buffer) {
			try {
				out.write(buffer);
				mHandler.obtainMessage(INFO, -1, -1, buffer).sendToTarget();
			} catch (IOException e) {
			}
		}


		public void cancel() {
			try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	//
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章