/** 关闭设备 */ private void closeClientDevice() { if (mInputStream != null) { try { mInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (mOutputStream != null) { try { mOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (mBluetoothSocketWrapper != null) { try { mBluetoothSocketWrapper.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 打开连接 * * @return 连接状态 */ public boolean connectDevice() { // 0、判断本机是否支持蓝牙设备 if (mBluetoothAdapter == null) { showTips(R.string.device_local_unsupport_bluetooth); return false; } Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices(); // 获取所有已配对的设备 // 1、没有配对的设备 if (pairedDevices.size() <= 0) { showTips( String.format( mActivity.getResources().getString(R.string.device_idcard_no_bonded_devices), DEVICE_NAME)); return false; } BluetoothDevice adaptedDevice = null; for (Iterator<BluetoothDevice> iterator = pairedDevices.iterator(); iterator.hasNext(); ) { BluetoothDevice device = (BluetoothDevice) iterator.next(); log.i(TAG, "name=" + device.getName() + ",address=" + device.getAddress()); if (DEVICE_NAME.equals(device.getName())) { adaptedDevice = device; break; } } // 2、没有合适的配对设备 if (adaptedDevice == null) { showTips( String.format( mActivity.getResources().getString(R.string.device_idcard_no_bonded_devices), DEVICE_NAME)); return false; } // 3、蓝牙设备没有打开时打开设备 if (!mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.enable(); } // 创建服务器端的监听 mAcceptThread = new AcceptThread(); mAcceptThread.start(); // 4、创建蓝牙Socket try { mBluetoothConnector = new BluetoothConnector(adaptedDevice, mBluetoothAdapter); // 5、连接读卡器设备 mBluetoothSocketWrapper = mBluetoothConnector.connect(); // 6、获取输入输出流 mInputStream = mBluetoothSocketWrapper.getInputStream(); mOutputStream = mBluetoothSocketWrapper.getOutputStream(); // 7、连接成功后血压仪会发送"READY"过来,如果安卓设备成功接收到"READY"说明连接成功了,就可以发送"TOK"指令 byte[] buffer = new byte[5]; int length = mInputStream.read(buffer); if (length != 5 || buffer[0] != 'R' || buffer[1] != 'E' || buffer[2] != 'A' || buffer[3] != 'D' || buffer[4] != 'Y') { showTips(R.string.device_connect_failure); return false; } // 结果应该是READY log.i(TAG, "read data=" + HexBinary.bytesToHexStringPrint(buffer, 0, length)); // 8、如果buffer中是"READY"则继续,发送"TOK"指令 byte[] sendCmd = new byte[] {0x54, 0x4f, 0x4b, (byte) 0xff, (byte) 0xff}; mOutputStream.write(sendCmd); mOutputStream.flush(); // 写入输出流后,要等待500ms,服务端处理延时500ms Thread.sleep(500); // 然后紧接着接收来自服务端也就是血压仪的返回信息,如果成功的话返回"OK" buffer = new byte[2]; length = mInputStream.read(buffer); if (length != 2 || buffer[0] != 'O' || buffer[1] != 'K') { showTips(R.string.device_connect_failure); return false; } log.i(TAG, "跟设备连接成功!"); } catch (IOException e) { e.printStackTrace(); showTips(R.string.device_connect_exception); return false; } catch (InterruptedException e) { e.printStackTrace(); showTips(R.string.device_connect_exception); return false; } finally { closeClientDevice(); } showTips(R.string.device_connect_success); return true; }