iOS TakeFitSDK 集成文档
导入SDK
将TakeFitSDK.framework文件手动拖入到Xcode工程目录中, 在弹出的options界面中勾选 Copy items if needed,并确保 Add to targets 勾选相应的 target:
E73A25D8-484F-42ED-92D4-5BAE4AB8D8AD.png
检查 TARGETS --> Build Phases --> Link Binary With Libraries 选项下是否已经加入 TakeFitSDK.framework,如果没有,请手动加入。
4AAB4800-CF51-4E6B-A16F-0354713D27CF.png
权限设置
在info.plist文件中添加蓝牙权限:
AE75C11B-8238-4772-8A0C-D46445EF7FB9.png
SDK初始化
在调用SDK之前,记得在类中引用头文件:
#import <TakeFitSDK/TakeFitSDK.h>
初始化单例对象
初始化参数TakeUserModel必填,计算运动相关的数值时需包含如上这些参数.
[TakeBlueToothManager initWithUserInfo:^(TakeUserModel * _Nonnull user) {
user.age = 24;
user.sex = 1;
user.weight = 140.5;
}];
初始化完成后会自动开始扫描周围设备,当然也可以手动调用扫描周围设备方法
[[TakeBlueToothManager sharedBlueToothManager] startScanBlueToothDevices];
监听消息,所有消息将以通知的形式发送,所有通知的key存放在TakeNotificationKey中 详请查阅注释
//发现新的设备
FOUNDATION_EXTERN NSString * const tkNoticeBlueToothDidFindNewDevice;
//设备连接失败
FOUNDATION_EXTERN NSString * const tkNoticeBlueToothConnectDeviceFild;
//设备成功连接
FOUNDATION_EXTERN NSString * const tkNoticeBlueToothDidConnected;
//收到设备发送的消息
FOUNDATION_EXTERN NSString * const tkNoticeBlueToothNewValue;
//蓝牙模块未开启
FOUNDATION_EXTERN NSString * const tkNoticeBlueToothPoweredOff;
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//TakeNotificationKey 存放了sdk中所有通知使用的key
[tkNotificationCenter addObserver:self selector:@selector(kNoticeBlueToothDidConnected:) name:tkNoticeBlueToothDidConnected object:nil];
[tkNotificationCenter addObserver:self selector:@selector(kNoticeBlueToothDidFindNewDevice) name:tkNoticeBlueToothDidFindNewDevice object:nil];
[tkNotificationCenter addObserver:self selector:@selector(kNoticeBlueToothConnectDeviceFild) name:tkNoticeBlueToothConnectDeviceFild object:nil];
[tkNotificationCenter addObserver:self selector:@selector(kNoticeBlueToothNewValue:) name:tkNoticeBlueToothNewValue object:nil];
[tkNotificationCenter addObserver:self selector:@selector(kNoticeBlueToothPoweredOff) name:tkNoticeBlueToothPoweredOff object:nil];
}
kNoticeBlueToothDidFindNewDevice 发现新的设备,所有发现的设备存储于blueToothDevices
-(void)kNoticeBlueToothDidFindNewDevice{
[self.centerManager.blueToothDevices enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//设备的peripheral
CBPeripheral *peripheral = obj[0];
//信号值
NSString * rssi = obj[1];
//扫描到设备的时间 (时间戳 单位为毫秒)
NSString *scanTime = obj[2];
if ([peripheral.name containsString:@"TAKE69060081"]) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self.centerManager connectPeripheral:peripheral];
});
}
}];
}
成功建立连接 参数为字典 保存了当前连接的CBPeriphera, key为@"peripheral"
-(void)kNoticeBlueToothDidConnected:(NSNotification *)nofi{
CBPeripheral *peripheral = nofi.userInfo[@"peripheral"];
self.currentPeripheral = peripheral;
NSLog(@"%@",peripheral.name);
}
接收到设备发来的数据,
//key为hrtValues 包含3个参数 hrt cal ck 类型均为NSString
-(void)kNoticeBlueToothNewValue:(NSNotification *)nofi{
NSDictionary *hrtValues = nofi.userInfo[@"hrtValues"];
//当前心率
NSString *hrt = hrtValues[@"hrt"];
//当前卡路里数 注:cal为累加值 既从连接开始后随着时间累加cal的值 直至断开连接,断开连接后或重新连接后会清空所有值 请妥善保存
NSString *cal = hrtValues[@"cal"];
//当前ck值 同卡路里
NSString *ck = hrtValues[@"ck"];
NSLog(@"%@",hrtValues);
}
至此您以完成TakeFitSDK for iOS的集成,感谢使用.
网友评论