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();
|
}
|
}
|