|
@@ -0,0 +1,181 @@
|
|
|
+package vinno.sportinspect.devices;
|
|
|
+
|
|
|
+import androidx.annotation.Keep;
|
|
|
+
|
|
|
+import com.clj.fastble.BleManager;
|
|
|
+import com.clj.fastble.callback.BleNotifyCallback;
|
|
|
+import com.clj.fastble.callback.BleWriteCallback;
|
|
|
+import com.clj.fastble.data.BleDevice;
|
|
|
+import com.clj.fastble.exception.BleException;
|
|
|
+
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import vinno.sportinspect.bean.WeightHeightEntity;
|
|
|
+import vinno.sportinspect.config.Constant;
|
|
|
+import vinno.sportinspect.config.StatusConstant;
|
|
|
+import vinno.sportinspect.utils.log.LogUtil;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @name: SportInspect
|
|
|
+ * @author: melon
|
|
|
+ * @time: 2024/3/4 14:27
|
|
|
+ * @version: 1.0
|
|
|
+ * @description:上禾E20身高体重秤
|
|
|
+ */
|
|
|
+@Keep
|
|
|
+public class WeightHeightBySHE20 extends BaseDevices {
|
|
|
+ @Override
|
|
|
+ public void BLUETOOTH_CONNECT(JSONObject params) {
|
|
|
+ super.BLUETOOTH_CONNECT(params);
|
|
|
+// //判断是否配对
|
|
|
+// if (!isBound(mac)) {
|
|
|
+// callBack(StatusConstant.BLE_NO_BOUND);
|
|
|
+// return;
|
|
|
+// }
|
|
|
+ connect(mac, (bleDevice, isFirst) -> {
|
|
|
+ if (isFirst == 0) notifyDataChange(bleDevice);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void notifyDataChange(BleDevice bleDevice) {
|
|
|
+ BleManager.getInstance().notify(bleDevice, Constant.SHE20_SERVER_UUID, Constant.SHE20_CHARACTERISTICS_READ_UUID, new BleNotifyCallback() {
|
|
|
+ @Override
|
|
|
+ public void onNotifySuccess() {
|
|
|
+ LogUtil.d("vinno==>BleManager---> indicate success");
|
|
|
+ checkOnline();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNotifyFailure(BleException exception) {
|
|
|
+ LogUtil.d("vinno==>BleManager---> indicate fail --- " + exception.getDescription());
|
|
|
+ BleManager.getInstance().stopNotify(bleDevice, Constant.SHE20_SERVER_UUID, Constant.SHE20_CHARACTERISTICS_READ_UUID);
|
|
|
+ disConnectDevice();
|
|
|
+ callBack(StatusConstant.BLE_REGISTER_NOTIFY_FAIL);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCharacteristicChanged(byte[] data) {
|
|
|
+ parseData(data);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void parseData(byte[] data) {
|
|
|
+ String result = new String(data);
|
|
|
+ LogUtil.d("vinno==>BleManager--->parseData: " + result);
|
|
|
+
|
|
|
+ if (result.equals("$60")) {
|
|
|
+ onCheckOnlineCallbcak(result);
|
|
|
+ } else if (result.equals("$11")) {
|
|
|
+ onStartMeasureCallbcak(result);
|
|
|
+ } else {
|
|
|
+ handleResultData(result);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理测量结果数据
|
|
|
+ *
|
|
|
+ * @param result
|
|
|
+ */
|
|
|
+ private void handleResultData(String result) {
|
|
|
+ final String[] dataList = result.split("\r\n");
|
|
|
+ for (int i = 0; i < dataList.length; i++) {
|
|
|
+ final String data = dataList[i];
|
|
|
+ if (data.startsWith("W:")) {
|
|
|
+ onWeightHeightCallbcak(data);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查设备在线
|
|
|
+ */
|
|
|
+ private void checkOnline() {
|
|
|
+ LogUtil.d("WeightHeightBySHE20==>CheckOnline");
|
|
|
+ sendCommand("10$");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 启动测量
|
|
|
+ */
|
|
|
+ private void startMeasure() {
|
|
|
+ LogUtil.d("WeightHeightBySHE20==>StartMeasure");
|
|
|
+ sendCommand("11$");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回调 - 检查设备在线
|
|
|
+ */
|
|
|
+ private void onCheckOnlineCallbcak(String result) {
|
|
|
+ LogUtil.d("WeightHeightBySHE20==>OnCheckOnlineCallbcak, Result: " + result);
|
|
|
+ if (result.equals("$60")) {
|
|
|
+ startMeasure();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回调 - 启动测量
|
|
|
+ */
|
|
|
+ private void onStartMeasureCallbcak(String result) {
|
|
|
+ LogUtil.d("WeightHeightBySHE20==>OnStartMeasureCallbcak, Result: " + result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 回调 - 身高体重结果
|
|
|
+ */
|
|
|
+ private void onWeightHeightCallbcak(String result) {
|
|
|
+ // W:075.8 H:172.5
|
|
|
+ // LogUtil.d("WeightHeightBySHE20==>OnWeightHeightCallbcak, Result: " + result);
|
|
|
+ float w = 0, h = 0;
|
|
|
+ Pattern pattern = Pattern.compile("W:(\\d+\\.\\d+) H:(\\d+\\.\\d+)");
|
|
|
+ Matcher matcher = pattern.matcher(result);
|
|
|
+ if (matcher.find()) {
|
|
|
+ String wStr = matcher.group(1);
|
|
|
+ String hStr = matcher.group(2);
|
|
|
+ w = Float.parseFloat(wStr);
|
|
|
+ h = Float.parseFloat(hStr);
|
|
|
+ }
|
|
|
+ final WeightHeightEntity entity = new WeightHeightEntity();
|
|
|
+ entity.WEIGHT = w;
|
|
|
+ entity.HEIGHT = h;
|
|
|
+ callBack(StatusConstant.BLE_MEASURE_WEIGHTHEIGHT_RESULT, entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送命令
|
|
|
+ *
|
|
|
+ * @param command
|
|
|
+ */
|
|
|
+ private void sendCommand(String command) {
|
|
|
+ final byte[] bytes = command.getBytes();
|
|
|
+ sendCommandData(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送命令数据
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
+ */
|
|
|
+ private void sendCommandData(byte[] data) {
|
|
|
+ // 数据下发服务,属性为无应答写入,当手机需要发送数据给外设的时候,直接通过这个服务发送数据即可。
|
|
|
+ BleManager.getInstance().write(bleDeviceConnection, Constant.SHE20_SERVER_UUID, Constant.SHE20_CHARACTERISTICS_WRITE_UUID, data, new BleWriteCallback() {
|
|
|
+ @Override
|
|
|
+ public void onWriteSuccess(int current, int total, byte[] justWrite) {
|
|
|
+ LogUtil.d("=============write success");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onWriteFailure(BleException exception) {
|
|
|
+ LogUtil.d("=============write fail");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|