Prechádzať zdrojové kódy

苹果安卓蓝牙接收数据,关门时检测三次夹手

DESKTOP-SADJPBG\47462 5 rokov pred
rodič
commit
dde83696fc

+ 5 - 0
app/build.gradle

xqd xqd
@@ -25,6 +25,10 @@ android {
         versionCode rootProject.ext.android.versionCode
         versionName rootProject.ext.android.versionName
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+        ndk {
+// 设置支持的SO库架构 注意如果是虚拟机使用的话需要吧x86加上去 否则无法运行的
+            abiFilters 'armeabi'  //, ‘x86’, ‘armeabi-v7a’, ‘x86_64’, ‘arm64-v8a’}
+        }
 
     }
     dataBinding {
@@ -66,4 +70,5 @@ dependencies {
     implementation 'com.github.maybesix:Android-XHLibrary:v1.0.0'
 
     implementation  'com.tencent.bugly:crashreport:3.1.0'
+    implementation  'com.tencent.bugly:nativecrashreport:latest.release'
 }

+ 12 - 13
app/src/main/java/com/siwei/recyclebox/application/AppApplication.java

xqd xqd xqd xqd
@@ -1,7 +1,6 @@
 package com.siwei.recyclebox.application;
 
-import android.Manifest;
-import android.app.ActivityManager;
+
 import android.app.AlarmManager;
 import android.app.Application;
 import android.app.PendingIntent;
@@ -66,14 +65,14 @@ import com.tencent.bugly.crashreport.CrashReport;
 
 public class AppApplication extends BaseApplication implements CustomActivityOnCrash.EventListener{
 
-    private TextToSpeech mTextToSpeech;
     private String TAG = "Application";
     @Override
     public void onCreate() {
         super.onCreate();
         SerialPortUtil.getInstance().init(this);        //初+始化串口
-        CrashReport.initCrashReport(getApplicationContext(), "40c3954b93", true);//测试阶段为true  发布时改成false
 
+        CrashReport.initCrashReport(getApplicationContext(), "40c3954b93", true);//测试阶段为true  发布时改成false
+        CrashReport.setUserId(this,MainViewModel.getDeviceId(this));
         Log.i(TAG,"AppApplication------------------------");
         //是否开启打印日志
         KLog.init(BuildConfig.DEBUG);
@@ -137,7 +136,7 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
                     public void run() {
                         initIoTDynamic();
                     }
-                },30000);
+                },300000);
             }
             @Override
             public void onInitDone(Object data) {
@@ -230,19 +229,19 @@ public class AppApplication extends BaseApplication implements CustomActivityOnC
                 .errorDrawable(R.mipmap.ic_launcher) //错误图标
                 .restartActivity(MainActivity.class) //重新启动后的activity
 //                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
-                .eventListener(this) //崩溃后的错误监听
+//                .eventListener(this) //崩溃后的错误监听
                 .apply();
 
     }
     @Override
     public void onLaunchErrorActivity() {
-        Log.i(TAG,"1111+++");
-            Intent mStartActivity = new Intent(this,MainActivity.class);
-            int mPendingIntentId = 123456;
-            PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
-            AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
-            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
-            System.exit(0);
+//        Log.i(TAG,"1111+++");
+//            Intent mStartActivity = new Intent(this,MainActivity.class);
+//            int mPendingIntentId = 123456;
+//            PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+//            AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
+//            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+//            System.exit(0);
 
     }
 

+ 0 - 4
app/src/main/java/com/siwei/recyclebox/deviceUtils/BaseDeviceEntity.java

xqd xqd
@@ -81,7 +81,6 @@ public abstract class BaseDeviceEntity {
 //                weightBytes[1] = data[4];
 //                latestWeight = ByteUtil.byte2short(weightBytes);
             }
-
             @Override
             public void onRunError(Exception e) {
                 e.printStackTrace();
@@ -90,9 +89,6 @@ public abstract class BaseDeviceEntity {
             }
         });
         executorService.execute(inputOutputManager);*/
-
-
-
     }
 
     abstract void onDataReceived(byte[] response);

