//这里只有扫描到服务后 才算连接成功
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();
+ }
}
+
}
}
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);
import com.zhixun.blesdk.hssdk.util.SobLog;
import java.util.ArrayList;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
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);
if (msgLooperThread == null) {
msgLooperThread = new MsgLooperThread();
msgLooperThread.start();
+
}
}
sendData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
pageIndex,pageLen,crc);
}
- msgList.add(sendData);
+ queueMsg.add(sendData);
}else {
SobLog.d(TAG,"分包发送");
//分包计算
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];
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);
}
}
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() {
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);
+ }
+
}
}
}
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;
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;
}
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--");
}
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() {
}
+ 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);
}