250417:添加乘车码逻辑
authorxiewei <892331662@qq.com>
Thu, 17 Apr 2025 10:31:48 +0000 (18:31 +0800)
committerxiewei <892331662@qq.com>
Thu, 17 Apr 2025 10:31:48 +0000 (18:31 +0800)
15 files changed:
hssdk/build.gradle
hssdk/src/main/AndroidManifest.xml
hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/NetworkStateReceiver.java [new file with mode: 0644]
hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/RideCodeManager.java [new file with mode: 0644]
hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/SocketClient.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/bttools/BleHandle.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/bttools/ByteTools.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/callback/OnSocketListener.java [new file with mode: 0644]
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/BleMsgSendHandler.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/FileHandler.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/FileHandlerE6.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/HsApiKit.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/data/ReceiverLooperThread.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/entity/MsgBody.java
hssdk/src/main/java/com/zhixun/blesdk/hssdk/util/BleUuidUtil.java

index a69a1ef..379021d 100644 (file)
@@ -12,14 +12,14 @@ android {
         versionCode 1
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
         consumerProguardFiles "consumer-rules.pro"
-        versionName '2.0.0'
+        versionName '2.1.0'
         buildConfigField("String", "SDK_VERSION", "\"${versionName}\"")
         multiDexEnabled true
     }
 
     buildTypes {
         release {
-            minifyEnabled true
+            minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
         }
     }
index a402b92..c500b79 100644 (file)
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
+
+    <application >
+        <receiver android:name=".alisocket.NetworkStateReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
+            </intent-filter>
+        </receiver>
+    </application>
 </manifest>
