Advertisement

基于esp8266和Android studio的智能灯控制及APP开发(四)

阅读量:

本文采用Android Studio来开发应用程序,并与其进行连接。最终完成了对智能灯具开关的控制。

一、主界面设置

采用线性布局,设置两个按钮用来进行开关操作,效果如下。

二、连接阿里云

由于控制灯具开关所需的业务类型仅限于云到设备的消息传输,因此可以通过SetDeviceProperty来实现该功能

复制代码
 package com.example.setdeviceproperty_demo;

    
  
    
  
    
 import com.aliyun.iot20180120.Client;
    
 import com.aliyun.iot20180120.models.SetDevicePropertyRequest;
    
 import com.aliyun.iot20180120.models.SetDevicePropertyResponse;
    
  
    
 import com.aliyun.tea.TeaModel;
    
 import com.aliyun.teaopenapi.models.Config;
    
 import com.aliyun.teautil.Common;
    
  
    
  
    
 public class SetDevicePropertyUtil {
    
     // Access Keys
    
     public static final String accessKeyId = "XXXXX";
    
     public static final String accessKeySecret = "XXXX";
    
  
    
     /** * 使用AK&SK初始化账号Client
    
      * @param accessKeyId
    
      * @param accessKeySecret
    
      * @return Client
    
      * @throws Exception
    
      */
    
     public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
    
     Config config = new Config()
    
             // 您的AccessKey ID
    
             .setAccessKeyId(accessKeyId)
    
             // 您的AccessKey Secret
    
             .setAccessKeySecret(accessKeySecret);
    
     // 访问的域名
    
     config.endpoint = "iot.cn-shanghai.aliyuncs.com";
    
     return new Client(config);
    
     }
    
  
    
     // 发起调用
    
     public static String setDevicePropertyOpen() throws Exception {
    
     Client client = SetDevicePropertyUtil.createClient(accessKeyId, accessKeySecret);
    
     SetDevicePropertyRequest setDevicePropertyRequest = new SetDevicePropertyRequest()
    
             .setIotInstanceId("iot-xxxx")
    
             .setProductKey("xxxx")
    
             .setDeviceName("xxxx")
    
             .setItems("{\"OF\":1}");
    
     SetDevicePropertyResponse resp = client.setDeviceProperty(setDevicePropertyRequest);
    
     return Common.toJSONString(TeaModel.buildMap(resp));
    
     }
    
     // 发起调用
    
     public static String setDevicePropertyClose() throws Exception {
    
     Client client = SetDevicePropertyUtil.createClient(accessKeyId, accessKeySecret);
    
     SetDevicePropertyRequest setDevicePropertyRequest = new SetDevicePropertyRequest()
    
             .setIotInstanceId("iot-xxxx")
    
             .setProductKey("xxxx")
    
             .setDeviceName("xxxx")
    
             .setItems("{\"OF\":0}");
    
     SetDevicePropertyResponse resp = client.setDeviceProperty(setDevicePropertyRequest);
    
     return Common.toJSONString(TeaModel.buildMap(resp));
    
     }
    
 }

三、主函数

主函数创建两个按钮,并将其与布局文件中的button关联起来;点击OPEN按钮时会开启灯泡,“CLOSE按钮点击则会关闭它。”

复制代码
 package com.example.setdeviceproperty_demo;

    
  
    
 import android.support.v7.app.AppCompatActivity;
    
 import android.os.Bundle;
    
 import android.view.View;
    
 import android.widget.Button;
    
  
    
 public class MainActivity extends AppCompatActivity {
    
  
    
     private Button mButtonOpen;//花生,做两个按钮,一开一关
    
     private Button mButtonClose;
    
  
    
     @Override
    
     protected void onCreate(Bundle savedInstanceState) {
    
     super.onCreate(savedInstanceState);
    
     setContentView(R.layout.activity_main);
    
  
    
     //接下来绑定按钮变量和按钮组件
    
    mButtonOpen = (Button)findViewById(R.id.button1);
    
  
    
     mButtonOpen.setOnClickListener(new View.OnClickListener()
    
     {
    
         @Override
    
         public void onClick(View v)
    
         {
    
             new Thread(new Runnable() { // 匿名类的Runnable接口
    
                 @Override
    
                 public void run() {
    
                     try {
    
                         String resultJsonStr = SetDevicePropertyUtil.setDevicePropertyOpen();
    
                     } catch (Exception e) {
    
                         e.printStackTrace();
    
                     }
    
                 }
    
             }).start();
    
         }
    
     });
    
  
    
     mButtonClose = (Button)findViewById(R.id.button2);
    
  
    
     mButtonClose.setOnClickListener(new View.OnClickListener()
    
     {
    
         @Override
    
         public void onClick(View v)
    
         {
    
             new Thread(new Runnable() { // 匿名类的Runnable接口
    
                 @Override
    
                 public void run() {
    
                     try {
    
                         String resultJsonStr = SetDevicePropertyUtil.setDevicePropertyClose();
    
                     } catch (Exception e) {
    
                         e.printStackTrace();
    
                     }
    
                 }
    
             }).start();
    
         }
    
     });
    
  
    
     }
    
 }

四、测试

编写完成后,将APP下载到手机上。按下OPEN,小灯点亮;按下CLOSE,小灯熄灭。

至此,成功实现项目目标。

全部评论 (0)

还没有任何评论哟~