Parcourir la source

添加AILink广播设备解密方法

master
elinkthings il y a 3 ans
Parent
révision
073c22b8a2

BIN
AILinkSecretTool.framework/AILinkSecretTool Voir le fichier


+ 4
- 0
AILinkSecretTool.framework/Headers/ELEncryptTool.h Voir le fichier

@@ -37,6 +37,10 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSData *)encryptXOR:(NSData *)macXOR deviceTypeXOR:(NSData *)deviceTypeXOR withXORData:(NSData *)dataXOR;


/// AILink广播设备(广播体脂秤、广播营养秤)解密方法
/// @param encryptData 广播数据中加密数据
+ (NSData *)broadcastDecryptTEA:(NSData * _Nonnull)encryptData cid:(Byte)cid vid:(Byte)vid pid:(Byte)pid;

@end

NS_ASSUME_NONNULL_END

BIN
AILinkSecretTool.framework/_CodeSignature/CodeDirectory Voir le fichier


BIN
AILinkSecretTool.framework/_CodeSignature/CodeRequirements Voir le fichier


BIN
AILinkSecretTool.framework/_CodeSignature/CodeRequirements-1 Voir le fichier


+ 3
- 3
AILinkSecretTool.framework/_CodeSignature/CodeResources Voir le fichier

@@ -6,7 +6,7 @@
<dict>
<key>Headers/ELEncryptTool.h</key>
<data>
9l+9JY8Tj2oxZTOPq1Cm+CDSw3c=
V9LzNc2XZCWjrfFm/x/2Yf+d+Lc=
</data>
<key>Headers/ELSDKHeader.h</key>
<data>
@@ -23,11 +23,11 @@
<dict>
<key>hash</key>
<data>
9l+9JY8Tj2oxZTOPq1Cm+CDSw3c=
V9LzNc2XZCWjrfFm/x/2Yf+d+Lc=
</data>
<key>hash2</key>
<data>
ys5z9HLJkLqfOhszinVfZTB8fdeuEAd66bW0q4tfv6o=
Ml2Tgb1iMRJ8fjf/lTyYMbQLPqZcpfXRaxwjbZ3SVVA=
</data>
</dict>
<key>Headers/ELSDKHeader.h</key>

BIN
AILinkSecretTool.framework/_CodeSignature/CodeSignature Voir le fichier


+ 12
- 6
AILinkSecretToolDemo/AILinkSecretToolDemo/ELBluetoothManager.h Voir le fichier

@@ -29,6 +29,18 @@
*/
-(void)bluetoothManagerReceivePassData:(NSData *_Nullable)data;


//Call back the scanned broadcast device(回调扫描到的广播设备)
-(void)bluetoothManagerScanedBroadcastPeripherals:(NSArray<ELPeripheralModel *> *_Nonnull)peripherals;

/// Callback broadcast device data(回调广播设备数据)
/// @param cid cid
/// @param vid vid
/// @param pid pid
/// @param mac mac地址
/// @param data Custom data(自定义数据)
-(void)bleBroadcastWithCid:(int)cid vid:(int)vid pid:(int)pid mac:(NSString *_Nonnull)mac data:(NSData *_Nonnull)data;

@end


@@ -51,12 +63,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, weak) id<ELBluetoothManagerDelegate> delegate;


/** 需要搜索与系统绑定的蓝牙设备 */
@property (nonatomic, assign) BOOL needScanBindSysDevice;

//设置appkey和secret
+(void)setAppKey:(NSString *)key appSecret:(NSString *)secret;

//
+(__kindof ELBluetoothManager * _Nonnull)shareManager;


+ 118
- 5
AILinkSecretToolDemo/AILinkSecretToolDemo/ELBluetoothManager.m Voir le fichier

@@ -19,6 +19,9 @@ static NSString *const notifyCharactisticUUID = @"FFE2";
//APP <-> BLE 二者双向交互的UUID,同时有write和notify
static NSString *const writeAndNotifyCharactisticUUID = @"FFE3";

//AILink广播设备服务UUID
static NSString *const broadcastServiceUUID = @"F0A0";

//握手超时时间
static NSInteger const HandShakeTimeout = 5.0;

@@ -33,7 +36,8 @@ static NSInteger const HandShakeTimeout = 5.0;


@property (nonatomic, strong) NSMutableArray<ELPeripheralModel *> *peripheralArray;//扫描到的外设

//广播设备
@property(nonatomic, strong) NSMutableArray<ELPeripheralModel *> *broadcastPeripheralArray;

@property (nonatomic, assign) ELBluetoothState state;//蓝牙状态
@property (nonatomic, assign) BOOL isOn;//
@@ -93,11 +97,15 @@ static NSInteger const HandShakeTimeout = 5.0;
return _peripheralArray;
}


