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