ZHBLE-Bluetooth的封装库

ZHBLE 使用Block回调方式,旨在使调用系统CoreBluetooth库简单明了.

Screenshot0
Screenshot1

Screenshot2
Screenshot3

Features

  • 基于原生CoreBluetooth,回调函数全部封装成Block方式,使调用相关函数简洁明了。
  • 设备作为Central端和Peripheral端都有封装。
  • 采用工厂模式和Block结合使得初始化和函数调用更容易。

Introduce

类名 作用及用法
ZHBLECentral 设备作为Central端的相关属性和操作例如:初始化Central,扫描,连接,检索设备等。
ZHBLEPeripheral 对Peripheral端的相关操作例如:发现服务和特征,监听,读写等操作。
ZHBLEPeripheralManager 设备作为Peripheral端时的相关操作例如:CBPeripheralManager的初始化,广播,添加服务,发送数据等。
ZHBLEStoredPeripherals 设备本地缓存相关操作
ZHBLEManager 快捷访问最近连接的设备
ZHBLEBlocks 所有Block定义

Usage

CocoaPods (recommended)

pod 'ZHBLE'

Central

#import "ZHBLE.h"

self.central = [ZHBLECentral sharedZHBLECentral];

//扫描

[self.central scanPeripheralWithServices:uuids options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @(YES)} onUpdated:^(ZHBLEPeripheral *peripheral,NSDictionary *data){
        if (peripheral) {
            
            //Do Something
        }
       
    }];

//连接

[self.central connectPeripheral:peripheral options:nil onFinished:^(ZHBLEPeripheral *peripheral, NSError *error){
}onDisconnected:^(ZHBLEPeripheral *peripheral, NSError *error){
                    
        });
    }];

Peripheral

#import "ZHBLE.h"


self.peripheralManager = [ZHBLEPeripheralManager sharedZHBLEPeripheralManager];

//广播

 CBUUID *temUUID = [CBUUID UUIDWithString:@"902DD287-69BE-4ADD-AACF-AA3C24D83B66"];
        NSArray *temUUIDArray = [NSArray arrayWithObjects:temUUID, nil];
        NSDictionary *temServiceDic = @{CBAdvertisementDataServiceUUIDsKey:temUUIDArray};
        [self.peripheralManager startAdvertising:temServiceDic onStarted:^(NSError *error){
                       
        }];


//添加服务
[self.peripheralManager addService:_transferService onFinish:^(CBService *service,NSError *error){
        
            }];
            
            

你可能感兴趣的:(ZHBLE-Bluetooth的封装库)