最近时间比较宽裕,觉得自己可以写一些东西来总结一下工作,索性就写一篇关于安卓蓝牙的开发总结吧
安卓蓝牙开发其实也就是socket的开发,同时分为服务端和客户端,下面我就按照我的开发流程来降整个的安卓蓝牙2.0开发叙述一下,蓝牙4.0BLE我也会在之后给大家更新
首先,我们要注册蓝牙相关的广播并在manifest中给出相应的权限(安卓6.0之后由于相应的底层改变,注册权限的时候不仅要给蓝牙的权限,还要给相应的位置权限),我在这里只为大家提供相应的广播,权限自己百度吧哈哈:
之后我们需要获得蓝牙模块的mac地址和模块名,所以我们要进行蓝牙设备的查找,在查找界面中 我们使用BluetoothAdapter这个适配器来存储我们搜索的设备,使用Set集合来降获得的设备进行存储通过for循环来循环出手机搜索到的所有设备,代码如下
public class BluetoothPort implements BasePrinterPort { private static final String TAG = "BluetoothPort"; private String mDeviceAddress; private BluetoothSocket mSocket; private BluetoothAdapter mAdapter; private static InputStream inputStream; private static OutputStream outputStream; private Context mContext; private Handler mHandler; private int mState; private BluetoothDevice mDevice; private final UUID PRINTER_UUID = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); public BluetoothPort(BluetoothDevice bluetoothDevice, Handler handler) { this.mHandler = handler; this.mDevice = bluetoothDevice; this.mAdapter = BluetoothAdapter.getDefaultAdapter(); this.mState = 103; } public BluetoothPort(String address, Handler handler) { this.mHandler = handler; this.mAdapter = BluetoothAdapter.getDefaultAdapter(); this.mDevice = this.mAdapter.getRemoteDevice(address); this.mState = 103; } public boolean open() { boolean isConnected = false; if(this.mState != 103) { this.close(); } isConnected = this.connect2Device(); return isConnected; } public void close() { try { if(this.mSocket != null) { this.mSocket.close(); } } catch (IOException var2) { var2.printStackTrace(); } this.mDevice = null; this.mSocket = null; if(this.mState != 102) { this.setState(103); } } public int write(byte[] srcData) { try { if(outputStream == null) { return -3; } outputStream.write(srcData); outputStream.flush(); } catch (IOException var3) { var3.printStackTrace(); return -1; } catch (Exception var4) { var4.printStackTrace(); return -2; } return srcData.length; } public int read(byte[] buffer) { int readLen = 0; try { if(inputStream != null && (readLen = inputStream.available()) > 0) { inputStream.read(buffer); } return readLen; } catch (IOException var4) { var4.printStackTrace(); return -1; } catch (Exception var5) { var5.printStackTrace(); return -1; } } public int read(int timeout, byte[] buffer) { int readLen = -1; try { while((readLen = inputStream.available()) <= 0) { timeout -= 50; if(timeout <= 0) { break; } try { Thread.sleep(50L); } catch (InterruptedException var5) { var5.printStackTrace(); } } if(readLen > 0) { buffer = new byte[readLen]; inputStream.read(buffer); } } catch (IOException var6) { var6.printStackTrace(); } return readLen; } private boolean connect2Device() { boolean hasError = false; if(this.mAdapter.isDiscovering()) { this.mAdapter.cancelDiscovery(); } try { this.mSocket = this.mDevice.createRfcommSocketToServiceRecord(this.PRINTER_UUID); this.mSocket.connect(); } catch (IOException var9) { var9.printStackTrace(); try { if(this.mSocket != null) { this.mSocket.close(); } Thread.sleep(2000L); } catch (IOException var7) { var7.printStackTrace(); } catch (InterruptedException var8) { var8.printStackTrace(); } hasError = this.ReTryConnect(); } catch (Exception var10) { try { if(this.mSocket != null) { this.mSocket.close(); } Thread.sleep(2000L); } catch (IOException var5) { var5.printStackTrace(); } catch (InterruptedException var6) { var6.printStackTrace(); } hasError = this.ReTryConnect(); } if(!hasError) { try { inputStream = this.mSocket.getInputStream(); outputStream = this.mSocket.getOutputStream(); } catch (IOException var4) { hasError = true; var4.printStackTrace(); } } if(hasError) { this.setState(102); this.close(); } else { this.setState(101); } return !hasError; } @SuppressLint({"NewApi"}) private boolean ReTryConnect() { try { if(VERSION.SDK_INT >= 10) { this.mSocket = this.mDevice.createInsecureRfcommSocketToServiceRecord(this.PRINTER_UUID); } else { Method var4 = this.mDevice.getClass().getMethod("createRfcommSocket", new Class[]{Integer.TYPE}); this.mSocket = (BluetoothSocket)var4.invoke(this.mDevice, new Object[]{Integer.valueOf(1)}); } this.mSocket.connect(); return false; } catch (Exception var41) { if(this.mSocket != null) { try { this.mSocket.close(); } catch (IOException var3) { var3.printStackTrace(); } } var41.printStackTrace(); return true; } } private synchronized void setState(int state) { if(this.mState != state) { this.mState = state; if(this.mHandler != null) { this.mHandler.obtainMessage(this.mState).sendToTarget(); } } } }
private BroadcastReceiver boundDeviceReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (!mDevice.equals(device)) { return; } switch (device.getBondState()) { case BluetoothDevice.BOND_BONDING: break; case BluetoothDevice.BOND_BONDED: mContext.unregisterReceiver(boundDeviceReceiver); dialog.show(); if (myPrinter != null) { new connectThread().start(); // received(); } break; case BluetoothDevice.BOND_NONE: mContext.unregisterReceiver(boundDeviceReceiver); break; } } } };
private class connectThread extends Thread { @Override public void run() { super.run(); if (myPrinter != null) { isConnected = myPrinter.openConnection(); } } }