LF Bluetooth iOS Demo Introducti

2018-08-25  本文已影响0人  肚子大脖子细

A Brief Introduction

DEMO Download Address

Instruction

Demo's main page is as below


Simulator Screen Shot - iPhone 8 - 2018-07-30 at 16.54.09.png

The method provided by BLEManager

BLEManager is a single instance object that ensures that Bluetooth is in the same state when invoked on different controllers.

/**
 Instantiation method

 @return Single Case Object
 */
+ (instancetype)shareInstance;

Since our peripherals are body fat scales, you must wear the user's body information to be able to measure, if you do not pass in this information or the incoming information is incorrect, you will not get the Htbodyfat_newsdk object, will only get a Bleprogressdatamodel object.

/**
 Initializing the user's data

 @param height 用户身高  cm
 @param sex 用户性别
 @param unit 用户当前使用的单位
 @param age 用户年龄
 */
- (void)initWithUserHeight:(NSInteger)height sex:(NSInteger) sex unit:(NSInteger)unit age:(NSInteger)age;

Methods to start searching/connecting existing devices The final data will be returned in the way the object is in the callback, and you can use it as you want.

/**
 Start a search/Connect existing device

 @param isBinding 是否为绑定
 @param progressDataHandle 过程数据回调
 @param lockDataHandle 锁定数据回调
 @param historyDataHandle 历史数据回调
 */
- (void)startSearchingPeripheralWithIsBinding:(BOOL)isBinding progressDataHandle:(progressDataHandle)progressDataHandle lockDataHandle:(lockDataHandle)lockDataHandle historyDataHandle:(historyDataHandle)historyDataHandle;

Stop Search

/**
 Stop Search
 */
- (void)stopScan;

Re-open Search

/**
 Re-open Search
 */
- (void)reStartScan;

Disconnect the current linked Bluetooth device

/**
Disconnect the current linked Bluetooth device
 
 */
- (void)disconnectPeripheral:(CBPeripheral *)peripheral;

BLEManger has done for you the logic of device management, when you go to connect peripherals in the binding state will filter out devicelist in the device through the mac address, in the connection state will filter out the devicelist of the device through the mac address. In this way you can get a list of binding devices

/**
 Binding Device List
 */
+ (NSArray <BLEDeviceModel *> *)deviceList;

When saving, you need to pass in an array of device lists. The main reason is that because of your business logic, you may need to maintain a list of devices yourself. Whether you add or delete devices, you can directly list BLEManager devices. Replace once.

/**
 Save device to device list
 */
+ (void)saveDeviceArr:(NSArray<BLEDeviceModel *> *)modelArr;

Delete all devices

/**
  Delete all devices
 */
+ (void)deleteAllDeviceArr;

Send a message to the currently connected device

/**
 Send a message to the currently connected device
 
 */
- (void)writeData:(NSData *)data toPeripheral:(CBPeripheral *)peripheral;

BLEManager's use

First, let's talk about the structure of BLEManager:


image.png

BLEManager as a management class to return the raw data of the peripherals of BLEConnect
After the Bledatahandle process is thrown to the outermost layer, the user can care nothing about the whole internal implementation process, and manage the peripherals through Bledevicemanager.

This is mainly about how to add or modify the name of the peripheral that needs to be filtered. The names of some peripherals are defined in BLEDefine.h.

#define kBLEDeviceEnergyScale @"Energy Scale"
#define kBLEDeviceHealthScale @"Health Scale"
#define kBLEDeviceADore @"ADORE"
#define kBLEDeviceLFScale @"LFScale"

These names in the bleconnectfilter.plist have a corresponding data resolution protocol, this is with the hardware engineer has been agreed, after bleconnectfilter filtering, Bleprotocohandle will use the corresponding protocol scheme to the peripheral returned to the original data assembly. If you need to modify the Bluetooth name, in the case of determining the same set of Bluetooth protocol scheme, directly modify the name of BLEDefine.h and Bleconnectfilter.plist.

image.png

Specific method calls and logic can refer to the code in Bindingdeviceviewcontroller and Connectdeviceviewcontroller.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bleConnectStateLabel.text = @"未连接";
    [[BLEManager shareInstance] initWithUserHeight:self.height sex:self.sex unit:self.unit age:self.age];
    [[BLEManager shareInstance] startSearchingPeripheralWithIsBinding:NO progressDataHandle:^(BLEProgressDataModel *progressDataModel, CBPeripheral *peripheral) {
        self.peripheral = peripheral;
        self.currentWeightLabel.text = [UnitTool weight:progressDataModel.weight withUnit:self.unit];
        self.bleConnectStateLabel.text = @"已连接";
    } lockDataHandle:^(BOOL isBodyFatModel, HTBodyfat_NewSDK *bodyFatModel, BLEProgressDataModel *progressDataModel, BLEDeviceModel *deviceModel, CBPeripheral *peripheral) {
        self.peripheral = peripheral;
        self.BodyDataHandler(isBodyFatModel, bodyFatModel, progressDataModel);
        [[BLEManager shareInstance] disconnectPeripheral:self.peripheral];
        [self.navigationController popViewControllerAnimated:YES];
    } historyDataHandle:^(BOOL isBodyFatModel, NSInteger impedance, CGFloat weight, BLEDeviceModel *deviceModel, CBPeripheral *peripheral, NSString *dateStr, BOOL isFinish) {
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[BLEManager shareInstance] disconnectPeripheral:self.peripheral];
}

It is simple to call.

  1. Initialization of user information
  2. Scan peripherals and handle business logic in callbacks
  3. Disconnect after receiving locked data
   [[BLEManager shareInstance] startSearchingPeripheralWithIsBinding:NO progressDataHandle:^(BLEProgressDataModel *progressDataModel, CBPeripheral *peripheral) {

    } lockDataHandle:^(BOOL isBodyFatModel, HTBodyfat_NewSDK *bodyFatModel, BLEProgressDataModel *progressDataModel, BLEDeviceModel *deviceModel, CBPeripheral *peripheral) {
  
    } historyDataHandle:^(BOOL isBodyFatModel, NSInteger impedance, CGFloat weight, BLEDeviceModel *deviceModel, CBPeripheral *peripheral, NSString *dateStr, BOOL isFinish) {
    }];

The above method Progressdatahandle returns the data in the weighing process, lockdatahandle is locking the data, because the user may not pass in the information such as the height gender, so the Htbodyfat_newsdk object may be nil, Historydatahandle is a callback that is run by a scale with historical data functions, and if your chosen model supports reading historical data, it will run the callback.

上一篇 下一篇

猜你喜欢

热点阅读