250318:数据发送队列优化
authorxiewei <892331662@qq.com>
Tue, 18 Mar 2025 10:10:57 +0000 (18:10 +0800)
committerxiewei <892331662@qq.com>
Tue, 18 Mar 2025 10:10:57 +0000 (18:10 +0800)
hssdk/src/main/java/com/zhixun/blesdk/hssdk/bttools/BleHandle.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/BleMsgReceiverHandler.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/BleMsgSendHandler.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/ReceiverLooperThread.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/entity/ChatAudio.java [new file with mode: 0644]

index 9190ba5..13fd8c0 100644 (file)
@@ -379,13 +379,15 @@ public class BleHandle {
 
             //这里只有扫描到服务后 才算连接成功
             for (BluetoothGattService se : gatt.getServices()) {
-                // SobLog.d(TAG, "discovered Service: " + se.getUuid() + " : " + se.getCharacteristics().toString());
-                if (BleUuidUtil.getInstance().getServiceUUID().toString().equals(se.getUuid().toString())) {
-                    SobLog.d(TAG, "BLE Discover Service Success!!!");
-                    deviceNotifyEnable();
-                    readInitMsg();
-
+                if(se!=null){
+                    SobLog.d(TAG, "discovered Service: " + se.getUuid() + " : " + se.getCharacteristics().toString());
+                    if (BleUuidUtil.getInstance().getServiceUUID().toString().equals(se.getUuid().toString())) {
+                        SobLog.d(TAG, "BLE Discover Service Success!!!");
+                        deviceNotifyEnable();
+                        readInitMsg();
+                    }
                 }
+
             }
         }
 
@@ -431,7 +433,7 @@ public class BleHandle {
 
             byte[] value = characteristic.getValue();
             SobLog.d(TAG, "onCharacteristicChanged--" + characteristic.getUuid().toString() + ":" +
-                    (value.length > 100 ? value.length : ByteTools.byteArrToHex(value)));
+                    (value.length > 500 ? value.length : ByteTools.byteArrToHex(value)));
             if (BleUuidUtil.getInstance().getNotifyUUID().toString().equals(characteristic.getUuid().toString())
                 ||BleUuidUtil.getInstance().getmAudioNotifyUUID().toString().equals(characteristic.getUuid().toString())) {
                 BleMsgReceiverHandler.getInstance().receiverData(value);
index 4a65043..cb2c79d 100644 (file)
@@ -60,7 +60,7 @@ public class BleMsgReceiverHandler {
         SobLog.d(TAG, "receiverData--" +
                 dirrction);
 
-        if(receiverLooperThread.isStartSendAudio()){
+        if(receiverLooperThread.isStartSendAudio()&&data[0]!=0x72){
             receiverLooperThread.sendAudio(data);
         }else {
             if(dirrction==0x72){
index c067b68..ea46835 100644 (file)
@@ -15,6 +15,8 @@ import com.zhixun.blesdk.hssdk.entity.MsgBody;
 import com.zhixun.blesdk.hssdk.util.SobLog;
 
 import java.util.ArrayList;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
 
 public class BleMsgSendHandler {
 
@@ -23,7 +25,7 @@ public class BleMsgSendHandler {
     private MsgLooperThread msgLooperThread;
     private static BleMsgSendHandler sInstance;
     public static final int TYPE_SET_PHONEINFO = 4098;//设置发送手机信息
-
+    private Queue<byte[]> queueMsg = new ConcurrentLinkedQueue<byte[]>();//线程安全到队列
     private int mtu = 20;
     public BleMsgSendHandler() {
         SobLog.d(TAG, "-----BleMsgManager---creat--" + msgLooperThread);
@@ -47,6 +49,7 @@ public class BleMsgSendHandler {
         if (msgLooperThread == null) {
             msgLooperThread = new MsgLooperThread();
             msgLooperThread.start();
+
         }
     }
 
@@ -120,7 +123,7 @@ public class BleMsgSendHandler {
                 sendData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
                         pageIndex,pageLen,crc);
             }
-            msgList.add(sendData);
+            queueMsg.add(sendData);
         }else {
             SobLog.d(TAG,"分包发送");
             //分包计算
@@ -140,7 +143,7 @@ public class BleMsgSendHandler {
             crc[0] = ByteTools.getLRC(firstContent);
             byte[] firstData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
                     pageIndex,pageLen,firstContent,crc);
-            msgList.add(firstData);
+            queueMsg.add(firstData);
 
             //添加剩余帧
             byte[] otherContent = new byte[otherPageSize];
@@ -151,13 +154,16 @@ public class BleMsgSendHandler {
                     otherData = new byte[otherPageSize%mtu];
                 }
                 System.arraycopy(otherContent,i*mtu,otherData,0,otherData.length);
-                msgList.add(otherData);
+                queueMsg.add(otherData);
             }
         }
-        if (msgList.size() != 0) {
+        if(!isStart){
+            mSendMsgHandle.sendEmptyMessage(MSG_SEND);
+        }
+        /*if (queueMsg.size() != 0) {
             mSendMsgHandle.removeMessages(MSG_CHECKSENDMSG);
             mSendMsgHandle.sendEmptyMessage(MSG_CHECKSENDMSG);
-        }
+        }*/
         if(callBack!=null){
             BleMsgReceiverHandler.getInstance().addCallBack(msgBody.getType(),callBack);
         }
@@ -172,15 +178,16 @@ public class BleMsgSendHandler {
     }
 
     public void clear(){
-        msgList.clear();
-        if(mSendMsgHandle!=null){
+        queueMsg.clear();
+        /*if(mSendMsgHandle!=null){
             mSendMsgHandle.removeMessages(MSG_SEND);
-        }
+        }*/
     }
 
-    private ArrayList<byte[]> msgList = new ArrayList<>();
+//    private ArrayList<byte[]> msgList = new ArrayList<>();
     private final int MSG_SEND = 101;//发送数据
-    private final int MSG_CHECKSENDMSG = 102;//检测普通数据
+    private boolean isStart = false;
+//    private final int MSG_CHECKSENDMSG = 102;//检测普通数据
     private class MsgLooperThread extends Thread {
 
         public void run() {
@@ -191,38 +198,45 @@ public class BleMsgSendHandler {
                     super.handleMessage(msg);
                     switch (msg.what) {
                         case MSG_SEND:
-
-                            byte[] data = (byte[]) msg.obj;
+                            isStart = true;
+                            byte[] data = queueMsg.poll();
+                            if(data==null){
+                                mSendMsgHandle.sendEmptyMessageDelayed(MSG_SEND,40);
+                                return;
+                            }
                             byte[] sendBytes = new byte[data.length-1];
                             System.arraycopy(data,1,sendBytes,0,sendBytes.length);
                             int channel = 0xFF & data[0];
                             SobLog.d(TAG, "MSG_SEND---" + ByteTools.byteArrToHex(sendBytes));
-
                             BleHandle.getInstance().deviceWrite(sendBytes,channel);
-
+                            mSendMsgHandle.sendEmptyMessageDelayed(MSG_SEND,40);
                             break;
-                        case MSG_CHECKSENDMSG:
-                            if (msgList.size() > 0) {
+                        /*case MSG_CHECKSENDMSG:
+                            if (queueMsg.size() > 0) {
                                 Message message = new Message();
                                 message.what = MSG_SEND;
-                                message.obj = msgList.get(0);
+                                message.obj = queueMsg.poll();
                                 mSendMsgHandle.sendMessage(message);
-                                msgList.remove(0);
+
                             }
-                            SobLog.d(TAG, "MSG_CHECKSENDMSG---" + msgList.size());
-                            if (msgList.size() > 0) {
+                            SobLog.d(TAG, "MSG_CHECKSENDMSG---" + queueMsg.size());
+                            if (queueMsg.size() > 0) {
                                 mSendMsgHandle.removeMessages(MSG_CHECKSENDMSG);
                                 mSendMsgHandle.sendEmptyMessageDelayed(MSG_CHECKSENDMSG,40);
                             } else {
                                 mSendMsgHandle.removeMessages(MSG_CHECKSENDMSG);
                             }
 
-                            break;
+                            break;*/
 
                     }
                 }
             };
             Looper.loop();
+            if(!isStart){
+                mSendMsgHandle.sendEmptyMessage(MSG_SEND);
+            }
+
         }
     }
 }
index 47282ba..8ebb763 100644 (file)
@@ -27,6 +27,7 @@ import com.zhixun.blesdk.hssdk.callback.OnTrainStateListener;
 import com.zhixun.blesdk.hssdk.callback.OnUnBondDeviceCallBack;
 import com.zhixun.blesdk.hssdk.entity.AppTime;
 import com.zhixun.blesdk.hssdk.data.bean.ReceiverData;
+import com.zhixun.blesdk.hssdk.entity.ChatAudio;
 import com.zhixun.blesdk.hssdk.entity.E3MsgBody;
 import com.zhixun.blesdk.hssdk.entity.E4MsgBody;
 import com.zhixun.blesdk.hssdk.entity.HsDevicePwd;
@@ -60,6 +61,8 @@ import org.json.JSONObject;
 
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.TimeZone;
@@ -558,9 +561,11 @@ public class ReceiverLooperThread extends Thread {
                                 }
                                 startSendAudio = false;
                                 if(iAiFunctionListener!=null){
+                                    audioCache = composeData(chatAudios);
                                     iAiFunctionListener.sendAudio(audioCache.length,audioCache);
                                 }
                                 audioCache = new byte[0];
+                                chatAudios.clear();
                                 break;
                             case TYPE_AI_CANCLE_CHAT:
                                 SobLog.d(TAG, "TYPE_AI_CANCLE_CHAT--");
@@ -720,9 +725,15 @@ public class ReceiverLooperThread extends Thread {
     }
 
     private byte[] audioCache = new byte[0];
+    private ArrayList<ChatAudio> chatAudios = new ArrayList<>();
     public synchronized void sendAudio(byte[] data){
         SobLog.d(TAG, "sendAudio---");
-        audioCache = ByteTools.concatAll(audioCache,data);
+        byte[] index = new byte[4];
+        byte[] audio = new byte[data.length-4];
+        System.arraycopy(data,0,index,0,index.length);
+        System.arraycopy(data,4,audio,0,audio.length);
+        chatAudios.add(new ChatAudio(ByteTools.little_bytesToInt(index),audio));
+//        audioCache = ByteTools.concatAll(audioCache,data);
 
     }
     public boolean isStartSendAudio() {
@@ -740,6 +751,25 @@ public class ReceiverLooperThread extends Thread {
 
     }
 
+    private byte[] composeData(ArrayList<ChatAudio> cacheData){
+        Collections.sort(cacheData, new Comparator<ChatAudio>() {
+            @Override
+            public int compare(ChatAudio b1, ChatAudio b2) {
+                int i = b1.getPosition() - b2.getPosition();
+                return i;
+            }
+        });
+
+        ChatAudio first = cacheData.get(0);
+        byte[] allContentB = new byte[0];
+        for (ChatAudio revdata:cacheData){
+            byte[] nextB = revdata.getData();
+            SobLog.d(TAG,"composeData--"+revdata.getPosition());
+            allContentB = ByteTools.concatAll(allContentB,nextB);
+        }
+        return allContentB;
+    }
+
     public void removeAudioTimeOut() {
         mReceiverMsgHandle.removeMessages(MSG_AUDIO_TIMEOUT);
     }
diff --git a/hssdk/src/main/java/com/zhixun/blesdk/hssdk/entity/ChatAudio.java b/hssdk/src/main/java/com/zhixun/blesdk/hssdk/entity/ChatAudio.java
new file mode 100644 (file)
index 0000000..7b3fdad
--- /dev/null
@@ -0,0 +1,27 @@
+package com.zhixun.blesdk.hssdk.entity;
+
+public class ChatAudio {
+    private int position;
+    private byte[] data;
+
+    public ChatAudio(int position, byte[] data) {
+        this.position = position;
+        this.data = data;
+    }
+
+    public int getPosition() {
+        return position;
+    }
+
+    public void setPosition(int position) {
+        this.position = position;
+    }
+
+    public byte[] getData() {
+        return data;
+    }
+
+    public void setData(byte[] data) {
+        this.data = data;
+    }
+}