import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
+import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.zhixun.blesdk.debug_tool.adapter.MsgAdapter;
import com.zhixun.blesdk.debug_tool.utils.FileHelper;
import com.zhixun.blesdk.debug_tool.utils.FuncationChosePopupWindow;
+import com.zhixun.blesdk.debug_tool.utils.HttpUtil;
import com.zhixun.blesdk.hssdk.HsAgentSdk;
import com.zhixun.blesdk.hssdk.callback.IFileReceiverCallback;
import com.zhixun.blesdk.hssdk.callback.ISendFileListener;
+import com.zhixun.blesdk.hssdk.callback.OnMsgSendCallBack;
+import com.zhixun.blesdk.hssdk.entity.E3MsgBody;
+import com.zhixun.blesdk.hssdk.entity.HsDeviceInfo;
+import com.zhixun.blesdk.hssdk.entity.MsgBody;
+import com.zhixun.blesdk.hssdk.entity.MsgResponse;
import com.zhixun.blesdk.hssdk.entity.OTAInfo;
+import org.json.JSONException;
+import org.json.JSONObject;
+
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
private ArrayList<String> msg;
private Button btn_ota_chose;
private Button btn_getLog;
+ private Button btn_check_ota;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
msg_list.setLayoutManager(layoutManager2);
btn_ota_chose = findViewById(R.id.btn_chose_ota);
btn_getLog = findViewById(R.id.btn_getlog);
+ btn_check_ota = findViewById(R.id.btn_check_ota);
msg = new ArrayList<>();
msgAdapter = new MsgAdapter(this,msg);
msg_list.setAdapter(msgAdapter);
getLog();
}
});
+ btn_check_ota.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ if(!HsAgentSdk.getInstance().getAPI().isConnected()){
+ Toast.makeText(OtaActivity.this,"请先连接设备",Toast.LENGTH_SHORT).show();
+ return;
+ }
+ E3MsgBody msgBody = new E3MsgBody(MsgBody.TYPE_GET_WATCH_INFO);
+ HsAgentSdk.getInstance().getAPI().sendMessage(msgBody, new OnMsgSendCallBack() {
+ @Override
+ public void onResponse(MsgResponse msgResponse) {
+ HsDeviceInfo deviceInfo = (HsDeviceInfo) msgResponse.getBody();
+ String version = deviceInfo.getMajor()+"."+deviceInfo.getMid()+"."+deviceInfo.getMinor();
+ //RL "http://iot.api.thisiot.com/ota/task?deviceId=%s&sourceVersion=%s"//获取升级url
+ String url = "http://iot.api.thisiot.com/ota/task?deviceId="+deviceInfo.getMacAddress().replace(":","")+"&sourceVersion="+version;
+ url = "http://iot.api.thisiot.com/ota/task/v2?deviceId=28110687C456&sourceVersion=1.1.38";
+ HttpUtil.CallHttp(url, new HttpUtil.HttpCallBack() {
+ @Override
+ public void onResponse(String datas) {
+
+ print_msg_to_end_of_view(datas);
+ //{"success":true,"msg":"发现新版本.","code":20007,"data":
+ // {"createdate":null,"customerno":"1","describe":"1.1.38到1.1.39","describeen":"1.1.39",
+ // "devicetype":"watch",
+ // "downurl":"http://r-oss.thisiot.com/device-ota/2024_08_31/1829817405184475138.fwpkg?Expires=1728532824&OSSAccessKeyId=LTAI5tJa2arDBmqSUsSTQJ1T&Signature=01CoA%2FFuaZHP5Sb1wZusGonlVkQ%3D","filesize":"5274448","force":0,"id":0,"isgw":0,"upgradtype":2,"version":"1.1.39"}}
+ try {
+ JSONObject jsonObject = new JSONObject(datas);
+ if(jsonObject.getBoolean("success")){
+ JSONObject data = jsonObject.getJSONObject("data");
+ if(data.has("downurl")){
+ String downurl = data.getString("downurl");
+ if(!TextUtils.isEmpty(downurl)){
+ HttpUtil.DoDownLoad(downurl, new HttpUtil.HttpCallBack() {
+ @Override
+ public void onResponse(String data) {
+ print_msg_to_end_of_view("下载完成:"+data);
+ startOta(data);
+ }
+
+ @Override
+ public void onprogress(int progress) {
+ print_msg_to_end_of_view("文件下载进度:"+progress);
+ }
+
+ @Override
+ public void onFail() {
+
+ }
+ });
+ }
+ }
+ }
+
+ } catch (JSONException e) {
+ Log.d(TAG,"JSONException--"+e.getMessage());
+ }
+ }
+
+ @Override
+ public void onprogress(int progress) {
+
+ }
+
+ @Override
+ public void onFail() {
+ Toast.makeText(OtaActivity.this,"检测失败",Toast.LENGTH_SHORT).show();
+ }
+ });
+ }
+
+ @Override
+ public void onFailure(int code, String msg) {
+
+ }
+ });
+ }
+ });
}
private int REQUEST_CODE_FILE_PICKER = 1001;
// 启动文件选择器
protected void print_msg_to_end_of_view(String msg)
{
- msgAdapter.addData(msg);
- msg_list.scrollToPosition(msgAdapter.getCount()-1);
+ runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ msgAdapter.addData(msg);
+ msg_list.scrollToPosition(msgAdapter.getCount()-1);
+ }
+ });
+
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
finish();
}
- private void getLog()
- {
+ private void getLog() {
HsAgentSdk.getInstance().getAPI().getDeviceLog(new IFileReceiverCallback() {
@Override
public void onProgressChange(String progress) {
--- /dev/null
+package com.zhixun.blesdk.debug_tool.utils;
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.zhixun.blesdk.hssdk.util.SpUtils;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
+
+public class HttpUtil {
+
+
+ public interface HttpCallBack{
+ void onResponse(String data);
+ void onprogress(int progress);
+ void onFail();
+ }
+ public static void CallHttp(String geturl,HttpCallBack callBack) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ int responseCode = -1;
+ String data = "";
+ try {
+ URL url = new URL(geturl);
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setConnectTimeout(10000);
+ connection.setRequestMethod("GET");
+ connection.setReadTimeout(30*1000);
+
+ responseCode = connection.getResponseCode();
+ //请求成功 获得返回的流
+ InputStream inputStream = connection.getInputStream();
+ data = readStreamToString(inputStream);
+ Log.e("HttpUtil", "请求返回--value:" + data);
+ callBack.onResponse(data);
+ inputStream.close();
+ } catch (MalformedURLException e) {
+ //Looper.prepare();
+ responseCode = -1009;
+ callBack.onFail();
+ Log.d("HttpUtil", "请求失败--MalformedURLException--"+e.getMessage());
+ e.printStackTrace();
+ //Looper.loop();
+ } catch (IOException e) {
+ //Looper.prepare();
+ Log.d("HttpUtil", "请求失败--IOException--"+e.getMessage());
+ e.printStackTrace();
+ callBack.onFail();
+ //Looper.loop();
+ }
+ Log.e("HttpUtil", "responseCode--" + responseCode);
+
+ Log.d("HttpUtil","请求结果--responseCode-"+responseCode+"--data--"+data);
+
+ }
+
+ }).start();
+ }
+
+ private static String downLoadPath = SpUtils.getInstance().getSdkPath()+"hsble_sdk/download/";
+
+ public static void DoDownLoad(String downloadurl,HttpCallBack callBack) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ File file1 = new File(downLoadPath);
+ if (!file1.exists()) {
+ boolean state=file1.mkdirs(); // 如果不存在则创建目录
+ Log.d("HttpUtil","文件创建"+(state?"成功":"失败")+"-"+downLoadPath);
+ }
+
+
+ String path = downLoadPath + "device_ota.fwpkg";
+
+
+
+ File file2 = new File(downLoadPath);
+ if (!file2.exists()){
+ boolean state = file2.mkdirs();
+ Log.d("HttpUtil","文件创建"+(state?"成功":"失败")+"-"+downLoadPath);
+ }
+
+ try {
+ //构建URL地址
+ URL url = new URL(downloadurl);
+ //连接
+ HttpURLConnection hcont = (HttpURLConnection) url.openConnection();
+ //建立实际链接
+ hcont.connect();
+ //获取输入流内容
+ InputStream is = hcont.getInputStream();
+ //获取文件长度
+ int fileLen = hcont.getContentLength();
+ Log.d("HttpUtil", "---下载中---lenth--"+fileLen);
+ //写入文件
+ OutputStream os = new FileOutputStream(path);
+ int length;
+ int lengtsh = 0;
+ byte[] bytes = new byte[fileLen];
+ while ((length = is.read(bytes)) != -1) {
+ os.write(bytes, 0, length);
+ //获取当前进度值
+ lengtsh += length;
+ float percentage = ( (float)lengtsh/ (float)fileLen);
+ int progress = (int)(percentage*100);
+ if(progress%10==0){
+ Log.d("HttpUtil", "---下载进度---"+progress);
+ }
+ if(progress<100){
+ if(callBack!=null){
+ callBack.onprogress(progress);
+ }
+ }
+
+ }
+ if(callBack!=null){
+ callBack.onResponse(path);
+ }
+ Log.d("HttpUtil", "---下载完成---"+path);
+ //关闭流
+ is.close();
+ os.close();
+ os.flush();
+
+
+ } catch (IOException e) {
+ // Looper.prepare();
+ if(callBack!=null){
+ callBack.onFail();
+ }
+ Log.d("HttpUtil", "文件读取异常--"+e.getMessage());
+ e.printStackTrace();
+ // Looper.loop();
+ }
+
+
+ }
+ }).start();
+ }
+
+
+
+ public static void downloadFile(String downlaodUrl, String savePath,String fileName){
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+
+ String path = savePath;
+
+ File file2 = new File(path);
+ if (!file2.exists())// 判断当前目录是否存在,存在返回true,否则返回false
+ file2.mkdirs(); // 如果不存在则创建目录
+ try {
+ //构建URL地址
+ URL url = new URL(downlaodUrl);
+ //连接
+ HttpURLConnection hcont = (HttpURLConnection) url.openConnection();
+ //建立实际链接
+ hcont.connect();
+ //获取输入流内容
+ InputStream is = hcont.getInputStream();
+ //获取文件长度
+ int fileLen = hcont.getContentLength();
+ Log.d("HttpUtil", "---下载中---lenth--"+fileLen);
+ //写入文件
+ OutputStream os = new FileOutputStream(path+fileName);
+ int length;
+ int lengtsh = 0;
+ byte[] bytes = new byte[fileLen];
+ while ((length = is.read(bytes)) != -1) {
+ os.write(bytes, 0, length);
+ //获取当前进度值
+ lengtsh += length;
+ float percentage = ( (float)lengtsh/ (float)fileLen);
+ int progress = (int)(percentage*100);
+ if(progress%10==0){
+ Log.d("HttpUtil", "---下载进度---"+progress);
+ }
+ /*if(progress<100){
+ callBack
+ }*/
+
+ }
+ /* if(onDownloadProgress!=null){
+ onDownloadProgress.onProgressChange(100);
+ }*/
+ Log.d("HttpUtil", "---下载完成---"+path);
+ //关闭流
+ is.close();
+ os.close();
+ os.flush();
+
+ } catch (IOException e) {
+ // Looper.prepare();
+ /*if(onDownloadProgress!=null){
+ onDownloadProgress.onProgressChange(-1);
+ }*/
+ Log.d("HttpUtil", "文件读取异常--"+e.getMessage());
+ e.printStackTrace();
+ // Looper.loop();
+ }
+
+ }
+ }).start();
+ }
+
+
+ private static String readStreamToString(InputStream inputStream) throws IOException {
+ //创建字节数组输出流 ,用来输出读取到的内容
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ //创建读取缓存,大小为1024
+ byte[] buffer = new byte[1024];
+ //每次读取长度
+ int len = 0;
+ //开始读取输入流中的文件
+ while ((len = inputStream.read(buffer)) != -1) { //当等于-1说明没有数据可以读取了
+ byteArrayOutputStream.write(buffer, 0, len); // 把读取的内容写入到输出流中
+ }
+ //把读取到的字节数组转换为字符串
+ String result = byteArrayOutputStream.toString();
+
+ //关闭输入流和输出流
+ inputStream.close();
+ byteArrayOutputStream.close();
+ //返回字符串结果
+ return result;
+ }
+
+
+
+}