美文网首页
LF Bluetooth iOS Demo Introducti

LF Bluetooth iOS Demo Introducti

作者: 肚子大脖子细 | 来源:发表于2018-08-25 15:59 被阅读0次

    A Brief Introduction

    • The peripherals refer to Unique Scales Co., Ltd. Bluetooth body fat scale.
    • Because of the need for Bluetooth connectivity, the demo requires a real machine to run with.
    • If you need information other than your weight, you need to enter height, age, gender and step on with bare feet.
    • The code under the BLE folder makes some simple encapsulation of Bluetooth filtering, connection and data parsing, and finally, it is called by the Blemanager class.
    • The code under the Htbodyfat folder is a number of calculations for the impedance values of the Bluetooth peripherals, and you will get the corresponding data by passing the impedance values in the specified way.
    • All controllers are created with storyboard, using segue to transmit values. All the storyboard are in the Main.stroyboard.

    DEMO Download Address

    Instruction

    Demo's main page is as below


    Simulator Screen Shot - iPhone 8 - 2018-07-30 at 16.54.09.png
    • Click “ Binding device” button to “Binding Device View Controller” page. After this controller is instantiated, it will start scanning nearby peripherals and make a record of your peripherals.

    • Click” Weighing” button to “ConnectDeviceViewController” page. After being instantiated, this controller will also start scanning nearby peripherals and filtering to connect the bound devices. Therefore, the scale can only be weighed after being bound, otherwise the data cannot be received.

    • Click” Device management” button to “ConnectDeviceViewController” page. This controller will show you the peripherals bound to the "Binding Devices" page in a list. You can remove the bound device by right-sliding.

    • After receiving the data returned by the peripherals on the bind devices and weigh on scales pages, automatically stops the scan and disconnects the peripherals, and then returns the data back to the "home page" to update the weight column, which can be viewed by the Data details page.

    • Click on the ”Data Details” button to jump to Bodydetailviewcontroller, where the controller will display the properties of the Htbodyfat_newsdk object in the form of a list, and if the impedance value is not measured, the data in the list will be empty.

    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.

    相关文章

      网友评论

          本文标题:LF Bluetooth iOS Demo Introducti

          本文链接:https://www.haomeiwen.com/subject/nglgiftx.html