protocol_ic 协议
authorsnickersun <snickersun@qq.com>
Mon, 30 Nov 2020 18:38:51 +0000 (02:38 +0800)
committersnickersun <snickersun@qq.com>
Mon, 30 Nov 2020 18:38:51 +0000 (02:38 +0800)
修改APBR上传蓝牙格式,添加BPBS设置 Majorlist 协议,修改BPBT协议单位为秒

app/src/main/java/com/enqualcomm/support/LocationClient.java
app/src/main/java/com/enqualcomm/support/MainActivity.java
app/src/main/java/com/enqualcomm/support/adapter/BTHelper.java
app/src/main/java/com/enqualcomm/support/bean/BLEInfo.java
app/src/main/java/com/ic/protocols/protocol_ic.java
local.properties

index d6b5459..64ea956 100644 (file)
@@ -14,6 +14,7 @@ import com.enqualcomm.support.adapter.BTHelper;
 import com.enqualcomm.support.utils.AlarmHelper;\r
 import com.enqualcomm.support.adapter.LBSHelper;\r
 import com.enqualcomm.support.utils.Connection;\r
+import com.enqualcomm.support.utils.IBeaconUtil;\r
 import com.enqualcomm.support.utils.Util;\r
 import com.ic.protocols.protocolAPIHelper;\r
 import com.enqualcomm.support.bean.ICLocation;\r
