Skip to content

Commit

Permalink
Merge pull request #13 from kadbbz/dev-1.14.1
Browse files Browse the repository at this point in the history
支持写入Hex字符串
  • Loading branch information
kadbbz authored Jan 3, 2024
2 parents 813ad92 + 1f211f4 commit e223c03
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ HAC是[活字格低代码开发平台](https://www.grapecity.com.cn/solutions/hu
* 提供通过GPS等精确定位方式获取当前位置的能力
* 提供调用蓝牙打印机(DothanTech方案)的能力
* 提供读取NFC标签的能力
* 提供读写蓝牙BLE设备的能力
* 提供下载和预览PDF文件的能力
* 提供获取设备唯一标识(SSAID)的能力
* 适配活字格[“PDA交互命令”(插件)](https://marketplace.grapecity.com.cn/ApplicationDetails?productID=SP2209070004&productDetailID=D2209070005)[“手机扫码命令”(插件)](https://marketplace.grapecity.com.cn/ApplicationDetails?productID=SP2104270020&productDetailID=D2206270041&tabName=Tabs_detail)
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
//noinspection ExpiredTargetSdkVersion
targetSdk 29 // 超过29后,文件权限要大改
versionCode 4
versionName '1.14.0-release'
versionName '1.14.1-release'

manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.clj.fastble.exception.BleException;
import com.hjq.permissions.Permission;
import com.huozige.lab.container.R;
import com.huozige.lab.container.utilities.MiscUtilities;
import com.huozige.lab.container.utilities.PermissionsUtility;

import java.util.ArrayList;
Expand Down Expand Up @@ -383,7 +384,7 @@ public void onReadSuccess(byte[] data) {
Log.v(LOG_TAG, "Data retrieved.");

if (data != null) {
Log.v(LOG_TAG, "Reading completed, data: " + stringifyByteArray(data));
Log.v(LOG_TAG, "Reading completed, data: " + MiscUtilities.bytesToHex(data));
sendResultAndFinish(data);
} else {
Log.v(LOG_TAG, "Reading completed, no data retrieved.");
Expand Down Expand Up @@ -463,7 +464,7 @@ public void onCharacteristicChanged(byte[] data) {
Log.v(LOG_TAG, "Notify Data retrieved.");

if (data != null) {
Log.v(LOG_TAG, "Reading completed, data: " + stringifyByteArray(data));
Log.v(LOG_TAG, "Reading completed, data: " + MiscUtilities.bytesToHex(data));
sendResultAndFinish(data);
} else {
Log.v(LOG_TAG, "Reading completed, no data retrieved.");
Expand Down Expand Up @@ -549,7 +550,7 @@ public void onCharacteristicChanged(byte[] data) {
Log.v(LOG_TAG, "Indicate Data retrieved.");

if (data != null) {
Log.v(LOG_TAG, "Reading completed, data: " + stringifyByteArray(data));
Log.v(LOG_TAG, "Reading completed, data: " + MiscUtilities.bytesToHex(data));
sendResultAndFinish(data);
} else {
Log.v(LOG_TAG, "Reading completed, no data retrieved.");
Expand Down Expand Up @@ -580,18 +581,33 @@ public void onDisConnected(boolean isActiveDisConnected, BleDevice bleDevice, Bl
);
}

private void startWrite(String mac, String uuid_service, String uuid_characteristic, String payload) {
private void startWrite(String mac, String uuid_service, String uuid_characteristic, String stringValue) {

final String[] realServiceCharacterUuid = new String[2];
realServiceCharacterUuid[0] = uuid_service;
realServiceCharacterUuid[1] = uuid_characteristic;

// 解析写入蓝牙的负载
if (payload == null) {
if(stringValue==null || stringValue.isEmpty()){
sendResultAndFinish(new BleError(-110, "PAYLOAD_IS_EMPTY"));
return;
}

byte[] data = Base64.getDecoder().decode(payload);
byte[] parsedData;

// 解析时,优先判断十六进制,然后再处理base64.如果都不是则直接报错
if (stringValue.toLowerCase().startsWith("0x")) {
parsedData = MiscUtilities.hexToByteArray(stringValue);
}else{
try {
parsedData = Base64.getDecoder().decode(stringValue);
}catch(IllegalArgumentException ex){
sendResultAndFinish(new BleError(-110, "PAYLOAD_IS_NOT_HEX_OR_BASE64"));
return;
}
}

byte[] data = parsedData;

PermissionsUtility.asyncRequirePermissions(this, new String[]{
Permission.ACCESS_FINE_LOCATION,
Expand Down Expand Up @@ -628,7 +644,7 @@ public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status

fillDefaultServiceAndCharacters(gatt, realServiceCharacterUuid, uuid_service, uuid_characteristic);

Log.v(LOG_TAG, "Start writing to " + bleDevice.getName() + ", service: " + realServiceCharacterUuid[0] + ", character: " + realServiceCharacterUuid[1] + ", payload: " + payload);
Log.v(LOG_TAG, "Start writing to " + bleDevice.getName() + ", service: " + realServiceCharacterUuid[0] + ", character: " + realServiceCharacterUuid[1] + ", payload: " + stringValue);

BleManager.getInstance().write(
bleDevice,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,60 @@ public static String getUrlparameter(String urlString, String paraName) {

return null;
}

/**
* hex字符串转byte数组
* @param inHex 待转换的Hex字符串
* @return 转换后的byte数组结果
*/
public static byte[] hexToByteArray(String inHex){

if(inHex.toLowerCase().startsWith("0x")){
inHex = inHex.substring(2,inHex.length()-2);
}

int hexlen = inHex.length();
byte[] result;
if (hexlen % 2 == 1){
//奇数
hexlen++;
result = new byte[(hexlen/2)];
inHex="0"+inHex;
}else {
//偶数
result = new byte[(hexlen/2)];
}
int j=0;
for (int i = 0; i < hexlen; i+=2){
result[j]=hexToByte(inHex.substring(i,i+2));
j++;
}
return result;
}

/**
* Hex字符串转byte
* @param inHex 待转换的Hex字符串
* @return 转换后的byte
*/
public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}

/**
* 字节数组转16进制
* @param bytes 需要转换的byte数组
* @return 转换后的Hex字符串
*/
public static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) {
String hex = Integer.toHexString(aByte & 0xFF);
if (hex.length() < 2) {
sb.append(0);
}
sb.append(hex);
}
return sb.toString();
}
}

0 comments on commit e223c03

Please sign in to comment.