Receiver的设置
一、Receiver1(蓝牙状态的改变通过广播接收)
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,-1);
switch (status) {
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "蓝牙已关闭");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "蓝牙已打开");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "蓝牙关闭中...");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "蓝牙打开中...");
break;
default:
break;
}
}
}
};
二、Receiver2(蓝牙搜索到设备、绑定设备(配对)通过广播接收)
IntentFilter intentFilterDevice = new IntentFilter();
intentFilterDevice.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intentFilterDevice.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intentFilterDevice.addAction(BluetoothDevice.ACTION_FOUND);
intentFilterDevice.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
intentFilterDevice.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mReceiverDeceiver,intentFilterDevice);
@Override
public void onReceive(Context context,Intent intent) {
String action = intent.getAction();
ArraySet<Object> mList = null;
Policy mAdapter = null;
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.d(TAG,"开始搜索");
mList.clear();
mAdapter.refresh(mList);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d(TAG,"查找到设备完成");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
Log.d(TAG,"搜索到设备");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mList.add(device);
mAdapter.refresh(mList);
} else if (BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
int scanMode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,0);
if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Log.d(TAG,"设备可见监听");
} else {
Log.d(TAG,"设备不可见监听");
}
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (remoteDevice == null) {
Log.d(TAG,"没有绑定设备");
return;
}
int status = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,0);
if (status == BluetoothDevice.BOND_BONDED) {
Log.d(TAG,"绑定设备完成: " + remoteDevice.getName());
} else if (status == BluetoothDevice.BOND_BONDING) {
Log.d(TAG,"绑定设备中: " + remoteDevice.getName());
} else if (status == BluetoothDevice.BOND_NONE) {
Log.d(TAG,"取消绑定: ");
}
}
服务端代码
public class AccepThread extends Thread {
private static final String NAME = "BluetoothClass";
private static final UUID MY_UUID = UUID.fromString(Constant.CONNECTTION_UUID);
private final BluetoothServerSocket mmServerSocket;
private final BluetoothAdapter mBluetoothAdapter;
private final Handler mHandler;
private ConnectedThread mConnectedThread;
public AccepThread(BluetoothAdapter adapter, Handler handler) throws IOException {
mBluetoothAdapter = adapter;
this.mHandler = handler;
mmServerSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
}
@Override
public void run() {
super.run();
BluetoothSocket socket = null;
while (true){
mHandler.sendEmptyMessage(Constant.MSG_START_LISTENING);
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
mHandler.sendEmptyMessage(Constant.MSG_ERROR);
e.printStackTrace();
break;
}
if(socket != null) {
manageConnectSocket(socket);
}
}
}
private void manageConnectSocket(BluetoothSocket socket) {
if(mConnectedThread != null) {
mConnectedThread.cancle();
}
mHandler.sendEmptyMessage(Constant.MSG_GOT_A_CLINET);
mConnectedThread = new ConnectedThread(socket, mHandler);
mConnectedThread.start();
}
public void cancle() {
try {
mmServerSocket.close();
mHandler.sendEmptyMessage(Constant.MSG_FINISH_LISTENING);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendData(byte[] data){
if(mConnectedThread != null) {
mConnectedThread.write(data);
}
}
}
客户端代码
public class ConnectThread extends Thread{
private static final UUID MY_UUID = UUID.fromString(Constant.CONNECTTION_UUID);
private final BluetoothSocket mmSoket;
private final BluetoothDevice mmDevice;
private BluetoothAdapter mBluetoothAdapter;
private final 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) {
mHandler.sendEmptyMessage(Constant.MSG_CONNECTED_TO_SERVER);
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";
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 = new byte[1024];
while (true) {
try {
int bytes = mmInputStream.read(buffer);
if(bytes > 0) {
String data = new String(buffer,0,bytes,"utf-8");
Message message = mHandler.obtainMessage(Constant.MSG_GOT_DATA,data);
mHandler.sendMessage(message);
}
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();
}
}
}
蓝牙通信的步骤如下:
1,开启蓝牙
2,搜索可用设备
3,创建蓝牙socket,获取输入输出流
4,读取和写入数据
5,断开连接关闭蓝牙
|