panlili2024
2025-03-05 134209ad70f82051da3ce63471df0cc8f778e57d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.hdl.hdlsdk;
 
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.appcompat.app.AppCompatActivity;
 
import com.hdl.sdk.common.utils.SPUtils;
import com.hdl.sdk.connect.bean.response.BindInfoBean;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.sourceos.bind.BaseEvent;
import com.hdl.sdk.sourceos.bind.BindAuthEvent;
import com.hdl.sdk.sourceos.bind.BindHomeService;
import com.hdl.sdk.sourceos.bind.EventType;
import com.hdl.sdk.sourceos.qrcode.QRCode;
import com.hdl.sdk.sourceos.qrcode.QrCodeView;
import com.hdl.sdk.sourceos.utils.SPKey;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
public class SourceBindActivity extends AppCompatActivity {
 
    private static final String TAG = "SourceBindActivity";
    private QrCodeView qrcodeView;
    private TextView responseTv;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_source_bind);
        registerEventBus();
        initView();
    }
 
    @SuppressLint("WrongConstant")
    private void initView() {
        qrcodeView = findViewById(R.id.qrcode_view);
        responseTv = findViewById(R.id.response_tv);
        createBindQRCodeInfo();
    }
 
    private void createBindQRCodeInfo() {
        final String time = String.valueOf(System.currentTimeMillis());
        String info = QRCode.createBindQRCodeInfo(time);
        qrcodeView.setContent(info);
 
        //开始轮询
        BindHomeService.getInstance().startQuery(time);
    }
 
    /**
     * 绑定成功通知事件
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMessage(BaseEvent event) {
        switch (event.getEventType()) {
            case EventType.ON_PLUS_BINDING_TYPE:
                if (event instanceof BindAuthEvent) {
                    int action = ((BindAuthEvent) event).getAction();
                    if (action == BindAuthEvent.ON_PLUS_BINDING_SUCCEED_ACTION) {
                        //on+绑定成功
                        Toast.makeText(this, "绑定成功", Toast.LENGTH_SHORT).show();
                        BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO);
                        responseTv.setText(bindInfoBean.toString());
 
                        if (bindInfoBean != null) {
                            HDLLinkConfig.getInstance().setHomeId(bindInfoBean.getHomeId());
                            HDLLinkConfig.getInstance().setLocalSecret(bindInfoBean.getLocalSecret());
                        }
                    } else if (action == BindAuthEvent.ON_PLUS_BINDING_TIMEOUT_ACTION) {
                        //on+绑定超时
                        Toast.makeText(this, "绑定超时", Toast.LENGTH_SHORT).show();
                    } else if (action == BindAuthEvent.ON_PLUS_BINDING_ERROR_ACTION) {
                        //网络错误
                        //Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }
 
    protected void unregisterEventBus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
 
    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterEventBus();
        BindHomeService.getInstance().stopQuery();
    }
}