public static void sendErrorMsg(CallbackContext callbackContext) { JSONObject jsonObject = new JSONObject(); Tools.addProperty(jsonObject, Tools.MES, Tools.ERROR); callbackContext.error(jsonObject); }
public static void sendSuccessMsg(CallbackContext callbackContext) { JSONObject jsonObject = new JSONObject(); Tools.addProperty(jsonObject, Tools.MES, Tools.SUCCESS); callbackContext.success(jsonObject); }
public static JSONObject decodeAdvData(byte[] advData) { JSONObject jsonAdvData = new JSONObject(); JSONArray serviceUUIDs = new JSONArray(); JSONArray solicitedServiceUUIDs = new JSONArray(); JSONArray overflowServiceUUIDs = new JSONArray(); boolean isOver = true; while (isOver) { int dataLen = advData[0]; if (dataLen == 0) { isOver = false; break; } byte[] allData = new byte[dataLen]; for (int i = 0; i < allData.length; i++) { allData[i] = advData[i + 1]; } byte[] type = {allData[0]}; byte[] data = new byte[allData.length - 1]; for (int i = 0; i < data.length; i++) { data[i] = allData[i + 1]; } if ((0xff & type[0]) == 0x02) { byte[] mByte = new byte[data.length]; for (int i = 0; i < mByte.length; i++) { mByte[i] = data[data.length - i - 1]; } serviceUUIDs.put(bytesToHexString(mByte)); } else if ((0xff & type[0]) == 0x03) { int number = data.length / 2; for (int i = 0; i < number; i++) { byte[] mByte = {data[i * 2], data[i * 2 + 1]}; serviceUUIDs.put(bytesToHexString(mByte)); } } else if ((0xff & type[0]) == 0x04) { byte[] mByte = new byte[data.length]; for (int i = 0; i < mByte.length; i++) { mByte[i] = data[data.length - i - 1]; } serviceUUIDs.put(bytesToHexString(mByte)); } else if ((0xff & type[0]) == 0x05) { int number = data.length / 4; for (int i = 0; i < number; i++) { byte[] mByte = {data[i * 4], data[i * 4 + 1], data[i * 4 + 2], data[i * 4 + 3]}; serviceUUIDs.put(bytesToHexString(mByte)); } } else if ((0xff & type[0]) == 0x06) { byte[] mByte = new byte[data.length]; for (int i = 0; i < mByte.length; i++) { mByte[i] = data[data.length - i - 1]; } serviceUUIDs.put(bytesToHexString(mByte)); } else if ((0xff & type[0]) == 0x07) { int number = data.length / 16; for (int i = 0; i < number; i++) { byte[] mByte = { data[i * 16], data[i * 16 + 1], data[i * 16 + 2], data[i * 16 + 3], data[i * 16 + 4], data[i * 16 + 5], data[i * 16 + 6], data[i * 16 + 7], data[i * 16 + 8], data[i * 16 + 9], data[i * 16 + 10], data[i * 16 + 11], data[i * 16 + 12], data[i * 16 + 13], data[i * 16 + 14], data[i * 16 + 15] }; serviceUUIDs.put(bytesToHexString(mByte)); } } else if ((0xff & type[0]) == 0x08) { addProperty(jsonAdvData, LOCAL_NAME, hexStrToStr(bytesToHexString(data))); } else if ((0xff & type[0]) == 0x09) { addProperty(jsonAdvData, LOCAL_NAME, hexStrToStr(bytesToHexString(data))); } else if ((0xff & type[0]) == 0x0a) { addProperty(jsonAdvData, TXPOWER_LEVEL, bytesToHexString(data)); } else if ((0xff & type[0]) == 0x12) { addProperty(jsonAdvData, IS_CONNECTED, bytesToHexString(data)); } else if ((0xff & type[0]) == 0x14) { int number = data.length / 2; for (int i = 0; i < number; i++) { byte[] mByte = {data[i * 2], data[i * 2 + 1]}; solicitedServiceUUIDs.put(bytesToHexString(mByte)); } } else if ((0xff & type[0]) == 0x15) { int number = data.length / 16; for (int i = 0; i < number; i++) { byte[] mByte = { data[i * 16], data[i * 16 + 1], data[i * 16 + 2], data[i * 16 + 3], data[i * 16 + 4], data[i * 16 + 5], data[i * 16 + 6], data[i * 16 + 7], data[i * 16 + 8], data[i * 16 + 9], data[i * 16 + 10], data[i * 16 + 11], data[i * 16 + 12], data[i * 16 + 13], data[i * 16 + 14], data[i * 16 + 15] }; solicitedServiceUUIDs.put(bytesToHexString(mByte)); } } else if ((0xff & type[0]) == 0x16) { addProperty(jsonAdvData, SERVICE_DATA, bytesToHexString(data)); } else if ((0xff & type[0]) == 0xff) { addProperty(jsonAdvData, MANUFACTURER_DATA, encodeBase64(data)); } byte[] newData = new byte[advData.length - dataLen - 1]; for (int i = 0; i < newData.length; i++) { newData[i] = advData[i + 1 + dataLen]; } advData = newData; } addProperty(jsonAdvData, SERVICE_UUIDS, serviceUUIDs); addProperty(jsonAdvData, SOLICITED_SERVICE_UUIDS, solicitedServiceUUIDs); addProperty(jsonAdvData, OVERFLOW_SERVICE_UUIDS, overflowServiceUUIDs); return jsonAdvData; }