阅读742 返回首页    go 微信


使用说明-java__SDK使用参考_服务器端API_阿里云物联网套件-阿里云

JAVA SDK使用说明

公共参数

名称 类型 是否必须 描述
accessKeyId String 必须 阿里云的Access Key ID
accessKeySecret String 必须 阿里云的Access Key Secret

引入SDK

*1 maven坐标:

  1. <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-iot -->
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-iot</artifactId>
  5. <version>2.1.1</version>
  6. </dependency>

依赖公共包:

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>2.4.2</version>
  5. </dependency>

*2 sdk源码地址参考-github https://github.com/aliyun/aliyun-openapi-java-sdk/tree/master/aliyun-java-sdk-iot

初始化

  1. String accessKey = "<your accessKey>";
  2. String accessSecret = "<your accessSecret>";
  3. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKey, accessSecret);
  4. DefaultAcsClient client = new DefaultAcsClient(profile);//初始化SDK客户端

发起调用

以推送数据到设备为例

  1. RevertRpcRequest rpcRequest = new RevertRpcRequest();
  2. rpcRequest.setDeviceName("11a936267d2a4b6eb7b4cb8549fc1fa7");//设备接入时候得到ID
  3. rpcRequest.setProductKey(ProductKey);//设备接入时候填写的ProductKey
  4. rpcRequest.setTimeOut(5000); //超时时间,单位毫秒.如果超过这个时间设备没反应则返回"TIMEOUT"
  5. rpcRequest.setRpcContent("aGVsbG8gd29ybGQ=");//推送给设备的数据.数据要求二进制数据做一次BASE64编码.(示例里面是"helloworld"编码后的值)
  6. RevertRpcResponse rpcResponse = client.getAcsResponse(rpcRequest);
  7. System.out.println(rpcResponse.getResponseContent());//得到设备返回的数据信息.
  8. System.out.println(rpcResponse.getRpcCode());//对应的响应码( TIMEOUT/SUCCESS/OFFLINE等)

最后更新:2016-12-20 11:02:13

  上一篇:go 总体说明__服务器端API_阿里云物联网套件-阿里云
  下一篇:go 使用说明-php__SDK使用参考_服务器端API_阿里云物联网套件-阿里云