IOS ZENGGE Sdk接入

1.开发前准备

1.1 CocosPod环境

本sdk依赖cocopod环境,请先确保项目已初始化cocospod框架

1.2 引入ZGLedSdk.framework

将提供的ZGLedSdk.framework拖入工程项目即可

1.2.1

同时在工程中通过CocoPods引入sdk所需三方库

  pod 'CocoaAsyncSocket', '~> 7.4.3'
  pod 'NSDate+Calendar', '~> 0.0.8'
  pod 'NSDate+Helper', '~> 1.0.4'
  pod 'JSONModel'
  pod 'MQTTClient', '~> 0.15.3'

1.3 项目设置

在工程项目的target->Build Settings->Allow Non-modular Includes In Framework Modules 设为YES

1.3.1

在工程项目的target->Build Settings->Linking->Other Link Flags增加-ObjC标记

1.3.2

1.4 申请权限

请在项目info.plist加入相关权限

1.4.1

1.5 sdk使用

在需要sdk的文件中引入头文件即可

#import <ZGLedSdk/ZGLedSdk.h>

2.设备配网

2.1 动态申请权限

​ 要使用发现设备功能在ios13以上需动态申请权限,请上网查阅或参考demo代码

2.2 搜索所有可配网设备

2.2.1 声明ZGHBDeviceProvisionHandler和ZGHBDeviceHadler类,并添加到DisBroad类(注:需准备一个唯一全局类存放DisBroad类以供后续调用)

//AppDelegate.h
@property(strong,nonatomic)DisBoard* disBoard;
//AppDelegate.m
self.disBoard=[DisBoard shareDisBoard];
ZGHBDeviceHandler* deviceHandler=[[ZGHBDeviceHandler alloc]initWithMacAddressList:[NSMutableArray array]];
ZGHBDeviceProvisionHandler* provisionHandler=[[ZGHBDeviceProvisionHandler alloc] init];
[self.disBoard addHandler:deviceHandler];
[self.disBoard addHandler:provisionHandler];

2.2.2 启动配网

self.deviceInfoList=[NSMutableArray array];
ZGHBDeviceHandler* deviceHandler=(ZGHBDeviceHandler*)[[AppDelegate Current].disBoard getHandler:ZGHBDeviceHandler.class];
ZGHBDeviceProvisionHandler* provisionHandler=(ZGHBDeviceProvisionHandler*)[[AppDelegate Current].disBoard getHandler:ZGHBDeviceProvisionHandler.class];
[provisionHandler start:5 timeOutBlock:^{
  //        NSLog(@"scan time out");
}];
[[AppDelegate Current].disBoard start];

2.2.3 搜索设备回调

@property(strong,nonatomic)NSMutableArray<NSString*>* deviceMacList;
self.deviceMacList=[NSMutableArray array];

//注册通知
ZGHBDeviceProvisionHandler* provisionHandler=(ZGHBDeviceProvisionHandler*)[disBoard getHandler:ZGHBDeviceProvisionHandler.class];
[provisionHandler.notificationCenter addObserver:self selector:@selector(onDeviceUpdate:) name:NotificationDevicesUpdate object:nil];

//设备搜索回调方法
-(void)onDeviceUpdate:(NSNotification*)notification{
  NSDictionary* info=notification.userInfo;
  BaseDevice* device=[info objectForKey:@"device"];
  if (device==nil) {
    return;
  }
  if (![self.deviceMacList containsObject:device.macAddress]) {
    [self.deviceMacList addObject:device.macAddress];
    DisBoard* disBoard=[AppDelegate Current].disBoard;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      ZGHBDeviceHandler* deviceHandler=(ZGHBDeviceHandler*)[disBoard getHandler:ZGHBDeviceHandler.class];
      if (deviceHandler!=nil) {
        [deviceHandler addMacAddress:device.macAddress];//配网成功添加到ZGHBDebviceHandler,则ZGHBDebviceHandler才会把搜到的设备传递给上层处理
      }
      ZGHBDeviceProvisionHandler* provisionHandler=(ZGHBDeviceProvisionHandler*)[disBoard getHandler:ZGHBDeviceProvisionHandler.class];
      if (provisionHandler!=nil) {
        [provisionHandler addMacAddress:device.macAddress];//配网成功添加到ZGHBDeviceProvisionHandler,则ZGHBDeviceProvisionHandler跳过对已配网设备处理
      }
    });
  }
}

3.设备控制

3.1 搜索所有可控制设备

3.1.1 ZGHBDeviceHadler添加监听回调

ZGHBDeviceHandler* deviceHandler=(ZGHBDeviceHandler*)[[AppDelegate Current].disBoard getHandler:ZGHBDeviceHandler.class];
[deviceHandler.notificationCenter addObserver:self selector:@selector(onDeviceUpdate:) name:NotificationDevicesUpdate object:nil];

3.1.2 监听回调

-(void)onDeviceUpdate:(NSNotification*)notification
{
    NSDictionary* info=notification.userInfo;
    BaseDevice* device=[info objectForKey:@"device"];
    if (device==nil) {
        return;
    }
    if ([device isKindOfClass:[ZGHBDevice class]]) {
        ZGHBDevice *zghbDevice = (ZGHBDevice*)device;
        //Do what you want to do
    }
}

3.2 发送控制指令

3.2.1 发送写命令(不需要返回结果)

NSData* payload=[[NSData alloc]init];//具体数据请参考协议

ZGHBDeviceHandler* deviceHandler=(ZGHBDeviceHandler*)[[AppDelegate Current].disBoard getHandler:ZGHBDeviceHandler.class];
[deviceHandler writeTo:macAddress withCmdId:cmdId withParams:payload withSuccessBlock:^(ZGHBDevice * _Nonnull device) {
  NSLog(@"write success");
} withFailBlock:^(NSError * _Nonnull error) {
  NSLog(@"write error:%@",error.description);
}];

3.2.2 发送请求指令(需要返回结果)

NSData* payload=[[NSData alloc]init];//具体数据请参考协议

ZGHBDeviceHandler* deviceHandler=(ZGHBDeviceHandler*)[[AppDelegate Current].disBoard getHandler:ZGHBDeviceHandler.class];
[deviceHandler requestTo:macAddress withCmdId:cmdId withParams:payload withSuccessBlock:^(NSDictionary * _Nonnull dic, ZGHBDevice * _Nonnull device) {

} withFailBlock:^(NSError * _Nonnull error) {

}];

4.相关协议

蓝牙SDK产品协议: https://docs.qq.com/sheet/DSUp0b3FpcW5uWVVO?u=86d4eef15cbd46dea4d1b72cb51c5f91&amp&tab=ri6a2d&tab=BB08J2

results matching ""

    No results matching ""