蓝牙通信模块

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();
			}
		}
	}
	//
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值