|
|
|
|
|
|
|
|
if (hex[0] == CmdConfig.SEND_BLE_START && hex[2] == CmdConfig.SET_HANDSHAKE) { |
|
|
if (hex[0] == CmdConfig.SEND_BLE_START && hex[2] == CmdConfig.SET_HANDSHAKE) { |
|
|
//是握手数据 |
|
|
//是握手数据 |
|
|
byte[] bleDataHandshake = AiLinkBleCheckUtil.returnHandshakeDataFormat(hex); |
|
|
byte[] bleDataHandshake = AiLinkBleCheckUtil.returnHandshakeDataFormat(hex); |
|
|
sendHandshakePwd( bleDataHandshake); |
|
|
|
|
|
|
|
|
sendHandshakePwd(bleDataHandshake); |
|
|
} else { |
|
|
} else { |
|
|
protocolNotifyData(mac, uuid, hex); |
|
|
protocolNotifyData(mac, uuid, hex); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (hex[0] == CmdConfig.SEND_BLE_START && hex[2] == CmdConfig.SET_HANDSHAKE) { |
|
|
if (hex[0] == CmdConfig.SEND_BLE_START && hex[2] == CmdConfig.SET_HANDSHAKE) { |
|
|
//是握手数据 |
|
|
//是握手数据 |
|
|
byte[] bleDataHandshake = AiLinkBleCheckUtil.returnHandshakeDataFormat(hex); |
|
|
byte[] bleDataHandshake = AiLinkBleCheckUtil.returnHandshakeDataFormat(hex); |
|
|
sendHandshakePwd( bleDataHandshake); |
|
|
|
|
|
|
|
|
sendHandshakePwd(bleDataHandshake); |
|
|
} else { |
|
|
} else { |
|
|
protocolNotifyData(mac, uuid, hex); |
|
|
protocolNotifyData(mac, uuid, hex); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case CmdConfig.SEND_MCU_START: |
|
|
case CmdConfig.SEND_MCU_START: |
|
|
payload = AiLinkBleCheckUtil.returnMcuDataFormat(hex); |
|
|
|
|
|
if (payload != null && payload.length >= 1) { |
|
|
|
|
|
byte[] CID = new byte[]{hex[1], hex[2]}; |
|
|
|
|
|
byte[] data; |
|
|
|
|
|
data = AiLinkBleCheckUtil.mcuEncrypt(CID, payload, mac); |
|
|
|
|
|
if (data.length > 0) { |
|
|
|
|
|
if (mOnAnalyticalListener != null) { |
|
|
|
|
|
mOnAnalyticalListener.onAnalyticalData(mac, uuid, data); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
byte[] bytes = mcuDecrypt(hex, mac); |
|
|
|
|
|
if (bytes.length > 0) { |
|
|
|
|
|
if (mOnAnalyticalListener != null) { |
|
|
|
|
|
mOnAnalyticalListener.onAnalyticalData(mac, uuid, bytes); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 密文握手 |
|
|
* 密文握手 |
|
|
*/ |
|
|
*/ |
|
|
public void sendHandshakePwd( byte[] hex) { |
|
|
|
|
|
|
|
|
public void sendHandshakePwd(byte[] hex) { |
|
|
byte[] bytes = initHandshakeArrPwd(hex); |
|
|
byte[] bytes = initHandshakeArrPwd(hex); |
|
|
if (bytes != null) { |
|
|
if (bytes != null) { |
|
|
sendCmd(bytes); |
|
|
sendCmd(bytes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private synchronized void sendCmd(final byte[] bytes) { |
|
|
private synchronized void sendCmd(final byte[] bytes) { |
|
|
if (bytes != null) { |
|
|
if (bytes != null) { |
|
|
|
|
|
|
|
|
mHandler.post(new Runnable() { |
|
|
mHandler.post(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
SystemClock.sleep(100); |
|
|
SystemClock.sleep(100); |
|
|
BluetoothGattService mGattService = MyBleDeviceUtils.getService(mBluetoothGatt, BleConfig.UUID_SERVER_AILINK); |
|
|
BluetoothGattService mGattService = MyBleDeviceUtils.getService(mBluetoothGatt, BleConfig.UUID_SERVER_AILINK); |
|
|
if (mGattService != null) { |
|
|
if (mGattService != null) { |
|
|
BluetoothGattCharacteristic mCharacteristic = MyBleDeviceUtils.getServiceWrite(mGattService, BleConfig.UUID_WRITE_NOTIFY_AILINK); |
|
|
|
|
|
|
|
|
BluetoothGattCharacteristic mCharacteristic = MyBleDeviceUtils |
|
|
|
|
|
.getServiceWrite(mGattService, BleConfig.UUID_WRITE_NOTIFY_AILINK); |
|
|
if (mCharacteristic != null) { |
|
|
if (mCharacteristic != null) { |
|
|
mCharacteristic.setValue(bytes); |
|
|
mCharacteristic.setValue(bytes); |
|
|
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); |
|
|
mCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE); |
|
|
boolean b = mBluetoothGatt.writeCharacteristic(mCharacteristic); |
|
|
boolean b = mBluetoothGatt.writeCharacteristic(mCharacteristic); |
|
|
Log.i("AnalyticalDataUtil","send:"+b); |
|
|
|
|
|
|
|
|
Log.i("AnalyticalDataUtil", "send:" + b); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------握手指令发送校验 end------------- |
|
|
//---------------------握手指令发送校验 end------------- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 解密A7数据 |
|
|
|
|
|
* |
|
|
|
|
|
* @param hex byte[] |
|
|
|
|
|
* @param mac mac |
|
|
|
|
|
* @return byte[] |
|
|
|
|
|
*/ |
|
|
|
|
|
private byte[] mcuDecrypt(byte[] hex, String mac) { |
|
|
|
|
|
byte[] data = AiLinkBleCheckUtil.returnMcuDataFormat(hex); |
|
|
|
|
|
if (data != null && data.length >= 1) { |
|
|
|
|
|
byte[] CID = new byte[]{hex[1], hex[2]}; |
|
|
|
|
|
data = AiLinkBleCheckUtil.mcuEncrypt(CID, data, mac); |
|
|
|
|
|
} |
|
|
|
|
|
return data; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 加密A7数据 |
|
|
|
|
|
* |
|
|
|
|
|
* @param cid cid |
|
|
|
|
|
* @param mac mac |
|
|
|
|
|
* @param payload byte[] |
|
|
|
|
|
* @return byte[] |
|
|
|
|
|
*/ |
|
|
|
|
|
private byte[] mcuEncrypt(int cid, String mac, byte[] payload) { |
|
|
|
|
|
byte[] data = null; |
|
|
|
|
|
if (payload != null && payload.length >= 1) { |
|
|
|
|
|
byte[] CID = new byte[2]; |
|
|
|
|
|
CID[0] = (byte) ((cid >> 8) & 0xff); |
|
|
|
|
|
CID[1] = (byte) (cid); |
|
|
|
|
|
data = AiLinkBleCheckUtil.mcuEncrypt(CID, payload, mac); |
|
|
|
|
|
} |
|
|
|
|
|
return data; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void close() { |
|
|
public void close() { |
|
|
if (mBluetoothGatt != null) { |
|
|
if (mBluetoothGatt != null) { |
|
|
mBluetoothGatt.disconnect(); |
|
|
mBluetoothGatt.disconnect(); |