+ 40 - 15
app/src/main/java/com/siwei/recyclebox/deviceUtils/OtherDevice.java

xqd xqd xqd xqd xqd
@@ -10,6 +10,7 @@ import com.hoho.android.usbserial.driver.UsbSerialPort;
 import android.os.Handler;
 
 import java.io.IOException;
+import java.util.Spliterator;
 import java.util.logging.LogRecord;
 
 import me.goldze.mvvmhabit.utils.SPUtils;
@@ -99,6 +100,7 @@ public class OtherDevice extends BaseDeviceEntity {
 
     private Handler mHandler=new Handler(msg -> {
 
+
        return true;
     });
 
@@ -178,7 +180,18 @@ public class OtherDevice extends BaseDeviceEntity {
     public  Integer queryInfrared(){
         try {
             SPUtils.getInstance().put("RunningState","0");
-            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 150);//
+//            Thread thread=new Thread(new Runnable() {
+//                @Override
+//                public void run() {
+//                    try {
+//                        port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);//
+//                    } catch (IOException e) {
+//                        e.printStackTrace();
+//                    }
+//                }
+//            });
+//            thread.start();
+            port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x55, 0x03, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xCC, (byte) 0xDD}, 100);//
             byte[] response = new byte[12];
             int readLen = port.read(response,200);
             System.out.println("读取红外返回:"+ByteUtil.bytesToString(response));
@@ -211,15 +224,16 @@ public class OtherDevice extends BaseDeviceEntity {
                     SPUtils.getInstance().put("RunningState","0");
                     SPUtils.getInstance().put("RodSwitch","0");
                     //门  66后面 01 表示杆 01表示伸杆  1c表示多少秒  02 1c 表示电流
-                    byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x0B, (byte) 0x02,0x1C, (byte) 0xCC, (byte) 0xDD};
-                    Log.i(TAG,"关门"+ByteUtil.bytesToString(pushRodOrderBytes));
+                    byte[] pushRodOrderBytes = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x0B, (byte) 0xff,0x1C, (byte) 0xCC, (byte) 0xDD};
+                    port.write(pushRodOrderBytes, 100);
 //                    byte[] pushRodOrderBytes2 = new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x01,0x1c, (byte) 0xFF,0x1c, (byte) 0xCC, (byte) 0xDD};
 //                    port.write(pushRodOrderBytes2, 100);
-                    port.write(pushRodOrderBytes, 100);
                     getRodOpenNumber();
                     byte[] response = new byte[ 12];
                     port.read(response,200);
                     System.out.println("push response:"+ByteUtil.bytesToString(response));
