diff --git a/app/build.gradle b/app/build.gradle index df1abe0..0b5a9bd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -65,7 +65,8 @@ dependencies { testCompile 'junit:junit:4.12' compile 'cn.bingoogolapple:bga-qrcode-zxing:1.3.6' // compile ("com.github.duodebuyali:QNScaleSDK:master-SNAPSHOT") { - compile("com.github.YolandaQingniu:qnscalesdk:2.2.0-beta8") { + /*compile("com.github.YolandaQingniu:qnscalesdk:2.2.0-beta8") { exclude module: "support-v4"//防止和module引用的v4资源版本不同 - } + }*/ + compile project(':qnscalesdk') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9d95468..945177a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -78,6 +78,10 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/qingniu/qnble/demo/view/HeightScaleActivity.java b/app/src/main/java/com/qingniu/qnble/demo/view/HeightScaleActivity.java new file mode 100644 index 0000000..508fe3c --- /dev/null +++ b/app/src/main/java/com/qingniu/qnble/demo/view/HeightScaleActivity.java @@ -0,0 +1,454 @@ +package com.qingniu.qnble.demo.view; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; + +import com.qingniu.qnble.demo.R; +import com.qingniu.qnble.demo.adapter.ListAdapter; +import com.qingniu.qnble.demo.bean.User; +import com.qingniu.qnble.demo.util.UserConst; +import com.qingniu.scale.constant.DecoderConst; +import com.yolanda.health.qnblesdk.constant.QNIndicator; +import com.yolanda.health.qnblesdk.constant.QNScaleStatus; +import com.yolanda.health.qnblesdk.constant.UserGoal; +import com.yolanda.health.qnblesdk.constant.UserShape; +import com.yolanda.health.qnblesdk.listener.QNBleConnectionChangeListener; +import com.yolanda.health.qnblesdk.listener.QNLogListener; +import com.yolanda.health.qnblesdk.listener.QNResultCallback; +import com.yolanda.health.qnblesdk.listener.QNScaleDataListener; +import com.yolanda.health.qnblesdk.out.QNBleApi; +import com.yolanda.health.qnblesdk.out.QNBleDevice; +import com.yolanda.health.qnblesdk.out.QNScaleData; +import com.yolanda.health.qnblesdk.out.QNScaleItemData; +import com.yolanda.health.qnblesdk.out.QNScaleStoreData; +import com.yolanda.health.qnblesdk.out.QNUser; + +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + + +/** + * author: ch + * date: 2020/6/16 + * package_name: com.qingniu.qnble.demo + * description:身高一体机界面 + */ + +public class HeightScaleActivity extends AppCompatActivity implements View.OnClickListener { + + + public static Intent getCallIntent(Context context, User user, QNBleDevice device) { + return new Intent(context, HeightScaleActivity.class) + .putExtra(UserConst.USER, user) + .putExtra(UserConst.DEVICE, device); + } + + + @BindView(R.id.connectBtn) + Button mConnectBtn; + @BindView(R.id.statusTv) + TextView mStatusTv; + @BindView(R.id.weightTv) + TextView mWeightTv; + @BindView(R.id.back_tv) + TextView mBackTv; + @BindView(R.id.listView) + ListView mListView; + + private QNBleDevice mBleDevice; + private List mDatas = new ArrayList<>(); + private QNBleApi mQNBleApi; + + private User mUser; + + private boolean mIsConnected; + + private ListAdapter listAdapter; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_height_scale); + mQNBleApi = QNBleApi.getInstance(this); + //此API是用来监听日志的,如果需要上传日志到服务器则可以使用,否则不需要设置 + mQNBleApi.setLogListener(new QNLogListener() { + @Override + public void onLog(String log) { + // Log.e("test", log); + } + }); + ButterKnife.bind(this); + initIntent(); + initView(); + initData(); + } + + private void initData() { + initBleConnectStatus(); + initUserData(); //设置数据监听器,返回数据,需在连接当前设备前设置 + //已经连接设备先断开设备,再连接 + if (mIsConnected) { + doDisconnect(); + } else { + connectQnDevice(mBleDevice); //连接当前设备 + } + } + + private void initBleConnectStatus() { + mQNBleApi.setBleConnectionChangeListener(new QNBleConnectionChangeListener() { + //正在连接 + @Override + public void onConnecting(QNBleDevice device) { + setBleStatus(QNScaleStatus.STATE_CONNECTING); + } + + //已连接 + @Override + public void onConnected(QNBleDevice device) { + setBleStatus(QNScaleStatus.STATE_CONNECTED); + } + + @Override + public void onServiceSearchComplete(QNBleDevice device) { + + } + + //正在断开连接,调用断开连接时,会马上回调 + @Override + public void onDisconnecting(QNBleDevice device) { + setBleStatus(QNScaleStatus.STATE_DISCONNECTING); + } + + // 断开连接,断开连接后回调 + @Override + public void onDisconnected(QNBleDevice device) { + setBleStatus(QNScaleStatus.STATE_DISCONNECTED); + } + + //出现了连接错误,错误码参考附表 + @Override + public void onConnectError(QNBleDevice device, int errorCode) { + Log.d("ConnectActivity", "onConnectError:" + errorCode); + setBleStatus(QNScaleStatus.STATE_DISCONNECTED); + } + + }); + } + + private void connectQnDevice(QNBleDevice device) { + mQNBleApi.connectDevice(device, createQNUser(), new QNResultCallback() { + @Override + public void onResult(int code, String msg) { + Log.d("ConnectActivity", "连接设备返回:" + msg); + } + }); + + } + + private QNUser createQNUser() { + UserShape userShape; + switch (mUser.getChoseShape()) { + case 0: + userShape = UserShape.SHAPE_NONE; + break; + case 1: + userShape = UserShape.SHAPE_SLIM; + break; + case 2: + userShape = UserShape.SHAPE_NORMAL; + break; + case 3: + userShape = UserShape.SHAPE_STRONG; + break; + case 4: + userShape = UserShape.SHAPE_PLIM; + break; + default: + userShape = UserShape.SHAPE_NONE; + break; + } + + UserGoal userGoal; + switch (mUser.getChoseGoal()) { + case 0: + userGoal = UserGoal.GOAL_NONE; + break; + case 1: + userGoal = UserGoal.GOAL_LOSE_FAT; + break; + case 2: + userGoal = UserGoal.GOAL_STAY_HEALTH; + break; + case 3: + userGoal = UserGoal.GOAL_GAIN_MUSCLE; + break; + case 4: + userGoal = UserGoal.POWER_OFTEN_EXERCISE; + break; + case 5: + userGoal = UserGoal.POWER_LITTLE_EXERCISE; + break; + case 6: + userGoal = UserGoal.POWER_OFTEN_RUN; + break; + default: + userGoal = UserGoal.GOAL_NONE; + break; + } + + return mQNBleApi.buildUser(mUser.getUserId(), + mUser.getHeight(), mUser.getGender(), mUser.getBirthDay(), mUser.getAthleteType(), + userShape, userGoal, mUser.getClothesWeight(), new QNResultCallback() { + @Override + public void onResult(int code, String msg) { + Log.d("ConnectActivity", "创建用户信息返回:" + msg); + } + }); + } + + + private void initUserData() { + mQNBleApi.setDataListener(new QNScaleDataListener() { + @Override + public void onGetUnsteadyWeight(QNBleDevice device, double weight) { + Log.d("ConnectActivity", "体重是:" + weight); + mWeightTv.setText(initWeight(weight)); + } + + @Override + public void onGetScaleData(QNBleDevice device, QNScaleData data) { + Log.d("ConnectActivity", "收到测量数据"); + onReceiveScaleData(data); + QNScaleItemData fatValue = data.getItem(QNIndicator.TYPE_SUBFAT); + if (fatValue != null) { + String value = fatValue.getValue() + ""; + Log.d("ConnectActivity", "收到皮下脂肪数据:" + value); + } + } + + @Override + public void onGetStoredScale(QNBleDevice device, List storedDataList) { + Log.d("ConnectActivity", "收到存储数据"); + if (storedDataList != null && storedDataList.size() > 0) { + QNScaleStoreData data = storedDataList.get(0); + for (int i = 0; i < storedDataList.size(); i++) { + Log.d("ConnectActivity", "收到存储数据:" + storedDataList.get(i).getWeight()); + } + QNUser qnUser = createQNUser(); + data.setUser(qnUser); + QNScaleData qnScaleData = data.generateScaleData(); + onReceiveScaleData(qnScaleData); + } + } + + @Override + public void onGetElectric(QNBleDevice device, int electric) { + String text = "收到电池电量百分比:" + electric; + Log.d("ConnectActivity", text); + if (electric == DecoderConst.NONE_BATTERY_VALUE) {//获取电池信息失败 + return; + } + Toast.makeText(HeightScaleActivity.this, text, Toast.LENGTH_SHORT).show(); + } + + //测量过程中的连接状态 + @Override + public void onScaleStateChange(QNBleDevice device, int status) { + Log.d("ConnectActivity", "秤的连接状态是:" + status); + setBleStatus(status); + } + + @Override + public void onScaleEventChange(QNBleDevice device, int scaleEvent) { + Log.d("ConnectActivity", "秤返回的事件是:" + scaleEvent); + } + }); + } + + private void initIntent() { + Intent intent = getIntent(); + if (intent != null) { + mBleDevice = intent.getParcelableExtra(UserConst.DEVICE); + mUser = intent.getParcelableExtra(UserConst.USER); + } + } + + private String initWeight(double weight) { + int unit = mQNBleApi.getConfig().getUnit(); + return mQNBleApi.convertWeightWithTargetUnit(weight, unit); + } + + private void initView() { + mConnectBtn.setOnClickListener(this); + mBackTv.setOnClickListener(this); + listAdapter = new ListAdapter(mDatas, mQNBleApi, createQNUser()); + mListView.setAdapter(listAdapter); + listAdapter.notifyDataSetChanged(); + } + + + @Override + protected void onDestroy() { + super.onDestroy(); + doDisconnect(); + mQNBleApi.setBleConnectionChangeListener(null); + mQNBleApi.setDataListener(null); + } + + + private void onReceiveScaleData(QNScaleData md) { + mDatas.clear(); + /** + * 增加身高显示 + */ + QNScaleItemData qnScaleItemData = new QNScaleItemData(); + qnScaleItemData.setName(getString(R.string.height)); + qnScaleItemData.setValue(md.getHeight()); + mDatas.add(qnScaleItemData); + + mDatas.addAll(md.getAllItem()); + + listAdapter.notifyDataSetChanged(); + } + + private void setBleStatus(int bleStatus) { + String stateString; + String btnString; + switch (bleStatus) { + case QNScaleStatus.STATE_CONNECTING: { + stateString = getResources().getString(R.string.connecting); + btnString = getResources().getString(R.string.disconnected); + mIsConnected = true; + break; + } + case QNScaleStatus.STATE_CONNECTED: { + stateString = getResources().getString(R.string.connected); + btnString = getResources().getString(R.string.disconnected); + mIsConnected = true; + break; + } + case QNScaleStatus.STATE_DISCONNECTING: { + stateString = getResources().getString(R.string.disconnect_in_progress); + btnString = getResources().getString(R.string.connect); + mIsConnected = false; + + break; + } + case QNScaleStatus.STATE_LINK_LOSS: { + stateString = getResources().getString(R.string.connection_disconnected); + btnString = getResources().getString(R.string.connect); + mIsConnected = false; + break; + } + case QNScaleStatus.STATE_START_MEASURE: { + stateString = getResources().getString(R.string.measuring); + btnString = getResources().getString(R.string.disconnected); + break; + } + case QNScaleStatus.STATE_REAL_TIME: { + stateString = getResources().getString(R.string.real_time_weight_measurement); + btnString = getResources().getString(R.string.disconnected); + break; + } + case QNScaleStatus.STATE_BODYFAT: { + stateString = getResources().getString(R.string.impedance_measured); + btnString = getResources().getString(R.string.disconnected); + break; + } + case QNScaleStatus.STATE_HEART_RATE: { + stateString = getResources().getString(R.string.measuring_heart_rate); + btnString = getResources().getString(R.string.disconnected); + break; + } + case QNScaleStatus.STATE_MEASURE_COMPLETED: { + stateString = getResources().getString(R.string.measure_complete); + btnString = getResources().getString(R.string.disconnected); + break; + } + case QNScaleStatus.STATE_WIFI_BLE_START_NETWORK: + stateString = getResources().getString(R.string.start_set_wifi); + btnString = getResources().getString(R.string.disconnected); + Log.d("ConnectActivity", "开始设置WiFi"); + break; + case QNScaleStatus.STATE_WIFI_BLE_NETWORK_FAIL: + stateString = getResources().getString(R.string.failed_to_set_wifi); + btnString = getResources().getString(R.string.disconnected); + Log.d("ConnectActivity", "设置WiFi失败"); + break; + case QNScaleStatus.STATE_WIFI_BLE_NETWORK_SUCCESS: + stateString = getResources().getString(R.string.success_to_set_wifi); + btnString = getResources().getString(R.string.disconnected); + Log.d("ConnectActivity", "设置WiFi成功"); + break; + default: { + stateString = getResources().getString(R.string.connection_disconnected); + btnString = getResources().getString(R.string.connect); + mIsConnected = false; + break; + } + } + mStatusTv.setText(stateString); + mConnectBtn.setText(btnString); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.connectBtn: + if (mIsConnected) { + //已经连接,断开连接 + this.doDisconnect(); + } else { + //断开连接,就开始连接 + mDatas.clear(); + listAdapter.notifyDataSetChanged(); + this.doConnect(); + } + break; + case R.id.back_tv: + doDisconnect(); + finish(); + break; + } + } + + + private void doConnect() { + if (mBleDevice == null || mUser == null) { + return; + } + + mQNBleApi.connectDevice(mBleDevice, createQNUser(), new QNResultCallback() { + @Override + public void onResult(int code, String msg) { + Log.d("ConnectActivity", "连接设备返回:" + msg); + if (code == 0) { + mIsConnected = true; + } + } + }); + + } + + private void doDisconnect() { + mQNBleApi.disconnectDevice(mBleDevice, new QNResultCallback() { + @Override + public void onResult(int code, String msg) { + Log.d("ConnectActivity", "断开连接设备返回:" + msg); + } + }); + } + +} diff --git a/app/src/main/java/com/qingniu/qnble/demo/view/ScanActivity.java b/app/src/main/java/com/qingniu/qnble/demo/view/ScanActivity.java index 8e9a519..1750224 100644 --- a/app/src/main/java/com/qingniu/qnble/demo/view/ScanActivity.java +++ b/app/src/main/java/com/qingniu/qnble/demo/view/ScanActivity.java @@ -306,13 +306,15 @@ public void cancelClick() { wifiSetDialog.show(); } else if (device.getDeviceType() == QNDeviceType.SCALE_WSP) { // wsp 双模秤 startActivity(WspConfigActivity.getIntent(ScanActivity.this, mUser, device)); + }else if (device.getDeviceType() == QNDeviceType.HEIGHT_SCALE) { // 身高一体机 + startActivity(HeightScaleActivity.getCallIntent(this, mUser, device)); } } else { // SCALE_BROADCAST if (device.getDeviceType() == QNDeviceType.SCALE_BROADCAST) { startActivity(BroadcastScaleActivity.getCallIntent(ScanActivity.this, mUser, device)); } else if (device.getDeviceType() == QNDeviceType.SCALE_KITCHEN) {// SCALE_KITCHEN - + startActivity(kitchenScaleActivity.getCallIntent(ScanActivity.this)); } else {//SCALE_BLE_DEFAULT //连接设备 connectDevice(device); diff --git a/app/src/main/res/layout/activity_height_scale.xml b/app/src/main/res/layout/activity_height_scale.xml new file mode 100644 index 0000000..3b407a7 --- /dev/null +++ b/app/src/main/res/layout/activity_height_scale.xml @@ -0,0 +1,55 @@ + + + + + +