1,首先开启蓝牙
2,搜索可用设备
3,创建蓝牙socket,获取输入输出流
4,读取和写入数据
5,断开连接关闭蓝牙
服务端
public class AcceptThread extends Thread {
private static final String NAME ="LSY";
private static final UUID MY_UUID =UUID.fromString("298a34ea-3e59-43c5-9128-48834a7a6cf4") ;
private final BluetoothServerSocket mmServerSocket;
private BluetoothAdapter bluetoothAdapter;
private final Handler mHandler;
private ConnectedThread mConnectedThread;
public AcceptThread(Handler UIhandler) {
// Use a temporary object that is later assigned to mmServerSocket
// because mmServerSocket is final.
mHandler = UIhandler;
BluetoothServerSocket tmp = null;
bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
try {
// MY_UUID is the app's UUID string, also used by the client code.
tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
} catch (IOException e) {
Log.e(TAG, "Socket's listen() method failed", e);
}
mmServerSocket = tmp;
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned.
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
Log.e(TAG, "Socket's accept() method failed", e);
break;
}
if (socket != null) {
// A connection was accepted. Perform work associated with
// the connection in a separate thread.
try {
manageMyConnectedSocket(socket);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
mHandler.setEncoding("lsy");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
mmServerSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
private void manageMyConnectedSocket(BluetoothSocket socket) throws UnsupportedEncodingException{
mHandler.setEncoding("Constant.MSG_CONNECTED_TO_SERVER");
mConnectedThread = new ConnectedThread(socket,mHandler);
mConnectedThread.start();
}
// Closes the connect socket and causes the thread to finish.
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) {
Log.e(TAG, "Could not close the connect socket", e);
}
}
public void sendData(byte[] data) {
if(mConnectedThread != null) {
mConnectedThread.write(data);
}
}
}
客户端
public class ConnectThread extends Thread{
private static Object Constant;
private static final UUID MY_UUID =UUID.fromString("298a34ea-3e59-43c5-9128-48834a7a6cf4") ;
private BluetoothSocket mmSoket;
private BluetoothDevice mmDevice;
private BluetoothAdapter mBluetoothAdapter;
private Handler mHandler;
private ConnectedThread mConnectedThread;
public ConnectThread(BluetoothDevice device, BluetoothAdapter bluetoothAdapter, Handler mUIhandler) {
mmDevice = device;
mBluetoothAdapter = bluetoothAdapter;
mHandler = mUIhandler;
BluetoothSocket tmp = null;
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
e.printStackTrace();
}
mmSoket = tmp;
}
@Override
public void run() {
super.run();
mBluetoothAdapter.cancelDiscovery();
try {
mmSoket.connect();
} catch (IOException e)
{
try {
mmSoket.close();
} catch (IOException e1) { }return;
}
manageConnectedSocket(mmSoket);
}
private void manageConnectedSocket(BluetoothSocket mmSoket) {
try {
mHandler.setEncoding("lsy");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
mConnectedThread = new ConnectedThread(mmSoket,mHandler);
mConnectedThread.start();
}
public void cancle() {
try {
mmSoket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(byte[] data) {
if(mConnectedThread != null) {
mConnectedThread.write(data);
}
}
}
共同通讯处理类
public class ConnectedThread extends Thread{ private final BluetoothSocket mmSokcet; private final InputStream mmInputStream; private final OutputStream mmOutputStream; private Handler mHandler; private String TAG ="ConnectedThread"; private byte[][] newbyte; public ConnectedThread(BluetoothSocket socket,Handler handler) { mmSokcet = socket; mHandler = handler; InputStream tmpIn =null; OutputStream tmpOut =null; try{ tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); }catch(IOException e) { e.printStackTrace(); } mmInputStream = tmpIn; mmOutputStream = tmpOut; } @Override public void run() { super.run(); byte[] buffer =newbyte[1024]; while(true) { try{ int bytes = mmInputStream.read(buffer); if(bytes >0) { String data =new String(buffer,0,bytes,"utf-8"); mHandler.setEncoding("lsy"); } Log.d(TAG,"messge size :"+ bytes); }catch(IOException e) { e.printStackTrace(); } } } public void cancle() { try{ mmSokcet.close(); }catch(IOException e) { e.printStackTrace(); } } public void write(byte[] data) { try{ mmOutputStream.write(data); }catch(IOException e) { e.printStackTrace(); } } }