+//                    SPUtils.getInstance().put("infraredNum1",0);
+//                    SPUtils.getInstance().getInt("infraredNum1",0);
                     long curr=System.currentTimeMillis();
                     while (System.currentTimeMillis() - curr < 8000) {
                         Thread.sleep(300);
@@ -228,20 +242,31 @@ public class OtherDevice extends BaseDeviceEntity {
                             //停止关门
                             Log.i(TAG, "停止关门");
                             port.write(new byte[]{(byte) 0xAA, (byte) 0xBB, 0x0A, 0x01, 0x66, 0x01, 0x03, (byte) 0x02, 0x05, (byte) 0x1C, (byte) 0xCC, (byte) 0xDD}, 200);
-                            mHandler.postDelayed(new Runnable() {
-                                @Override
-                                public void run() {
-                                    try {
-                                        port.write(pushRodOrderBytes, 100);
-                                    } catch (IOException e) {
-                                        e.printStackTrace();
+                            int num=SPUtils.getInstance().getInt("infraredNum1")+1;
+                            SPUtils.getInstance().put("infraredNum1",num);
+                            if(num<3){
+                                mHandler.postDelayed(new Runnable() {
+                                    @Override
+                                    public void run() {
+                                            pushRod();
                                     }
-                                }
-                            },10000);
-                            break;
+                                },10000);
+                                break;
+                            }else {
+                                mHandler.postDelayed(new Runnable() {
+                                    @Override
+                                    public void run() {
+                                        try {
+                                            port.write(pushRodOrderBytes, 100);
+                                        } catch (IOException e) {
+                                            e.printStackTrace();
+                                        }
+                                    }
+                                },10000);
+                                break;
+                            }
                         }
                     }
-
                 }catch (Exception e ){
                     MainViewModel.reportDeviceEvent("pushRod");
                     e.printStackTrace();

+ 6 - 6
app/src/main/java/com/siwei/recyclebox/service/MyService.java

xqd
@@ -32,12 +32,12 @@ public class MyService extends Service {
 //            Intent intent=new Intent(this,MyService.class);
 //            this.startService(intent);
 
-        Intent mStartActivity = new Intent(this, MainActivity.class);
-        int mPendingIntentId = 123456;
-        PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
-        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
-        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
-        System.exit(0);
+//        Intent mStartActivity = new Intent(this, MainActivity.class);
+//        int mPendingIntentId = 123456;
+//        PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
+//        AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
+//        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
+//        System.exit(0);
         super.onDestroy();
 //        Toast.makeText(this, "服务已经停止", Toast.LENGTH_LONG).show();
 //        Log.i("","服务已经停止");

+ 187 - 105
app/src/main/java/com/siwei/recyclebox/ui/main/MainViewModel.java

xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd xqd
@@ -9,6 +9,7 @@ import android.app.PendingIntent;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothGatt;
+import android.bluetooth.BluetoothGattCallback;
 import android.bluetooth.BluetoothGattCharacteristic;
 import android.bluetooth.BluetoothGattDescriptor;
 import android.bluetooth.BluetoothGattServer;
@@ -169,6 +170,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onClick(View view) {
             System.out.println("btnClickqueryCurrent");
             Integer queryCurrentData=SerialPortUtil.getInstance().getOtherDevice().queryCurrent();
+            textqueryCurrentData.set(String.valueOf(queryCurrentData));
+//            int[] a=new int[2];
+//            for(int i=0;i<5;i++){
+//                a[i]=i;
+//            }
+//            avgWeight();
         }
     };
     public void onSerialPortDataReceived(ComPortData comPortData) {
@@ -197,8 +204,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                         AssetFileDescriptor fd = getApplication().getAssets().openFd("onPushRod.mp3");
                         mMediaPlay =new MediaPlayer();
                         mMediaPlay.reset();
-                        mMediaPlay.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
-                        mMediaPlay.prepare();
+                        mMediaPlay.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());mMediaPlay.prepare();
                         Log.i(TAG,"播放音频");
                         mMediaPlay.start();
                     } catch (IOException e) {
@@ -371,6 +377,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             System.out.println("btnClickPushRod");
 //            mHandler.sendEmptyMessage(2);
 //            SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
+
             pushRodMethodNonet();
         }
     };
@@ -423,6 +430,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
+                SPUtils.getInstance().put("infraredNum1",0);
                 SerialPortUtil.getInstance().getOtherDevice().pushRod();
             }
         },60000);
@@ -527,6 +535,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         }
     }
     private void pullonNetRodMethod(){
+        //有网开门
         SerialPortUtil.getInstance().getOtherDevice().pullRod();
         try {
             AssetFileDescriptor fd = getApplication().getAssets().openFd("netPullRod.mp3");
@@ -542,15 +551,16 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     }
     private void pushRodMethod(){
         //有网下关门
+        SPUtils.getInstance().put("infraredNum1",0);
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
 
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
-                int a=avgWeight();
-                SPUtils.getInstance().put("avgWeight",a);
+                avgWeight();
+
             }
-        },3000);
+        },1000);
 
         mHandler.postDelayed(new Runnable() {
             @Override
@@ -608,42 +618,50 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             public void run() {
                 reportProperty(2);
             }
-        },6500);
+        },4000);
     }
 