- (void)setNeedScanBindSysDevice:(BOOL)needScanBindSysDevice {
_needScanBindSysDevice = needScanBindSysDevice;
- (NSMutableArray<ELPeripheralModel *> *)broadcastPeripheralArray {
if (_broadcastPeripheralArray == nil) {
_broadcastPeripheralArray = [[NSMutableArray alloc] init];
}
return _broadcastPeripheralArray;
}



#pragma mark ============ 自定义方法 ==============
-(void)startScan{
@@ -106,7 +114,8 @@ static NSInteger const HandShakeTimeout = 5.0;
[self.peripheralArray removeAllObjects];//移除之前扫描的设备

CBUUID *uuid = [CBUUID UUIDWithString:serviceUUID];//serviceUUID
[self.centralManager scanForPeripheralsWithServices:@[uuid] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];
CBUUID *broadcastUUID = [CBUUID UUIDWithString:broadcastServiceUUID];//广播设备UUID
[self.centralManager scanForPeripheralsWithServices:@[uuid,broadcastUUID] options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:YES]}];
}else{
NSLog(@"please open bluetooth");
@@ -249,7 +258,50 @@ static NSInteger const HandShakeTimeout = 5.0;
} else {
NSLog(@"Other perpheral = %@",advertisementData.description);
}
} else if ([serviceUUIDs.description rangeOfString:broadcastServiceUUID].length) {
//MARK: 广播设备
if (manufactureData.length != 20) {
NSLog(@"Broadcast device error");
return;
}
NSLog(@"=====AILink广播设备:%@", manufactureData);
//CID.VID.PID----MAC-------校验和----自定义数据(前8个byte要解密)
//0x010101-----06000070b801 3c 6bfae71d19cb18d7 0000
Byte byteCid = byte[0];
int cid = byte[0] + 65535;
int vid = byte[1];
int pid = byte[2];
//mac地址
NSData *macData = [manufactureData subdataWithRange:NSMakeRange(3, 6)];
NSString *macStr = [self getBroadcastPeripheralWithData:macData];
//回调到广播设备列表
[self addBroadcastPeriphralWithName:peripheralName withMacAddress:macStr withRSSI:RSSI.integerValue withPeripheral:peripheral withVendorID:vid withProductID:pid withDeviceType:cid devData:manufactureData];
//校验和
NSData *chechsumData = [manufactureData subdataWithRange:NSMakeRange(9, 11)];
if ([self broadcastChecksum:chechsumData]) {
//获取加密数据
NSData *data = [manufactureData subdataWithRange:NSMakeRange(10, 8)];
//对加密数据进行解密
data = [ELEncryptTool broadcastDecryptTEA:data cid:byteCid vid:vid pid:pid];
//获取包尾2个byte数据
NSData *tailData = [manufactureData subdataWithRange:NSMakeRange(18, 2)];
//获取最终的数据
NSMutableData *result = [NSMutableData dataWithData:data];
[result appendData:tailData];
//
//在主线程上回调数据
dispatch_async(dispatch_get_main_queue(), ^{
if ([self.delegate respondsToSelector:@selector(bleBroadcastWithCid:vid:pid:mac:data:)]) {
[self.delegate bleBroadcastWithCid:cid vid:vid pid:pid mac:macStr data:result];
}
});
} else {
NSLog(@"Broadcast equipment verification failed");
}
}
}
}
@@ -285,6 +337,35 @@ static NSInteger const HandShakeTimeout = 5.0;
});
}

//MARK: 回调广播设备
- (void)addBroadcastPeriphralWithName:(NSString *)name withMacAddress:(NSString *)mac withRSSI:(NSInteger)rssi withPeripheral:(CBPeripheral *)peripheral withVendorID:(NSInteger)vid withProductID:(NSInteger)pid withDeviceType:(NSInteger)cid devData:(NSData *)devData {

for (ELPeripheralModel *model in self.broadcastPeripheralArray) {
if ([model.macAddress isEqualToString:mac]) {
return;
}
}

ELPeripheralModel *model = [[ELPeripheralModel alloc] init];
model.deviceName = name;
model.macAddress = mac;
model.rssi = rssi;
model.peripheral = peripheral;
model.deviceType = cid;
model.vendorID = vid;
model.productID = pid;
model.manufactureData = devData;
[self.broadcastPeripheralArray addObject:model];
// [self.broadcastPeripheralArray sortUsingFunction:bluetoothManagerPeriphelRSSIsortUsingFunction context:NULL];
dispatch_async(dispatch_get_main_queue(), ^{
NSArray<ELPeripheralModel *> *peripherals = self.broadcastPeripheralArray.copy;
if ([self.delegate respondsToSelector:@selector(bluetoothManagerScanedBroadcastPeripherals:)]) {
[self.delegate bluetoothManagerScanedBroadcastPeripherals:peripherals];
}
});
}



//MARK: 获取连接类设备mac地址
-(NSString *)getMacAddressWithByte:(NSData *)data{
@@ -721,6 +802,38 @@ static NSInteger const HandShakeTimeout = 5.0;
}


-(BOOL)broadcastChecksum:(NSData *)data{
if (data.length != 11) {
return NO;
}else{
BOOL is = NO;
Byte *bytes = (Byte *)[data bytes];
//校验和
Byte checksum = 0x00;
for (int i=1; i<data.length; i++) {
checksum += bytes[i];
}
if (checksum == bytes[0]) {
is = YES;
}
return is;
}
}

//MARK: 解析广播设备mac地址
- (NSString *)getBroadcastPeripheralWithData:(NSData *)macData {
Byte *macBytes = (Byte *) macData.bytes;
NSMutableString *macStr = [[NSMutableString alloc] init];
for (NSInteger i = macData.length - 1; i >= 0; i--) {
NSString *bStr = [NSString stringWithFormat:@"%02x", macBytes[i]];
[macStr appendString:bStr.uppercaseString];
if (i > 0) {
[macStr appendString:@":"];
}
}
return macStr.copy;
}


@end

+ 11
- 1
AILinkSecretToolDemo/AILinkSecretToolDemo/MyTableViewController.m Voir le fichier

@@ -35,6 +35,16 @@
[self.tableView reloadData];
}

#pragma mark ============ 广播设备 ==============
- (void)bluetoothManagerScanedBroadcastPeripherals:(NSArray<ELPeripheralModel *> *)peripherals {
NSLog(@"---scan broadcast device:%zd",peripherals.count);
}

- (void)bleBroadcastWithCid:(int)cid vid:(int)vid pid:(int)pid mac:(NSString *)mac data:(NSData *)data {
NSLog(@"---receive broadcast device data:%@",data);
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
@@ -72,7 +82,7 @@
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
}
*/


Chargement…
Annuler
Enregistrer