欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android10调用涂鸦接口实现网管适配

程序员文章站 2024-02-14 23:03:58
...

布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".net.ActivityNetZigbee">
    <EditText
        android:id="@+id/tv_wifi_name"
        android:layout_gravity="center"
        android:gravity="left"
        android:hint="请输入wifi名称"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"/>
    <EditText
        android:id="@+id/tv_wifi_pwd"
        android:gravity="left"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:hint="请输入wifi密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/tv_wifi_prompt"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="请输入同一个局域网的wifi名称密码"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_wifi_search"
        android:layout_margin="20dp"
        android:layout_gravity="center"
        android:text="搜索"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
<!-- android:indeterminate="true" 表示未知时长进度   -->
    <com.google.android.material.progressindicator.CircularProgressIndicator
        android:id="@+id/cpi_wifi_progress"
        android:layout_gravity="center"
        android:visibility="gone"
        android:indeterminate="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

主界面关键代码

public class ActivityNetZigbee extends AppCompatActivity implements View.OnClickListener{
    private TuyaHomeSdk tuyaHomeSdk;
    private long homeId=29846363;   //写死
//    private EditText etWifiName,etWifiPwd;
//    private TextView tvPrompt;
//    private Button btnSearch;
//    private CircularProgressIndicator cpiProcess;
    private final static String TAG="ActivityNetZigbee:xwg";
    private ActivityNetZigbeeBinding activityNetZigbeeBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_net_zigbee);
        activityNetZigbeeBinding= ActivityNetZigbeeBinding.inflate(LayoutInflater.from(this));
        setContentView(activityNetZigbeeBinding.getRoot());
        activityNetZigbeeBinding.btnWifiSearch.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
//        重写点击按钮方法
        String strWifiName=activityNetZigbeeBinding.tvWifiName.getText().toString();
        String strWifiPwd=activityNetZigbeeBinding.tvWifiPwd.getText().toString();
        if (v.getId()==R.id.btn_wifi_search){
            if(strWifiName.length()==0||strWifiPwd.length()==0){
                Toast.makeText(ActivityNetZigbee.this,"wifi name or pws must not be null!",Toast.LENGTH_SHORT).show();
            }
            else{
                tuyaHomeSdk=new TuyaHomeSdk();
                tuyaHomeSdk.getActivatorInstance().getActivatorToken(homeId, new ITuyaActivatorGetToken() {
                    @Override
                    public void onSuccess(String token) {
                        Log.i(TAG,"get token success,start zigbee mode!");
                        ActivatorBuilder builder = new ActivatorBuilder()
                                .setSsid(strWifiName)
                                .setContext(v.getContext())
                                .setPassword(strWifiPwd)
                                .setActivatorModel(ActivatorModelEnum.TY_EZ)
                                .setTimeOut(100)
                                .setToken(token)
                                .setListener(new ITuyaSmartActivatorListener() {
                                    @Override
                                    public void onError(String errorCode, String errorMsg) {
                                        Log.i(TAG,"activater error,errorCode:"+errorCode+",errorMsg:"+errorMsg);
                                        activityNetZigbeeBinding.cpiWifiProgress.setVisibility(View.GONE);
                                        activityNetZigbeeBinding.btnWifiSearch.setClickable(true);
                                    }

                                    @Override
                                    public void onActiveSuccess(DeviceBean devResp) {
                                        Log.i(TAG,"onActiveSuccess!");
                                        activityNetZigbeeBinding.cpiWifiProgress.setVisibility(View.GONE);
                                        activityNetZigbeeBinding.btnWifiSearch.setClickable(false);
                                    }

                                    @Override
                                    public void onStep(String step, Object data) {
                                        Log.i(TAG,"onStep:"+step+",data:"+data);
                                    }
                                });
                        ITuyaActivator mTuyaActivator = TuyaHomeSdk.getActivatorInstance().newActivator(builder);
                        //开始配网
                        Log.i(TAG,"start search...,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                        mTuyaActivator.start();
                        activityNetZigbeeBinding.cpiWifiProgress.setVisibility(View.VISIBLE);   //显示转圈
                        activityNetZigbeeBinding.btnWifiSearch.setClickable(false);
                    }

                    @Override
                    public void onFailure(String errorCode, String errorMsg) {
                        Log.i(TAG,"get token error,errorCode:"+errorCode+",errorMsg:"+errorMsg);
                    }
                });
            }
        }
    }
}

相关标签: android android