-    private int avgWeight(){
-        int weight = 0;
-        int [] arr = new int[]{};
-        int n=0;
-        long curr=System.currentTimeMillis();
-        while(System.currentTimeMillis()-curr<2000){
-            try {
-                sleep(200);
-                arr[n]=SerialPortUtil.getInstance().getWeightDevice().readWeight();
+    private void avgWeight(){
+        Thread thread=new Thread(new Runnable() {
+            @Override
+            public void run() {
+                int weight = 0;
+                int[] arr=new int[10];
+                int n;
+                for(n=0;n<10;n++){
+                        arr[n]=SerialPortUtil.getInstance().getWeightDevice().readWeight();
+//                    arr[n]=n;
+                    Log.i("n=", String.valueOf(n));
 //                arr[n]=SerialPortUtil.getInstance().getOtherDevice().readWeight();
-                Log.i("weight=", String.valueOf(arr[n]));
-                weight=arr[n]+weight;
-                n++;
-            } catch (InterruptedException e) {
-                e.printStackTrace();
+                    Log.i("weight=", String.valueOf(arr[n]));
+                    weight=arr[n]+weight;
+                    if(n==9){
+                        int dvalue1 = Math.abs(arr[9]-arr[8]);
+                        int dvalue2 = Math.abs(arr[9]-arr[7]);
+                        if(dvalue1<30&&dvalue2<30){
+                            Log.i("weight n-1=", String.valueOf(arr[9]));
+                            SPUtils.getInstance().put("avgWeight",arr[9]);
+                        }else {
+                            double v = weight / 10;
+                            int avg = new Double(v).intValue();
+                            Log.i("weight avg=", String.valueOf(avg));
+                            SPUtils.getInstance().put("avgWeight",avg);
+                        }
+                    }
+                }
             }
-        }
-        int dvalue1 = Math.abs(arr[n-1]-arr[n-2]);
-        int dvalue2 = Math.abs(arr[n-1]-arr[n-3]);
-        if(dvalue1<30&&dvalue2<30){
-            Log.i("weight n-1=", String.valueOf(arr[n-1]));
-            return arr[n-1];
-        }
-        double v = weight / arr.length;
-        int avg = new Double(v).intValue();
-        Log.i("weight avg=", String.valueOf(avg));
-        return avg;
+        });
+        thread.start();
+
+
     }
 
 
     private void pushRodMethodNonet(){
         //无网下关门
+        SPUtils.getInstance().put("infraredNum1",0);
         SerialPortUtil.getInstance().getOtherDevice().pushRod();//关门
+//        avgWeight();
         mHandler.postDelayed(new Runnable() {
             @Override
             public void run() {
@@ -662,13 +680,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             }
         },200);
 
-        mHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                int a=avgWeight();
-                SPUtils.getInstance().put("avgWeight",a);
-            }
-        },3000);
 
         mHandler.postDelayed(new Runnable() {
             @Override
@@ -710,6 +721,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 
 
     public Handler mHandler = new Handler(msg -> {
+
         return true;
     });
     public String IMEI = getDeviceId(getApplication());
@@ -1002,7 +1014,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onConnectStateChange(String connectId, ConnectState connectState) {
             System.out.println("onConnectStateChange  connectId:"+connectId+"  connectState:"+connectState);
             if(connectState == ConnectState.CONNECTED){
-                reportProperty(1);
+//                reportProperty(1);
                 int writePermission = ActivityCompat.checkSelfPermission(getApplication(),Manifest.permission.WRITE_EXTERNAL_STORAGE);
                 if(writePermission != PackageManager.PERMISSION_GRANTED){
                     //未授权写权限。
@@ -1145,7 +1157,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
    /*
    * 上报给蓝牙的数据格式
    * */
-    public JSONObject reportDataMethod(){
+    public JSONObject reportDataMethod(int num){
         Integer capacity=0;
         capacity=SerialPortUtil.getInstance().getOtherDevice().getDistance();//超声波距离/
         Integer magDoorSwitch=0;
@@ -1153,9 +1165,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         Integer currentTemperature=0;
         currentTemperature=SerialPortUtil.getInstance().getOtherDevice().geTemperature();//温度
         Integer weight = 0;
-//        weight=SerialPortUtil.getInstance().getWeightDevice().readWeight();
-        weight=avgWeight();
-        //        weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
+        if(num==2){
+            weight=SerialPortUtil.getInstance().getWeightDevice().readWeight();
+            //        weight=SerialPortUtil.getInstance().getOtherDevice().readWeight();//称重
+        }else if(num==1){
+            weight=SPUtils.getInstance().getInt("avgWeight");
+        }
 
         String  text = "称重 " + weight.toString() + ";距离 " + capacity.toString() + "mm;温度 " + currentTemperature+"度;门磁 "+magDoorSwitch+";杆状态 "+";锁状态 "+lockSwitch;
         Log.i(TAG, text + "=textReportData---------------------");
@@ -1183,9 +1198,9 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
             runningStateStr="0";
         }
         int runningStateInt=Integer.parseInt(runningStateStr);
-        JSONObject tem1 =new JSONObject();
-        tem1.put("V",IMEI);
-        tem1.put("I","IMEI");
+//        JSONObject tem1 =new JSONObject();
+//        tem1.put("V",IMEI);
+//        tem1.put("I","IMEI");
 
         JSONObject tem2 =new JSONObject();
         tem2.put("V",rodNumberInt);
@@ -1223,15 +1238,15 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         tem10.put("V",runningStateInt);
         tem10.put("I","RunningState");
 
-        locationMap.put("longitude", 107.31);
-        locationMap.put("latitude", 23.16);
-        locationMap.put("altitude", 523.1);
-        JSONObject tem11 =new JSONObject();
-        tem11.put("V",locationMap);
-        tem11.put("I","GeoLocation");
+//        locationMap.put("longitude", 107.31);
+//        locationMap.put("latitude", 23.16);
+//        locationMap.put("altitude", 523.1);
+//        JSONObject tem11 =new JSONObject();
+//        tem11.put("V",locationMap);
+//        tem11.put("I","GeoLocation");
 
         JSONArray array =new JSONArray();
-        array.add(tem1);
+//        array.add(tem1);
         array.add(tem2);
         array.add(tem3);
         array.add(tem4);
@@ -1241,7 +1256,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         array.add(tem8);
         array.add(tem9);
         array.add(tem10);
-        array.add(tem11);
+//        array.add(tem11);
 
         JSONObject reportData =new JSONObject();
         reportData.put("P",array);
@@ -1255,13 +1270,13 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     * 加密传给蓝牙的数据格式
     * */
     @RequiresApi(api = Build.VERSION_CODES.O)
-    public String reportDataMethodEncrypt() {
+    public String reportDataMethodEncrypt(int num) {
         JSONObject reportData =new JSONObject();
         reportData.put("T",SPUtils.getInstance().getString("type"));
         //加密数据
         String base64Str="";
         try {
-            base64Str= DemoOne.encrypt(reportDataMethod().toString()+"@", "1234567890123456", "0987654321098765");//加密数据
+            base64Str= DemoOne.encrypt(reportDataMethod(num).toString()+"@", "1234567890123456", "0987654321098765");//加密数据
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -1279,16 +1294,21 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
 //        hashMap.put("event",identifier);
         OutputParams params = new OutputParams(hashMap);
         Log.i("设备故障",identifier+"====="+params);
-        LinkKit.getInstance().getDeviceThing().thingEventPost(identifier, params, new IPublishResourceListener() {
-            @Override
-            public void onSuccess(String resId, Object o) { // 事件上报成功
-                Log.i("事件上报","上报成功");
-            }
-            @Override
-            public void onError(String resId, AError aError) { // 事件上报失败
-                Log.i("事件上报","上报失败");
-            }
-        });
+//        int networkInfo=netWorkInfo();
+
+//        if(netWorkInfo!=0){
+//
+//        }
+//        LinkKit.getInstance().getDeviceThing().thingEventPost(identifier, params, new IPublishResourceListener() {
+//            @Override
+//            public void onSuccess(String resId, Object o) { // 事件上报成功
+//                Log.i("事件上报","上报成功");
+//            }
+//            @Override
+//            public void onError(String resId, AError aError) { // 事件上报失败
+//                Log.i("事件上报","上报失败");
+//            }
+//        });
     }
 
     private void getDeviceProperty(String identifier){
@@ -1428,10 +1448,6 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         publishRequest.topic = "/a13H8L6bDyf/device2/user/checkOnlineStatus";
         publishRequest.payloadObj = "{\"id\":"+ publishRequest.msgId+", \"version\":\"1.0\",\"time\":" + System.currentTimeMillis() + "}";
         Integer networkInt=netWorkInfo();
-        if(networkInt!=0){
-            reportProperty(1);
-            Log.i(TAG,"循环上报数据");
-        }
         Log.i(TAG,"network网络"+networkInt);
             LinkKit.getInstance().publish(publishRequest, new IConnectSendListener() {
                 @Override
@@ -1442,6 +1458,10 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                         @Override
                         public void run() {
                             publish();
+                            if(networkInt!=0){
+                                reportProperty(1);
+                                Log.i(TAG,"循环上报数据");
+                            }
                         }
                     }, 1800000);
                 }
@@ -1514,13 +1534,30 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
             Log.d(TAG,"BLE广播开启成功");
         }
-
         @Override
         public void onStartFailure(int errorCode) {
             Log.e(TAG,"BLE广播开启失败,错误码:" + errorCode);
         }
     };
-
+//    private final BluetoothGattCallback mBluetoothGattCallback = new BluetoothGattCallback() {
+//        @Override
+//        public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
+//            super.onMtuChanged(gatt, mtu, status);
+//            Log.d("onMtuChanged"," onMtuChanged");
+//            if (BluetoothGatt.GATT_SUCCESS == status) {
+//                Log.d("BleService", "onMtuChanged success MTU = " + mtu);
+//            }else {
+//                Log.d("BleService", "onMtuChanged fail ");
+//            }
+//        }
+//    };
+//    private boolean requestMtu2(){
+//        if (bluetoothGatt != null) {
+//            // 25 *6 +3 =153
+//            return bluetoothGatt.requestMtu(517);
+//        }
+//        return false;
+//    }
     /*
     * 蓝牙连接,通信
     * */
@@ -1528,6 +1565,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
     private BluetoothGattServerCallback mBluetoothGattServerCallback = new BluetoothGattServerCallback() {
         @Override
         public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
+
+//            bluetoothGatt = device.connectGatt(getApplication(), true, mBluetoothGattCallback);
+//            Log.i("requestMTUU=",bluetoothGatt.requestMtu(25)+"");
+//            Log.i("requestMtu2=",requestMtu2()+"");
+//            onMtuChanged(device,517);
+
             Log.i(TAG, String.format("onConnectionStateChange:%s,%s,%s,%s", device.getName(), device.getAddress(), status, newState));
             Log.d(TAG,String.format(status == 0 ? (newState == 2 ? "与[%s]连接成功" : "与[%s]连接断开") : ("与[%s]连接出错,错误码:" + status), device));
         }
@@ -1543,22 +1586,32 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                 @RequiresApi(api = Build.VERSION_CODES.O)
                 @Override
                 public void run() {
+
                     String response=SPUtils.getInstance().getString("response");
+//                    +"asasasa15s1a5s1a651sa51s6a51s6a51s5a1s65a1sa51sa651sa561sa51s5a1s6a51s65a1s"
 //                    response="{\"encryptData\":\"81ypl9FQ9YyoyLyjPpd6BpvC0kT+QDGvKt9eQU8YjFhMuMpVsAMwSw/+kOFDhKKp8JEbFWKM3vTDZp5HhxIl0GloODH5RAutIFDNcLP1c13OrIfeFMmaOivV63omw5CbAMBQp0nuMXPbzHICiZ2HFJhcA0Ahm0g+R+4ze74ku4Ojbor+3s3Q4G3pF+6tnJelR/P4ovA81Ls0dvc1aBSC15hPjkD94j4mAsariqcj328zDQE9oXFb7I5k7z5jkAAVB4cm0qxnk/Np+KoEA1DU0TtQEjvN18Ak/kEND7hk5fU2xGcCmS6OMzltUk4Z3acaydWDaAfvZnChP/wUwXBcVmRdK50tgq8/70xhUWZT5WycOYmov7cO/F7gK1aP/u8hCNztYkG+sXyReXfHfFWk/0AtbT19+wUoyVgaihUfKI/knNSjqunBnVjksNKqRIKqWAUdQIarlxo+V7zdwsBcMnHQBCVeSGdG3OPfaEVMEOFopjOB7q7YLPd93AhYr/x9eQc6tRaZOR1dVgiq3hpxOD3uDCcumDjK88XcyvaD/EYV7rEukd8bTOF6lVEy3iCx+AT9GTqm123";
                     byte[] responseBytes = response.getBytes();
                     Log.i(TAG,"responseBytes.length=="+responseBytes.length);
                     Log.i(TAG,"offset="+offset);
-                    if(responseBytes.length>offset){
-                        int restLen = responseBytes.length-offset;
-                        byte[] bytesCopy = new byte[restLen>22?22:restLen];
-                        System.arraycopy(responseBytes,offset,bytesCopy,0,restLen>22?22:restLen);
-                        mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,offset,bytesCopy );// 响应客户端
-                        Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + bytesCopy.toString());
-                        Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + response);
-                    }else{
-                        mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,offset,responseBytes );// 响应客户端
-                         }
+                    if(SPUtils.getInstance().getInt("phoneType")==2){
+                        mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,responseBytes.length,responseBytes );// 响应客户端
+                    }else {
+                        if(responseBytes.length>offset){
+                            int restLen = responseBytes.length-offset;
+                            byte[] bytesCopy = new byte[restLen>22?22:restLen];
+                            System.arraycopy(responseBytes,offset,bytesCopy,0,restLen>22?22:restLen);
+                            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,offset,bytesCopy );// 响应客户端
+                            Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + bytesCopy.toString());
+                            Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + response);
+                        }else{
+                            mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,offset,responseBytes );// 响应客户端
+                        }
                     }
