ESpBlufi官网代码Esp32蓝牙配网流程浅析

基于EspBlufi蓝牙配网流程

1.初始化服务

self.espFBYBleHelper = [ESPFBYBLEHelper share];

2.扫码外设

[self.espFBYBleHelper startScan:^(ESPPeripheral * _Nonnull device) {        if ([self shouldAddToSource:device]) {            [self.dataSource addObject:device];            dispatch_async(dispatch_get_main_queue(), ^{                [self.peripheralView reloadData];            });        }    }];

3.连接外设

3.1连接外设

_blufiClient = [[BlufiClient alloc] init];    _blufiClient.centralManagerDelete = self;    _blufiClient.peripheralDelegate = self;    _blufiClient.blufiDelegate = self;    [_blufiClient connect:_device.uuid.UUIDString];

3.2外设连接成功失败回调

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {    [self updateMessage:@"Connet device success"];} 

 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {    [self updateMessage:@"Connet device failed"];    self.connected = NO;}

3.3外设连接状态回调

- (void)blufi:(BlufiClient *)client gattPrepared:(BlufiStatusCode)status service:(CBService *)service writeChar:(CBCharacteristic *)writeChar notifyChar:(CBCharacteristic *)notifyChar {    NSLog(@"Blufi gattPrepared status:%d", status);    if (status == StatusSuccess) {        self.connected = YES;        [self updateMessage:@"BluFi connection has prepared"];        [self onBlufiPrepared];    } else {        [self onDisconnected];        if (!service) {            [self updateMessage:@"Discover service failed"];        } else if (!writeChar) {            [self updateMessage:@"Discover write char failed"];        } else if (!notifyChar) {            [self updateMessage:@"Discover notify char failed"];        }    }}

4.获取外设Mac

4.1自定义发送数据来获取Mac地址

 [self.blufiClient postCustomData:data];

4.2.收到自定义数据是否发送成功回调

- (void)blufi:(BlufiClient *)client didPostCustomData:(nonnull NSData *)data status:(BlufiStatusCode)status {

    if (status == StatusSuccess)

{       

[self updateMessage:@"Post custom data complete"];   

}

else {     

  [self updateMessage:[NSString stringWithFormat:@"Post custom data failed: %d", status]]; 

  }

}

4.3 收到自定义数据回调

在这里就能拿到自定义的Mac信息了

- (void)blufi:(BlufiClient *)client didReceiveCustomData:(NSData *)data status:(BlufiStatusCode)status {   

NSString *customString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];   

[self updateMessage:[NSString stringWithFormat:@"Receive device custom data: %@", customString]];

}

5.给外设发送WiFi信息

5.1发送配网信息

// 参数封装

 BlufiConfigureParams *params = [[BlufiConfigureParams alloc] init];

 params.opMode = OpModeSta;           

params.staSsid = self.WifiSSidTextField.text;           

params.staPassword = self.WifiPasswordTextField.text;

// 发送配网信息

 [_blufiClient configure:params];

5.2 配网信息发送是否成功回调

- (void)blufi:(BlufiClient *)client didPostConfigureParams:(BlufiStatusCode)status {   

if (status == StatusSuccess) {     

  [self updateMessage:@"Post configure params complete"];   

} else {     

  [self updateMessage:[NSString stringWithFormat:@"Post configure params failed: %d", status]]; 

  }

}

5.3 收到配网信息数据回调

- (void)blufi:(BlufiClient *)client didReceiveDeviceStatusResponse:(BlufiStatusResponse *)response status:(BlufiStatusCode)status {    [[NSOperationQueue mainQueue] addOperationWithBlock:^{        [self setButton:self.stateBtn enable:self.connected];    }];    if (status == StatusSuccess) {        [self updateMessage:[NSString stringWithFormat:@"Receive device status:\n%@", response.getStatusInfo]];    } else {        [self updateMessage:[NSString stringWithFormat:@"Receive device status error: %d", status]];    }}

到这里说明,设备已经配网成功了,我们拿到前获取到的Mac,进行上云操作(已云智易sn订阅为例)

6.设备上云操作

6.1调用云智易sn订阅接口

6.2 将设备添加到当前home中

6.3.修改设备名

至此,整个蓝牙配网上云流程结束



核心写入数据方法,加入了Condition锁,确保写入线程安全

- (void)gattWrite:(NSData *)data {        [_writeCondition lock];    if (![self isConnected]) {        [_writeCondition unlock];        return;    }    if (DBUG) {        NSLog(@"Blufi GattWrite Length: %lu,  %@", (unsigned long)data.length, data);    }    [_peripheral writeValue:data forCharacteristic:_writeChar type:CBCharacteristicWriteWithResponse];    [_writeCondition wait];    [_writeCondition unlock];    return;}

官方demo

你可能感兴趣的:(ESpBlufi官网代码Esp32蓝牙配网流程浅析)