@@ -143,7 +144,7 @@ public class LocationClient {
     runtime = paramRuntime;\r
     btHelper = BTHelper.CreateInstance(paramRuntime, new BTHelper.BTScanCallback() {\r
       @Override\r
-      public void OnScanFinished(ArrayList<String> btPairedScanResult, ArrayList<String> btNewScanResult) {\r
+      public void OnScanFinished(ArrayList<IBeaconUtil.iBeacon> btBeaconScanResult, ArrayList<String> btNewScanResult) {\r
         if (Config.BLEScanProtocol == 0) { //扫描并判定是否在区域内。\r
           if (btNewScanResult != null && btNewScanResult.size() > 0) {\r
             StringBuilder sb = new StringBuilder();\r
@@ -279,13 +280,18 @@ public class LocationClient {
           scanBLEOnly = false;\r
         } else {\r
           //Config.BLEScanProtocol == 1\r
-          if (btNewScanResult != null && btNewScanResult.size() > 0) {\r
+          if (btBeaconScanResult != null && btBeaconScanResult.size() > 0) {\r
             StringBuilder sb = new StringBuilder();\r
-            sb.append("found bt mac ").append(btNewScanResult.size());\r
+            sb.append("found bt mac ").append(btBeaconScanResult.size());\r
             runtime.bleInfo.btScanResult.clear();\r
-            for (String mac : btNewScanResult) {\r
-              sb.append("\n").append(mac);\r
-              runtime.bleInfo.btScanResult.add(mac.replace(":", ""));\r
+            for (IBeaconUtil.iBeacon beacon : btBeaconScanResult) {\r
+              sb.append("\n").append(beacon.toString());\r
+              if(runtime.bleInfo.btMajorList.contains(beacon.major)) {\r
+                runtime.bleInfo.btScanResult.add(beacon.toString());\r
+                sb.append("in list!");\r
+              }else{\r
+                sb.append("not in!");\r
+              }\r
             }\r
             ICLogger.i(sb.toString());\r
             Deliver.sendLocationMSG(protocolAPIHelper.getBLEScanResult(runtime));\r
index 658df9e..ec718b0 100644 (file)
@@ -19,6 +19,7 @@ import android.widget.TextView;
 
 import com.enqualcomm.support.adapter.BTHelper;
 import com.enqualcomm.support.utils.DeviceInfoUtil;
+import com.enqualcomm.support.utils.IBeaconUtil;
 import com.enqualcomm.support.utils.ICLogger;
 import com.enqualcomm.support.utils.ImageUtil;
 import com.enqualcomm.support.utils.SharPreferenceUtil;
@@ -126,7 +127,7 @@ public class MainActivity extends Activity {
     private void testBLE(){
         btHelper= BTHelper.CreateInstance(CoreService.getInstance().runtime, new BTHelper.BTScanCallback() {
                     @Override
-                    public void OnScanFinished(ArrayList<String> btPairedScanResult, ArrayList<String> btNewScanResult) {
+                    public void OnScanFinished(ArrayList<IBeaconUtil.iBeacon> btBeaconScanResult, ArrayList<String> btNewScanResult) {
 
 
 //                        ICLogger.i("BT Pared "+btPairedScanResult.size());
index 8487507..a243392 100644 (file)
@@ -11,6 +11,7 @@ import android.net.wifi.ScanResult;
 import android.os.Handler;
 import com.enqualcomm.support.bean.Runtime;
 import com.enqualcomm.support.Config;
+import com.enqualcomm.support.utils.IBeaconUtil;
 import com.enqualcomm.support.utils.ICLogger;
 import com.enqualcomm.support.utils.Util;
 
@@ -22,7 +23,7 @@ import java.util.Set;
 public class BTHelper {
 
     private BluetoothAdapter mBtAdapter;
-    private static ArrayList<String> mPairedDevicesArray=new ArrayList<>();
+    private static ArrayList<IBeaconUtil.iBeacon> mBeaconDevicesArray=new ArrayList<>();
     private static ArrayList<String> mNewDevicesArray=new ArrayList<>();
     private Context mContext=null;
     private BTScanCallback mCallback=null;
@@ -119,7 +120,7 @@ public class BTHelper {
             enabledBeforScan = mBtAdapter.isEnabled();
             enableBT();
 
-            mPairedDevicesArray.clear();
+            mBeaconDevicesArray.clear();
             mNewDevicesArray.clear();
 
 //            mBtAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_NONE, BLETimeoutForOneScan);
@@ -194,7 +195,10 @@ public class BTHelper {
                     break;
                 }
             }
-             if (!found) {
+            if (!found) {
+                IBeaconUtil.iBeacon iBeacon = IBeaconUtil.fromScanData(bluetoothDevice.getName(), bluetoothDevice.getAddress(), rssi, scanRecode);
+                if (iBeacon != null)
+                    mBeaconDevicesArray.add(iBeacon);
                 mNewDevicesArray.add(bluetoothDevice.getAddress() + "," + rssi);
             }
 
@@ -202,26 +206,29 @@ public class BTHelper {
             //scanRecode 可以解码 major minor
             //ICLogger.i(bluetoothDevice.getAddress()+" "+bluetoothDevice.getName()+" "+rssi+" "+Util.byte2hex(scanRecode));
 
-            if(isBeaconFounded(bluetoothDevice.getAddress())){
-
-                mHandler.removeCallbacks(mRunable_stopBleScan);
-
-                ICLogger.i("BT onLeScan stopLeScan");
-                mBtAdapter.stopLeScan(this);
-                //开始扫描前是关闭的,就关闭它
-                //                if(!enabledBeforScan)
-                disableBT();
-                hasStopedByFoundBT=true;
-                if (mCallback != null) {
-                    resortScanResult(mNewDevicesArray);
-                    mCallback.OnScanFinished(mPairedDevicesArray, mNewDevicesArray);
+            //通过绑定mac地址定位的蓝牙扫描协议0,扫到就可以关闭蓝牙省电
+            if (Config.BLEScanProtocol == 0) {
+                if (isBeaconFounded(bluetoothDevice.getAddress())) {
+
+                    mHandler.removeCallbacks(mRunable_stopBleScan);
+
+                    ICLogger.i("BT onLeScan stopLeScan");
+                    mBtAdapter.stopLeScan(this);
+                    //开始扫描前是关闭的,就关闭它
+                    //                if(!enabledBeforScan)
+                    disableBT();
+                    hasStopedByFoundBT = true;
+                    if (mCallback != null) {
+                        resortScanResult1(mNewDevicesArray);
+                        mCallback.OnScanFinished(mBeaconDevicesArray, mNewDevicesArray);
+                    }
+                    btscanning = false;
                 }
-                btscanning=false;
             }
         }
     }
 
-    private  void resortScanResult( ArrayList<String> btlist){
+    private  void resortScanResult1( ArrayList<String> btlist){
 
         Collections.sort(btlist, new Comparator<String>() {
             public int compare(String var1, String var2) {
@@ -237,6 +244,22 @@ public class BTHelper {
             }
         });
 
+    }
+
+    private  void resortScanResult( ArrayList<IBeaconUtil.iBeacon> btlist){
+
+        Collections.sort(btlist, new Comparator<IBeaconUtil.iBeacon>() {
+            public int compare(IBeaconUtil.iBeacon var1, IBeaconUtil.iBeacon var2) {
+                try {
+                    int rssi1 = var1.rssi;
+                    int rssi2 = var2.rssi;
+                    return Math.abs(rssi1) - Math.abs(rssi2);
+                }catch (Exception e){
+                    return 0;
+                }
+            }
+        });
+
     }
     private class RunableStopBleScan implements   Runnable{
 
@@ -248,8 +271,9 @@ public class BTHelper {
             //                if(!enabledBeforScan)
             disableBT();
             if (mCallback != null){
-                resortScanResult(mNewDevicesArray);
-                mCallback.OnScanFinished(mPairedDevicesArray, mNewDevicesArray);
+                resortScanResult1(mNewDevicesArray);
+                resortScanResult(mBeaconDevicesArray);
+                mCallback.OnScanFinished(mBeaconDevicesArray, mNewDevicesArray);
             }
             btscanning=false;
 
@@ -257,6 +281,6 @@ public class BTHelper {
     }
 
     public interface BTScanCallback{
-        void OnScanFinished(ArrayList<String> btPairedScanResult,ArrayList<String> btNewScanResult);
+        void OnScanFinished(ArrayList<IBeaconUtil.iBeacon> btBeaconScanResult,ArrayList<String> btNewScanResult);
     }
 }
index 7c95702..3836397 100644 (file)
@@ -7,6 +7,7 @@ public class BLEInfo {
     public String ble_area_id="";\r
 \r
     public ArrayList<String> btMacList=new ArrayList<>();\r
+    public ArrayList<String> btMajorList=new ArrayList<>();\r
     public ArrayList<String> btScanResult=new ArrayList<>();\r
 \r
     public String gps_n="";\r
index 90df28b..762c82b 100644 (file)
@@ -159,6 +159,8 @@ public class protocol_ic {
         BPAT,
         APBT,
         BPBT,//设置蓝牙扫描间隔
+        APBS,
+        BPBS,//设置蓝牙绑定Major
         APWL,
         BPWL,//设置白名单
         AP65,
@@ -275,6 +277,8 @@ public class protocol_ic {
         cmdMap.put(CMD_TYPE.BPAT, "BPAT");
         cmdMap.put(CMD_TYPE.APBT, "APBT");
         cmdMap.put(CMD_TYPE.BPBT, "BPBT");
+        cmdMap.put(CMD_TYPE.APBS, "APBS");
+        cmdMap.put(CMD_TYPE.BPBS, "BPBS");
         cmdMap.put(CMD_TYPE.APWL, "APWL");
         cmdMap.put(CMD_TYPE.BPWL, "BPWL");
         cmdMap.put(CMD_TYPE.AP65, "AP65");
@@ -2667,7 +2671,7 @@ public class protocol_ic {
                 int intcycle=Integer.parseInt(cycle);
                 Config.setBleScanCycle(runtime.getContextProxy().context,intcycle);
                 runtime.bleInfo.bleScanCycle=intcycle;
-                ICLogger.i("设置蓝牙扫描周期 "+intcycle+" 分钟");
+                ICLogger.i("设置蓝牙扫描周期 "+intcycle+" 秒");
                 CoreService.getInstance().setBleScanAlarm(intcycle);
             }else{
                 ICLogger.i(" BPBT Format error!");
@@ -2684,10 +2688,9 @@ public class protocol_ic {
      * @return
      */
     public static StringMessage APBR(Runtime runtime) {
-        /**
-         * #IWAPBR,353456789012345,080835,1#
-         *
-         * #IWBPBR#
+
+        /***
+         * IWAPBR,IMEI,yyyyMMddHHmmss,beaconCount,major0,minor0,mac0,rssi0,major1,minor1,mac1,rssi1...#
          */
         if (runtime.bleInfo.btScanResult.size() > 0) {
             StringBuffer sb=new StringBuffer();
@@ -2696,6 +2699,7 @@ public class protocol_ic {
                 sb.append(CMDSPLIT).append(it);
             }
             return genStringMessage("APBR", new StringBuffer(CMDHEADER).append(cmdMap.get(CMD_TYPE.APBR))
+                    .append(CMDSPLIT).append(runtime.IMEI())
                     .append(CMDSPLIT).append(TimeUtil.getSystemTimeyyyyMMddHHmmss())
                     .append(CMDSPLIT).append(sb.toString())
                     .append(CMDEND).toString());
@@ -2704,7 +2708,43 @@ public class protocol_ic {
             return null;
         }
     }
+    public static StringMessage APBS(String id,String cycle)
+    {
+        return genStringMessage("APBS",new StringBuffer(CMDHEADER).append(cmdMap.get(CMD_TYPE.APBS))
+                .append(CMDSPLIT).append(id)
+                .append(CMDSPLIT).append(cycle)
+                .append(CMDEND).toString());
+    }
+    public void handleBPBS( Connection connection,Runtime runtime,String cmd) {
+/***
+ * IWBPBS,353456789012345,080835,150&151#
+ * 说明:设置蓝牙扫描绑定的 major id
+ * IW:标识符
+ * BPBS : 命令字符
+ * 353456789012345:IMEI号,设备唯一ID
+ * 080835:指令流水号
+ * majorIDs: 用 & 分隔的 多个 Majorid
+ * 设备响应:     IWAPBS,080835, 1#
+ * 080835:指令流水号
+ * 1: 成功,0失败
+ */
+        log("BPBS "+cmd);
+        String[] cmdlist=cmd.split(CMDSPLIT);
+        if (cmdlist.length==4){
+            String id=cmdlist[2];
+            connection.send(APBS(id,"1"));
 
+            ICLogger.i("设置蓝牙扫描绑定MajorID "+cmdlist[3]+" ");
+            String[] majorlist=cmdlist[3].split("&");
+            runtime.bleInfo.btMajorList.clear();
+            for(int i=0;i<majorlist.length;i++){
+                runtime.bleInfo.btMajorList.add(majorlist[i]);
+            }
+
+        }else{
+            ICLogger.i(" BPBS Format error!");
+        }
+    }
     /**
      * 85.设置联系人白名单(10个)(下行协议:BPWL,响应:APWL)
      * 示例:
@@ -3148,6 +3188,9 @@ public class protocol_ic {
             case BPBT://蓝牙扫描设置
                 handleBPBT(mConnection,mRuntime,cmd);
                 break;
+            case BPBS://蓝牙扫描设置Majorlist
+                handleBPBS(mConnection,mRuntime,cmd);
+                break;
 
             case BP65://声音设置
                 handleBP65(mConnection,mRuntime,cmd);
index 85972cc..922ccaf 100644 (file)
@@ -4,5 +4,5 @@
 # Location of the SDK. This is only used by Gradle.\r
 # For customization when using a Version Control System, please read the\r
 # header note.\r
-#Mon Oct 12 09:44:16 CST 2020\r
-sdk.dir=C\:\\Users\\incar\\AppData\\Local\\Android\\Sdk\r
+#Sat Nov 28 22:50:15 CST 2020\r
+sdk.dir=D\:\\Android\\Sdk\r