+
+//                    Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + bytesCopy.toString());
+//                        Log.d(TAG,"客户端读取Characteristic[" + characteristic.getUuid() + "]:\n" + response);
+//                    mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS,responseBytes.length,responseBytes );// 响应客户端
+                }
             }).start();
         }
         @Override
@@ -1576,11 +1629,11 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                     Log.i(TAG,"responseNeeded="+responseNeeded);
                     Log.i(TAG,"offset="+offset);
                     Log.i(TAG,"requestBytes="+requestBytes.toString());
-
                     String requestStr = new String(requestBytes);
                     System.out.println(requestStr);
                     Log.i(TAG,"changdu=="+requestStr.length());
                     String[] data=requestStr.split("[|]");
+
                         if(data[0].endsWith("fir3")) {
                             mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
                             if (data[1].equals("11")) {
@@ -1598,7 +1651,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                     public void run() {
                                         int numInt=0;
                                         long curr=System.currentTimeMillis();
-                                        while(System.currentTimeMillis()-curr<89000){
+                                        while(System.currentTimeMillis()-curr<finalTimeInt+5){
                                             try {
                                                 sleep(1000);
                                                 numInt++;
@@ -1606,7 +1659,11 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                                     break;
                                                 }
                                                 if(numInt== finalTimeInt){
-                                                    pushRodMethod();
+                                                    pushRodMethodNonet();
+                                                    SPUtils.getInstance().put("type","close");
+                                                    String response=reportDataMethodEncrypt(1);
+                                                    SPUtils.getInstance().put("response",response);
+                                                    onCharacteristicReadRequest(device,  requestId, offset, characteristic);
                                                     break;
                                                 }
                                             } catch (InterruptedException e) {
@@ -1619,6 +1676,7 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                                 thread.start();
                             }
                             if(data[1].equals("22")){
+                                mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
                                 try {
                                     SPUtils.getInstance().put("magDoorSwitch","1");
                                     SerialPortUtil.getInstance().getOtherDevice().openLock();
@@ -1673,45 +1731,65 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
                             }
                         }
                     if(requestStr.equals("close")){
+//                        avgWeight();
                         pushRodMethodNonet();
                             Log.i(TAG,"++++++收到321");
                             Log.i(TAG,"+++++关门");
                         SPUtils.getInstance().put("type","close");
-                        mHandler.postDelayed(new Runnable() {
-                            @Override
-                            public void run() {
-                                String response=reportDataMethodEncrypt();
-                                SPUtils.getInstance().put("response",response);
-                                onCharacteristicReadRequest(device,  requestId, offset, characteristic);
-                            }
-                        },6500);
-
+//                        mHandler.postDelayed(new Runnable() {
+//                            @Override
+//                            public void run() {
+//                                SPUtils.getInstance().put("type","close");
+//                                String response=reportDataMethodEncrypt(2);
+//                                SPUtils.getInstance().put("response",response);
+//                                onCharacteristicReadRequest(device,  requestId, offset, characteristic);
+//                            }
+//                        },3500);
+                        String response=reportDataMethodEncrypt(2);
+                        SPUtils.getInstance().put("response",response);
+                        onCharacteristicReadRequest(device,  requestId, offset, characteristic);
                     }
                     if(requestStr.equals("close2")){
+//                        avgWeight();
                         Log.i(TAG,"+++++收运门关");
                         SPUtils.getInstance().put("type","close");
-                        mHandler.postDelayed(new Runnable() {
-                            @Override
-                            public void run() {
-                                String response=reportDataMethodEncrypt();
-                                SPUtils.getInstance().put("response",response);
-                                onCharacteristicReadRequest(device,  requestId, offset, characteristic);
-                            }
-                        },500);
+//                        mHandler.postDelayed(new Runnable() {
+////                            @Override
+////                            public void run() {
+////                                SPUtils.getInstance().put("type","close");
+////                                String response=reportDataMethodEncrypt(2);
+////                                SPUtils.getInstance().put("response",response);
+////                                onCharacteristicReadRequest(device,  requestId, offset, characteristic);
+////                            }
+////                        },3500);
+                        String response=reportDataMethodEncrypt(2);
+                        SPUtils.getInstance().put("response",response);
+                        onCharacteristicReadRequest(device,  requestId, offset, characteristic);
+                    }
+                    if(requestStr.equals("link|1")){
+                        SPUtils.getInstance().put("phoneType",1);
+                        Log.i(TAG,"++++++收到123");
+                        Log.i(TAG,"+++++开门请求");
+                        Log.i(TAG,"+++++requestStrlength"+requestStr.length());
+                        SPUtils.getInstance().put("type","open");
+                        String response=reportDataMethodEncrypt(2);
+                        SPUtils.getInstance().put("response",response);
+                        onCharacteristicReadRequest(device,  requestId, offset, characteristic);
                     }
-                    if(requestStr.equals("link")){
+                    if(requestStr.equals("link|2")){
+                        SPUtils.getInstance().put("phoneType",2);
                         Log.i(TAG,"++++++收到123");
                         Log.i(TAG,"+++++开门请求");
                         Log.i(TAG,"+++++requestStrlength"+requestStr.length());
                         SPUtils.getInstance().put("type","open");
-                        String response=reportDataMethodEncrypt();
+                        String response=reportDataMethodEncrypt(2);
                         SPUtils.getInstance().put("response",response);
                         onCharacteristicReadRequest(device,  requestId, offset, characteristic);
                     }
                     Log.i(TAG, String.format("onCharacteristicWriteRequest:%s,%s,%s,%s,%s,%s,%s,%s", device.getName(), device.getAddress(), requestId, characteristic.getUuid(),
                             preparedWrite, responseNeeded, offset, requestStr));
                     Log.d(TAG,"客户端写入Characteristic[" + characteristic.getUuid() + "]write:\n" + requestStr);
-//                    mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
+                    mBluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, requestBytes);// 响应客户端
                 }
             }).start();
         }
@@ -1729,8 +1807,12 @@ public class MainViewModel extends BaseViewModel implements SerialPortHelper.OnS
         public void onNotificationSent(BluetoothDevice device, int status) {
             Log.i(TAG, String.format("onNotificationSent:%s,%s,%s", device.getName(), device.getAddress(), status));
         }
+
+
+
         @Override
         public void onMtuChanged(BluetoothDevice device, int mtu) {
+            super.onMtuChanged(device, 512);
             Log.i(TAG, String.format("onMtuChanged:%s,%s,%s", device.getName(), device.getAddress(), mtu));
         }