\ No newline at end of file
diff --git a/hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/NetworkStateReceiver.java b/hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/NetworkStateReceiver.java
new file mode 100644 (file)
index 0000000..95b9082
--- /dev/null
@@ -0,0 +1,37 @@
+package com.zhixun.blesdk.hssdk.alisocket;
+
+import static android.graphics.Color.BLACK;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+
+import com.zhixun.blesdk.hssdk.util.SobLog;
+
+public class NetworkStateReceiver extends BroadcastReceiver {
+    @Override
+    public void onReceive(Context context, Intent intent) {
+
+
+        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
+            NetworkInfo info =
+                    intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
+            if (info == null) {
+                return;
+            }
+            if (info.getState() == NetworkInfo.State.CONNECTED && info.isAvailable()) {
+                if (info.getType() == ConnectivityManager.TYPE_WIFI
+                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
+                    SobLog.d("NetworkStateReceiver","有网");
+                    RideCodeManager.getInstance().sendNetWorkState(true);
+                    return;
+                }
+            }
+            SobLog.d("NetworkStateReceiver","无网");
+            RideCodeManager.getInstance().sendNetWorkState(false);
+        }
+
+    }
+}
diff --git a/hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/RideCodeManager.java b/hssdk/src/main/java/com/zhixun/blesdk/hssdk/alisocket/RideCodeManager.java
new file mode 100644 (file)
index 0000000..744dfe8
--- /dev/null
@@ -0,0 +1,196 @@
+package com.zhixun.blesdk.hssdk.alisocket;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+
+import androidx.annotation.NonNull;
+
+import com.zhixun.blesdk.hssdk.bttools.BleHandle;
+import com.zhixun.blesdk.hssdk.bttools.ByteTools;
+import com.zhixun.blesdk.hssdk.callback.OnSocketListener;
+import com.zhixun.blesdk.hssdk.data.BleMsgReceiverHandler;
+import com.zhixun.blesdk.hssdk.data.BleMsgSendHandler;
+import com.zhixun.blesdk.hssdk.data.ReceiverLooperThread;
+import com.zhixun.blesdk.hssdk.entity.MsgBody;
+import com.zhixun.blesdk.hssdk.util.SobLog;
+
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Iterator;
+
+public class RideCodeManager implements OnSocketListener {
+    private static RideCodeManager sInstance;
+    private String TAG = "RideCodeManager";
+
+    private RideCodeMsgLooperThread rideCodeMsgLooperThread;
+    private SocketClient client;
+    public static RideCodeManager getInstance() {
+        if (sInstance == null) {
+            synchronized (RideCodeManager.class) {
+                if (null == sInstance)
+                    sInstance = new RideCodeManager();
+            }
+        }
+
+        return sInstance;
+    }
+
+    public void init(){
+        if (rideCodeMsgLooperThread == null) {
+            rideCodeMsgLooperThread = new RideCodeMsgLooperThread();
+            rideCodeMsgLooperThread.start();
+        }
+    }
+
+    public void receiverMsg(byte[] msg){
+
+    }
+
+    public void iPInetAddress(String domain)
+    {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                String ipv4Address = "";
+                try {
+                    InetAddress[] inetAddresses = InetAddress.getAllByName(domain);
+                    for (InetAddress inetAddress : inetAddresses) {
+                        if (inetAddress instanceof Inet4Address) {
+                            ipv4Address = inetAddress.getHostAddress();
+                            break;
+                        }
+                    }
+                } catch (UnknownHostException e) {
+                    e.printStackTrace();
+                }
+                if(ipv4Address == null || ipv4Address.isEmpty()){
+                    SobLog.d(TAG,"ipv4Address is null");
+                    return;
+                }
+                sendIpRespons(getDnsResponseFrame(ipv4Address));
+            }
+        }).start();
+
+    }
+
+    /**
+     * 手机响应dns解析帧value结构
+     *
+     * @param ipv4Address ipv4信息
+     * @return byte[]
+     */
+    private byte[] getDnsResponseFrame(String ipv4Address) {
+        SobLog.d(TAG,"sendIpRespons--"+ipv4Address);
+
+        byte[] ipByteList = new byte[4];
+        String[] ipSplitList = ipv4Address.split("\\.");
+        for (int i = 0; i < ipSplitList.length; i++) {
+            ipByteList[i] = (byte) Integer.parseInt(ipSplitList[i], 10);
+        }
+        return ipByteList;
+    }
+
+    public void connectClient(String address,int port){
+        SobLog.d(TAG,"connectClient--"+address+":"+port);
+        client = new SocketClient();
+        client.connect(address,port);
+        client.setSocketListener(this);
+    }
+
+    public void sendMsgToService(byte[] msg){
+        SobLog.d(TAG,"sendMsgToService--");
+        if(client!=null){
+            client.sendMessage(msg);
+        }
+    }
+
+    public void disconnect(){
+        if(client!=null){
+            client.disConnection();
+        }
+    }
+
+    private void sendIpRespons(byte[] ipv4Address){
+
+        byte[] data = ipv4Address;
+        MsgBody msgBody = new MsgBody();
+        msgBody.setService(1);
+        msgBody.setData(data);
+        msgBody.setType(MsgBody.TYPE_RIDECODE_DNS);
+        BleMsgSendHandler.getInstance().sendMsg(msgBody,null);
+    }
+
+    public void sendNetWorkState(boolean enable){
+
+        byte[] data = new byte[1];
+        data[0] = enable?(byte) (1 & 0xFF):(byte) (0 & 0xFF);
+        MsgBody msgBody = new MsgBody();
+        msgBody.setService(1);
+        msgBody.setData(data);
+        msgBody.setType(MsgBody.TYPE_RIDECODE_NETSTATE);
+        BleMsgSendHandler.getInstance().sendMsg(msgBody,null);
+        SobLog.d(TAG,"sendNetWorkState--");
+    }
+
+    public void sendConnectState(boolean enable){
+        byte[] data = new byte[1];
+        data[0] = enable?(byte) (1 & 0xFF):(byte) (0 & 0xFF);
+        MsgBody msgBody = new MsgBody();
+        msgBody.setService(1);
+        msgBody.setData(data);
+        msgBody.setType(MsgBody.TYPE_RIDECODE_CONNECT);
+        BleMsgSendHandler.getInstance().sendMsg(msgBody,null);
+        SobLog.d(TAG,"sendConnectState--");
+    }
+    private void sendSocketMsgToD(byte[] msg){
+        SobLog.d(TAG,"sendSocketMsgToD--");
+        MsgBody msgBody = new MsgBody();
+        msgBody.setService(1);
+        msgBody.setData(msg);
+        msgBody.setType(MsgBody.TYPE_RIDECODE_MSG);
+        BleMsgSendHandler.getInstance().sendMsg(msgBody,null);
+
+    }
+
+    @Override
+    public void onMessageReceived(byte[] message) {
+        SobLog.d(TAG,"onMessageReceived--"+ByteTools.byteArrToHex(message));
+        sendSocketMsgToD(message);
+    }
+
+    @Override
+    public void onConnectStateChange(boolean state) {
+        SobLog.d(TAG,"onConnectStateChange--");
+        sendConnectState(state);
+    }
+
+    @Override
+    public void onConnectionLost() {
+        SobLog.d(TAG,"onConnectionLost--");
+    }
+
+    private final int MSG_SEND = 101;//发送数据
+    private Handler msgHandler;
+    //    private final int MSG_CHECKSENDMSG = 102;//检测普通数据
+    private class RideCodeMsgLooperThread extends Thread {
+
+        public void run() {
+            Looper.prepare();
+            msgHandler = new Handler(Looper.myLooper()) {
+                @Override
+                public void handleMessage(@NonNull Message msg) {
+                    super.handleMessage(msg);
+                    switch (msg.what) {
+
+
+                    }
+                }
+            };
+            Looper.loop();
+
+
+        }
+    }
+}
index d6e751d..283a8ef 100644 (file)
@@ -1,5 +1,8 @@
 package com.zhixun.blesdk.hssdk.alisocket;
 
+import com.zhixun.blesdk.hssdk.callback.OnSocketListener;
+import com.zhixun.blesdk.hssdk.util.SobLog;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
@@ -15,9 +18,13 @@ public class SocketClient {
     private InputStream mInputStream = null;
     private BufferedReader reader = null;
     private PrintWriter writer = null;
-    private OnMessageReceivedListener messageListener;
+    private OnSocketListener onSocketListener;
     private String serverIpAddress;
     private int serverPort;
+    private String TAG = "SocketClient";
+    public void setSocketListener(OnSocketListener onSocketListener) {
+        this.onSocketListener = onSocketListener;
+    }
 
     public void setServerIpAddress(String serverIpAddress) {
         this.serverIpAddress = serverIpAddress;
@@ -27,20 +34,33 @@ public class SocketClient {
         this.serverPort = serverPort;
     }
 
-    public void connect() {
-        try {
-            socket = new Socket();
-            SocketAddress socAddress = new InetSocketAddress(serverIpAddress, serverPort);
-            socket.connect(socAddress, 2000);
-            // 启动心跳线程
-            socket.setKeepAlive(true);
-            mInputStream = socket.getInputStream();
-            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
-            writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
-            startReadMsg();
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
+    public void connect(String serverIpAddress,int serverPort) {
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    socket = new Socket();
+                    SocketAddress socAddress = new InetSocketAddress(serverIpAddress, serverPort);
+                    socket.connect(socAddress, 2000);
+                    // 启动心跳线程
+                    socket.setKeepAlive(true);
+                    mInputStream = socket.getInputStream();
+                    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+                    writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
+                    startReadMsg();
+                    if (onSocketListener != null) {
+                        onSocketListener.onConnectStateChange(true);
+                    }
+                    SobLog.d(TAG,"连接成功");
+                } catch (IOException e) {
+                    e.printStackTrace();
+                    SobLog.d(TAG,"连接失败"+e.getMessage());
+                    if (onSocketListener != null) {
+                        onSocketListener.onConnectStateChange(false);
+                    }
+                }
+            }
+        }).start();
     }
 
     public void startReadMsg() {
@@ -48,24 +68,27 @@ public class SocketClient {
             @Override
             public void run() {
 
-                byte[] arrayOfByte = new byte[1024];
+
                 try {
                     while (!socket.isClosed()) {
 
                         //方法三:基于字符的读取或基于字节的读取,如果bytesRead一直不返回-1,可以尝试替换以下方法
                         if (mInputStream.available() > 0) {
+                            SobLog.d(TAG,"receiver size--"+mInputStream.available());
+                            byte[] arrayOfByte = new byte[mInputStream.available()];
                             mInputStream.read(arrayOfByte);
-                            if (arrayOfByte.length > 0 && messageListener != null) {
+                            if (arrayOfByte.length > 0 && onSocketListener != null) {
                                 // Process the entire content at once
-                                messageListener.onMessageReceived(arrayOfByte);
+                                onSocketListener.onMessageReceived(arrayOfByte);
                             }
                         }
                     }
                 } catch (IOException e) {
                     e.printStackTrace();
                     // 处理连接中断的情况
-                    if (messageListener != null) {
-                        messageListener.onConnectionLost();
+                    SobLog.d(TAG,"连接中断"+e.getMessage());
+                    if (onSocketListener != null) {
+                        onSocketListener.onConnectionLost();
                     }
                 }
             }
@@ -74,28 +97,37 @@ public class SocketClient {
 
 
     public void sendMessage(byte[] message) {
-        if (socket != null && socket.isConnected()) {
-            writer.println(message);
-        }
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                if (socket != null && socket.isConnected()) {
+                    writer.println(message);
+                }
+            }
+        }).start();
+
     }
 
     public void disConnection() {
+        SobLog.d(TAG,"disConnection");
         try {
             if (socket != null && !socket.isClosed()) {
                 socket.close();
             }
-            reader.close();
-            writer.close();
+            if(reader!=null){
+                reader.close();
+            }
+            if(writer!=null){
+                writer.close();
+            }
+
+            socket = null;
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
 
-    public interface OnMessageReceivedListener {
-        void onMessageReceived(byte[] message);
 
-        void onConnectionLost();
-    }
 
 
 }
index 13fd8c0..9dbcee6 100644 (file)
@@ -22,6 +22,8 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
 import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
@@ -37,6 +39,7 @@ import androidx.core.content.ContextCompat;
 import com.google.gson.Gson;
 import com.zhixun.blesdk.hssdk.HsAgentSdk;
 
+import com.zhixun.blesdk.hssdk.alisocket.RideCodeManager;
 import com.zhixun.blesdk.hssdk.callback.BtVtransferCallback;
 import com.zhixun.blesdk.hssdk.callback.IBtScanCallback;
 import com.zhixun.blesdk.hssdk.callback.OnBleControlCallback;
@@ -385,6 +388,10 @@ public class BleHandle {
                         SobLog.d(TAG, "BLE Discover Service Success!!!");
                         deviceNotifyEnable();
                         readInitMsg();
+                    }else if (BleUuidUtil.getInstance().getRCServiceUUID().toString().equals(se.getUuid().toString())) {
+                        SobLog.d(TAG, "BLE Discover RC Service Success!!!");
+                        deviceRcNotifyEnable();
+
                     }
                 }
 
@@ -407,8 +414,6 @@ public class BleHandle {
                 //3D 00 01 00 00 01 61 22 33 44 09 11 00 00 05 05 00 01 00 01
                 //64 00 01 00 00 01 28 11 06 AF B0 F7 00 00 05 05 00 01 00 01
                 updataDevice(data);
-
-
                 SobLog.d(TAG, "mBleConnectDevice--" + new Gson().toJson(mBleConnectDevice));
                 sendPhoneInfo();
                 //读取成功后 需要下发同步基础信息  才能进行接下来的通信
@@ -435,7 +440,8 @@ public class BleHandle {
             SobLog.d(TAG, "onCharacteristicChanged--" + characteristic.getUuid().toString() + ":" +
                     (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())) {
+                ||BleUuidUtil.getInstance().getmAudioNotifyUUID().toString().equals(characteristic.getUuid().toString())
+                ||BleUuidUtil.getInstance().getRCNotifyUUID().toString().equals(characteristic.getUuid().toString())) {
                 BleMsgReceiverHandler.getInstance().receiverData(value);
             } else if (BleUuidUtil.getInstance().getFileNotifyUUID().toString().equals(characteristic.getUuid().toString())) {
 
@@ -672,7 +678,15 @@ public class BleHandle {
         mBluetoothGatt.disconnect();
     }
 
-
+    public  boolean isNetworkAvailable() {
+        ConnectivityManager connectivityManager =
+                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+        if (connectivityManager != null) {
+            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
+            return networkInfo != null && networkInfo.isConnected();
+        }
+        return false;
+    }
     private boolean isBinded = false;
     public void connectStateChange(int state) {
         SobLog.d(TAG, "connectStateChange--" + state);
@@ -700,6 +714,8 @@ public class BleHandle {
             }
             isConnected = true;
             SpUtils.getInstance().saveValue(SpUtils.KEY_LAST_DEVICE,new Gson().toJson(mBleConnectDevice));
+
+            RideCodeManager.getInstance().sendNetWorkState(isNetworkAvailable());
         }else {
             if(state==BtVtransferCallback.STATE_BLE_ISBINDED){
                 isBinded = true;
@@ -713,12 +729,12 @@ public class BleHandle {
             }
             isConnected = false;
             clear();
-
-
         }
 
     }
 
+
+
     public void setDeviceStor(long storage){
         mBleConnectDevice.setStorage(storage);
     }
@@ -751,6 +767,25 @@ public class BleHandle {
         return false;
     }
 
+    private void deviceRcNotifyEnable() {
+        if (!mBluetoothDeviceConnected) {
+            return;
+        }
+        if (!checkBluePermission())
+            return;
+        BluetoothGattService service = mBluetoothGatt.getService(BleUuidUtil.getInstance().getRCServiceUUID());//对应server端的setService方法
+
+        BluetoothGattCharacteristic characteristic = service.getCharacteristic(BleUuidUtil.getInstance().getRCNotifyUUID());//对应server端的setCharacteristic方法
+        if(characteristic!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic, true);
+        }
+
+        BluetoothGattCharacteristic characteristic2 = service.getCharacteristic(BleUuidUtil.getInstance().getRCWriteUUID());//对应server端的setCharacteristic方法
+        if(characteristic2!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic2, true);
+        }
+    }
+
     private void deviceNotifyEnable() {
         if (!mBluetoothDeviceConnected) {
             return;
@@ -765,31 +800,47 @@ public class BleHandle {
             return;
         }
         BluetoothGattCharacteristic characteristic = service.getCharacteristic(BleUuidUtil.getInstance().getNotifyUUID());//对应server端的setCharacteristic方法
-        mBluetoothGatt.setCharacteristicNotification(characteristic, true);
+        if(characteristic!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic, true);
+        }
 
 
         BluetoothGattCharacteristic characteristic2 = service.getCharacteristic(BleUuidUtil.getInstance().getReadUUID());//对应server端的setCharacteristic方法
-        mBluetoothGatt.setCharacteristicNotification(characteristic2, true);
+        if(characteristic2!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic2, true);
+        }
 
         BluetoothGattCharacteristic characteristic3 = service.getCharacteristic(BleUuidUtil.getInstance().getFileNotifyUUID());//对应server端的setCharacteristic方法
-        mBluetoothGatt.setCharacteristicNotification(characteristic3, true);
+        if(characteristic3!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic3, true);
+        }
 
         BluetoothGattCharacteristic characteristic4 = service.getCharacteristic(BleUuidUtil.getInstance().getmAudioNotifyUUID());//对应server端的setCharacteristic方法
-        mBluetoothGatt.setCharacteristicNotification(characteristic4, true);
+        if(characteristic4!=null){
+            mBluetoothGatt.setCharacteristicNotification(characteristic4, true);
+        }
+
     }
 
-    public void deviceWrite(byte[] msg, int type) {
+    public void deviceWrite(byte[] msg, int type,int service) {
         if(!mBluetoothDeviceConnected||mContext==null){
             return;
         }
-        UUID writeUUID = BleUuidUtil.getInstance().getWriteUUID(type);
-        SobLog.d(TAG, "---btsend-uuid--" + writeUUID.toString());
+
         new Thread(new Runnable() {
             @Override
             public void run() {
                 if (!mBluetoothDeviceConnected || !checkBluePermission()) return;
+                UUID writeUUID = BleUuidUtil.getInstance().getWriteUUID(type);
+                if(service==1){
+                    writeUUID = BleUuidUtil.getInstance().getRCWriteUUID();
+                }
+                SobLog.d(TAG, "---btsend-uuid--" + writeUUID.toString());
 
                 gattService = mBluetoothGatt.getService(BleUuidUtil.getInstance().getServiceUUID());//对应server端的setService方法
+                if(service==1){
+                    gattService = mBluetoothGatt.getService(BleUuidUtil.getInstance().getRCServiceUUID());
+                }
                 if(gattService==null){
                     SobLog.d(TAG,"deviceWrite gattService null");
                     disconnectDevice();
@@ -937,7 +988,7 @@ public class BleHandle {
     }
 
     public void sendE6Msg(byte[] data){
-        deviceWrite(data,6);
+        deviceWrite(data,6,0);
     }
 
 
index da3918a..7d40273 100644 (file)
@@ -589,5 +589,21 @@ public class ByteTools {
         return num & 0x1;
     }
 
+    public static int CRC16_XMODEM(byte[] buffer) {
+        int wCRCin = 0x0000;
+        int wCPoly = 0x1021;
+        for (byte b : buffer) {
+            for (int i = 0; i < 8; i++) {
+                boolean bit = ((b >> (7 - i) & 1) == 1);
+                boolean c15 = ((wCRCin >> 15 & 1) == 1);
+                wCRCin <<= 1;
+                if (c15 ^ bit)
+                    wCRCin ^= wCPoly;
+            }
+        }
+        wCRCin &= 0xffff;
+        return wCRCin ^= 0x0000;
+    }
+    
 
 }
diff --git a/hssdk/src/main/java/com/zhixun/blesdk/hssdk/callback/OnSocketListener.java b/hssdk/src/main/java/com/zhixun/blesdk/hssdk/callback/OnSocketListener.java
new file mode 100644 (file)
index 0000000..42ac4cb
--- /dev/null
@@ -0,0 +1,9 @@
+package com.zhixun.blesdk.hssdk.callback;
+
+public interface OnSocketListener {
+    void onMessageReceived(byte[] message);
+
+    void onConnectStateChange(boolean state);
+
+    void onConnectionLost();
+}
index b1cbf93..710d4b0 100644 (file)
@@ -101,7 +101,7 @@ public class BleMsgSendHandler {
             SobLog.d(TAG,"未匹配到数据类型");
             return;
         }
-        SobLog.d(TAG,"sendMsg--"+msgBody.toString());
+
         byte[] data = msgBody.getData();
         byte[] pageNum = new byte[4];
         byte[] pageTotalLen = new byte[4];
@@ -128,35 +128,74 @@ public class BleMsgSendHandler {
         }else {
             SobLog.d(TAG,"分包发送");
             //分包计算
-            int otherPageSize = data.length-mtu-18;
-            int otherPage = otherPageSize/mtu;
-            if(otherPageSize%mtu!=0){
-                otherPage++;
-            }
-            int allpage = otherPage+1;//得加上第一帧
-            pageNum = ByteTools.little_intToByte(allpage,pageNum.length);
+            if(msgBody.getService()==0){
+                int otherPageSize = data.length-mtu-18;
+                int otherPage = otherPageSize/mtu;
+                if(otherPageSize%mtu!=0){
+                    otherPage++;
+                }
+                int allpage = otherPage+1;//得加上第一帧
+                pageNum = ByteTools.little_intToByte(allpage,pageNum.length);
+
+                //添加第一帧
+                byte[] crc  = new byte[1];
+                byte[] firstContent = new byte[mtu-18];
+                System.arraycopy(data,0,firstContent,0,firstContent.length);
+
+                pageLen = ByteTools.little_intToByte(mtu-18,pageLen.length);
+                crc[0] = ByteTools.getLRC(firstContent);
+                byte[] firstData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
+                        pageIndex,pageLen,firstContent,crc);
+                SobLog.d(TAG,"firstData--"+ByteTools.byteArrToHex(firstData));
+                queueMsg.add(firstData);
+
+                //添加剩余帧
+                byte[] otherContent = new byte[otherPageSize];
+                System.arraycopy(data,firstContent.length,otherContent,0,otherContent.length);
+                for (int i=0;i<otherPage;i++){
+                    byte[] otherData = new byte[mtu];
+                    if(otherPageSize%mtu!=0&&i==otherPage-1){
+                        otherData = new byte[otherPageSize%mtu];
+                    }
+                    System.arraycopy(otherContent,i*mtu,otherData,0,otherData.length);
+                    byte channel = (byte) (msgBody.getChannel() & 0xFF);
+                    byte service = (byte) (msgBody.getService() & 0xFF);
+                    byte[] otherHead = new byte[2];
+                    otherHead[0] = channel;
+                    otherHead[1] = service;
+                    queueMsg.add(ByteTools.concatAll(otherHead,otherData));
+                }
+            }else {
+                int pageSize = (mtu-18);
+                int page = data.length/pageSize;
+                if(page%mtu!=0){
+                    page++;
+                }
+
+                pageNum = ByteTools.little_intToByte(page,pageNum.length);
+
+                for (int i=0;i<page;i++){
+                    byte[] childContent = new byte[pageSize];
+                    if(data.length%pageSize!=0&&i==page-1){
+                        childContent = new byte[data.length%pageSize];
+                    }
+                    System.arraycopy(data,i*pageSize,childContent,0,childContent.length);
+
+                    byte[] crc  = new byte[1];
+                    pageIndex = ByteTools.little_intToByte(i+1,pageIndex.length);
+
+                    pageLen = ByteTools.little_intToByte(childContent.length,pageLen.length);
+                    crc[0] = ByteTools.getLRC(childContent);
+
+                    byte[] childData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
+                            pageIndex,pageLen,childContent,crc);
+                    SobLog.d(TAG,"childData--"+ByteTools.byteArrToHex(childData));
+                    queueMsg.add(childData);
 
-            //添加第一帧
-            byte[] crc  = new byte[1];
-            byte[] firstContent = new byte[mtu-18];
-            System.arraycopy(data,0,firstContent,0,firstContent.length);
-            pageLen = ByteTools.little_intToByte(mtu-18,firstContent.length);
-            crc[0] = ByteTools.getLRC(firstContent);
-            byte[] firstData = ByteTools.concatAll(ByteTools.hexToByteArr(head),pageNum,pageTotalLen,
-                    pageIndex,pageLen,firstContent,crc);
-            queueMsg.add(firstData);
-
-            //添加剩余帧
-            byte[] otherContent = new byte[otherPageSize];
-            System.arraycopy(data,firstContent.length,otherContent,0,otherContent.length);
-            for (int i=0;i<otherPage;i++){
-                byte[] otherData = new byte[mtu];
-                if(otherPageSize%mtu!=0&&i==otherPage-1){
-                    otherData = new byte[otherPageSize%mtu];
                 }
-                System.arraycopy(otherContent,i*mtu,otherData,0,otherData.length);
-                queueMsg.add(otherData);
             }
+
+
         }
         if(!isStart){
             mSendMsgHandle.sendEmptyMessage(MSG_SEND);
@@ -172,7 +211,8 @@ public class BleMsgSendHandler {
 
     private String getDataHead(MsgBody msgBody){
         byte channel = (byte) (msgBody.getChannel() & 0xFF);
-        StringBuffer head = new StringBuffer(ByteTools.byteToHex(channel)+"73");
+        byte service = (byte) (msgBody.getService() & 0xFF);
+        StringBuffer head = new StringBuffer(ByteTools.byteToHex(channel)+ByteTools.byteToHex(service)+"73");
         byte[] typeB = ByteTools.little_intToByte(msgBody.getType(),2);
         head.append(ByteTools.byteArrToHex(typeB));
         return head.toString();
@@ -207,7 +247,7 @@ public class BleMsgSendHandler {
                                     byte[] element = iterator.next();
                                     byte[] head = new byte[3];
                                     System.arraycopy(element,0,head,0,3);
-                                    if (ByteTools.byteArrToHex(head).equals("73021F")) { // 如果是偶数
+                                    if (ByteTools.byteArrToHex(head).equals("73021F")) { // 如果是AI数据
                                         iterator.remove(); // 移除该元素
                                     }
                                 }
@@ -219,12 +259,14 @@ public class BleMsgSendHandler {
                                 mSendMsgHandle.sendEmptyMessageDelayed(MSG_SEND,40);
                                 return;
                             }
-
-                            byte[] sendBytes = new byte[data.length-1];
-                            System.arraycopy(data,1,sendBytes,0,sendBytes.length);
+                            SobLog.d(TAG, "data---" + ByteTools.byteArrToHex(data));
+                            byte[] sendBytes = new byte[data.length-2];
+                            System.arraycopy(data,2,sendBytes,0,sendBytes.length);
+                            SobLog.d(TAG, "sendBytes---" + ByteTools.byteArrToHex(sendBytes));
                             int channel = 0xFF & data[0];
+                            int service = 0xFF & data[1];
                             SobLog.d(TAG, "MSG_SEND---" + ByteTools.byteArrToHex(sendBytes));
-                            BleHandle.getInstance().deviceWrite(sendBytes,channel);
+                            BleHandle.getInstance().deviceWrite(sendBytes,channel,service);
                             mSendMsgHandle.sendEmptyMessageDelayed(MSG_SEND,40);
                             break;
                         /*case MSG_CHECKSENDMSG:
index 57820ad..1df3ca7 100644 (file)
@@ -617,7 +617,7 @@ public class FileHandler {
                                     msgSendState = false;
                                     sendSize++;
                                     SobLog.d(TAG,"sendSize--"+sendSize);
-                                    BleHandle.getInstance().deviceWrite(sendArr.get(sendSize),MsgBody.CHANNEL_E0);
+                                    BleHandle.getInstance().deviceWrite(sendArr.get(sendSize),MsgBody.CHANNEL_E0,0);
                                     BleHandle.getInstance().setOnSendStateChangeListener(new BleHandle.OnSendStateChangeListener() {
                                         @Override
                                         public void onSendStateRespose(boolean state) {
index 4dd6074..bb017a8 100644 (file)
@@ -406,7 +406,7 @@ public class FileHandlerE6 {
                                         msgSendState = true;
                                         msgSendRespose = true;
                                     }*/
-                                    BleHandle.getInstance().deviceWrite(sendArr.get(sendSize),MsgBody.CHANNEL_E6);
+                                    BleHandle.getInstance().deviceWrite(sendArr.get(sendSize),MsgBody.CHANNEL_E6,0);
 
                                     BleHandle.getInstance().setOnSendStateChangeListener(new BleHandle.OnSendStateChangeListener() {
                                         @Override
index 1638dca..2eb6dce 100644 (file)
@@ -6,6 +6,7 @@ import android.graphics.Bitmap;
 import com.google.gson.Gson;
 
 
+import com.zhixun.blesdk.hssdk.alisocket.RideCodeManager;
 import com.zhixun.blesdk.hssdk.bttools.BleHandle;
 import com.zhixun.blesdk.hssdk.callback.BtVtransferCallback;
 import com.zhixun.blesdk.hssdk.bttools.ByteTools;
@@ -47,6 +48,7 @@ public class HsApiKit implements IHSWearAPI {
         SpUtils.getInstance().init(mContext);
         BleHandle.getInstance().init(context);
         BleMsgSendHandler.getInstance().init();
+        RideCodeManager.getInstance().init();
         BleMsgReceiverHandler.getInstance().init();
         FileHandler.getInstance().init();
 //        FileHandlerE6.getInstance().init();
index 8228daa..f5859cf 100644 (file)
@@ -15,6 +15,8 @@ import androidx.annotation.NonNull;
 import com.google.gson.Gson;
 
 import com.zhixun.blesdk.hssdk.HsAgentSdk;
+import com.zhixun.blesdk.hssdk.alisocket.RideCodeManager;
+import com.zhixun.blesdk.hssdk.alisocket.SocketClient;
 import com.zhixun.blesdk.hssdk.bttools.BleHandle;
 import com.zhixun.blesdk.hssdk.callback.BtVtransferCallback;
 import com.zhixun.blesdk.hssdk.bttools.ByteTools;
@@ -59,12 +61,16 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 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.Locale;
 import java.util.TimeZone;
 
 public class ReceiverLooperThread extends Thread {
@@ -99,8 +105,11 @@ public class ReceiverLooperThread extends Thread {
 
     public static final int TYPE_START_SEND = 264;
     public static final int TYPE_STOP_SEND = 520;
+
+    public static final int TYPE_RIDE_CODE = 0000;
     private boolean startSendAudio = false;
     private Handler mReceiverMsgHandle;
+
     private HashMap<Integer, OnMsgSendCallBack> callBackMap = new HashMap<>();
 
     public void run() {
@@ -659,6 +668,36 @@ public class ReceiverLooperThread extends Thread {
                                     PolymerizeAgent.getInstance().getAuthenAPI().setAIChannelKey("xunfei", "", callback);
                                 }*/
 
+                                break;
+                            case MsgBody.TYPE_RIDECODE_DNS:
+
+                                String address = ByteTools.getString(content,"UTF8");
+                                SobLog.d(TAG, "TYPE_RIDECODE_DNS--"+address);
+                                RideCodeManager.getInstance().iPInetAddress(address);
+                                break;
+                            case MsgBody.TYPE_RIDECODE_CONNECT:
+                                SobLog.d(TAG, "TYPE_RIDECODE_CONNECT--"+content.length);
+                                if(content.length<6){
+                                    return;
+                                }
+                                byte[] ip = new byte[4];
+                                byte[] port = new byte[2];
+                                System.arraycopy(content,0,ip,0,4);
+                                System.arraycopy(content,4,port,0,2);
+                                String ipStr = "";
+                                for (byte b:ip){
+                                    ipStr = ipStr+"."+ByteTools.byte2Int(b);
+                                }
+                                int portI = ByteTools.little_bytesToInt(port);
+                                RideCodeManager.getInstance().connectClient(ipStr.substring(1,ipStr.length()),portI);
+                                break;
+                            case MsgBody.TYPE_RIDECODE_MSG:
+                                SobLog.d(TAG, "TYPE_RIDECODE_MSG--"+content.length);
+                                RideCodeManager.getInstance().sendMsgToService(content);
+                                break;
+                            case MsgBody.TYPE_RIDECODE_DISCONNECT:
+                                SobLog.d(TAG, "TYPE_RIDECODE_DISCONNECT--");
+                                RideCodeManager.getInstance().disconnect();
                                 break;
                         }
                         if (callBackMap.containsKey(revData.getType()) && response.getBody() != null) {
@@ -686,6 +725,9 @@ public class ReceiverLooperThread extends Thread {
     public ReceiverLooperThread() {
     }
 
+
+
+
     private void sendTime() {
         AppTime appTime = new AppTime();
         appTime.setTime((System.currentTimeMillis() / 1000));
index 7f7b9f0..994d3e8 100644 (file)
@@ -85,6 +85,11 @@ public class MsgBody implements Serializable {
     public static final int TYPE_GET_SOS = 13825;
     public static final int TYPE_SET_SOS = 9474;
 
+    public static final int TYPE_RIDECODE_NETSTATE = 265;
+    public static final int TYPE_RIDECODE_DNS = 521;
+    public static final int TYPE_RIDECODE_CONNECT = 777;
+    public static final int TYPE_RIDECODE_MSG = 1033;
+    public static final int TYPE_RIDECODE_DISCONNECT = 1289;
     //AI2.0
 //    public static final int TYPE_START_CHAT = 264;
 //    public static final int TYPE_CHAT_AUDIO = 520;
@@ -110,12 +115,21 @@ public class MsgBody implements Serializable {
     public static final int CHANNEL_E6 = 6;
     public static final int CHANNEL_E8 = 8;
     private int channel;
+    private int service;
     private int port;
     private int type;
     private byte[] data;
     private String filePath;
 
 
+    public int getService() {
+        return service;
+    }
+
+    public void setService(int service) {
+        this.service = service;
+    }
+
     public int getChannel() {
         return channel;
     }
index fc3e65b..3f1e0fb 100644 (file)
@@ -26,6 +26,11 @@ public class BleUuidUtil {
     private UUID mFileNotifyUUID = UUID.fromString("00002ae7-0000-1000-8000-00805f9b34fb");//客户端文件接收
 
     private UUID mServiceUUID = UUID.fromString("000018e0-0000-1000-8000-00805f9b34fb");//客户端接收;
+
+    //乘车码
+    private UUID mRCServiceUUID = UUID.fromString("00001000-0000-1000-8000-00805f9b34fb");
+    private UUID mRCNotifyUUID = UUID.fromString("00001001-0000-1000-8000-00805f9b34fb");
+    private UUID mRCWriteUUID = UUID.fromString("00001002-0000-1000-8000-00805f9b34fb");
     public static BleUuidUtil getInstance() {
         if(sInstance == null) {
             synchronized(BleUuidUtil.class) {
@@ -58,7 +63,17 @@ public class BleUuidUtil {
         return r2AE2UUID;
     }
 
+    public UUID getRCServiceUUID() {
+        return mRCServiceUUID;
+    }
 
+    public UUID getRCNotifyUUID() {
+        return mRCNotifyUUID;
+    }
+
+    public UUID getRCWriteUUID() {
+        return mRCWriteUUID;
+    }
 
     public UUID getWriteUUID(int channal) {
         if (channal == MsgBody.CHANNEL_E1) {