Skip to content
coolnameismy edited this page Mar 14, 2016 · 2 revisions

Quick Example

app模拟一个,BLE4.0外设,可以被其他设备连接和使用

模拟一个有2个service和6个characteristic的外设

//导入.h文件和系统蓝牙库的头文件
#import "BabyBluetooth.h"
//定义变量
BabyBluetooth *baby;

-(void)viewDidLoad {
    [super viewDidLoad];

    //配置第一个服务s1
    CBMutableService *s1 = makeCBService(@"FFF0");
    //配置s1的3个characteristic
    makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//
    makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//
    makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//可读写,uuid自动生成
    makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段
    makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段
    //配置第一个服务s2
    CBMutableService *s2 = makeCBService(@"FFE0");
    makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读
   
    //实例化baby
    baby = [BabyBluetooth shareBabyBluetooth];
    //配置委托
    [self babyDelegate];
    //启动外设
    baby.bePeripheral().addServices(@[s1,s2]).startAdvertising();
    //启动外设并附带名称(注意,设备的名称是硬件写的,不能修改,软件可以修改的只是广播时候的那个CBAdvertisementDataLocalNameKey)
    //baby.bePeripheralWithName(@"Name of yours").addServices(@[s1,s2]).startAdvertising();
}

//设置蓝牙外设模式的委托
-(void)babyDelegate{

     //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) {
        NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state);
    }];
    
    //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) {
        NSLog(@"didStartAdvertising !!!");
    }];
    
    //设置添加service委托 | set didAddService block
    [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) {
        NSLog(@"Did Add Service uuid: %@ ",service.UUID);
    }];

    //.....
}

外设模式的api

----------------------------------------------------------------------------------------------------------------
//进入外设模式
-(BabyPeripheralManager *(^)()) bePeripheral;

//进入外设模式,并给外设添加一个localname,这个localname会在放在外设发送的广播数据包中
-(BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName;
----------------------------------------------------------------------------------------------------------------
/**
 添加服务
 */
-(BabyPeripheralManager *(^)(NSArray *array))addServices;
----------------------------------------------------------------------------------------------------------------
/**
启动广播
 */
-(BabyPeripheralManager *(^)())startAdvertising;
----------------------------------------------------------------------------------------------------------------
/**
 生成CBService
 */
CBMutableService* makeCBService(NSString *UUID);
----------------------------------------------------------------------------------------------------------------

/**
 *  构造Characteristic,并加入service
 *  service:CBService
 
 *  param`ter for properties :option 'r' | 'w' | 'n' or combination
 *  r                       CBCharacteristicPropertyRead
 *  w                       CBCharacteristicPropertyWrite
 *  n                       CBCharacteristicPropertyNotify
 *  default value is rw     Read-Write

 *  paramter for descriptor:be uesd descriptor for characteristic
 */
void makeCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *properties,NSString *descriptor);

/**
 *  构造一个包含初始值的Characteristic,并加入service,包含了初值的characteristic必须设置permissions和properties都为只读
 *  make characteristic then add to service, a static characteristic mean it has a initial value .according apple rule, it must set properties and permissions to CBCharacteristicPropertyRead and CBAttributePermissionsReadable
*/
void makeStaticCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *descriptor,NSData *data);

----------------------------------------------------------------------------------------------------------------

/**
 生成UUID
 */
NSString* genUUID();

----------------------------------------------------------------------------------------------------------------

外设模式的委托

//peripheral model block

/**
 PeripheralManager did update state block
 */
-(void)peripheralModelBlockOnPeripheralManagerDidUpdateState:(void(^)(CBPeripheralManager *peripheral))block;

/**
 PeripheralManager did add service block
 */
-(void)peripheralModelBlockOnDidAddService:(void(^)(CBPeripheralManager *peripheral,CBService *service,NSError *error))block;

/**
 PeripheralManager did start advertising block
 */
-(void)peripheralModelBlockOnDidStartAdvertising:(void(^)(CBPeripheralManager *peripheral,NSError *error))block;

/**
 peripheralManager did receive read request block
 */
-(void)peripheralModelBlockOnDidReceiveReadRequest:(void(^)(CBPeripheralManager *peripheral,CBATTRequest *request))block;

/**
 peripheralManager did receive write request block
 */
-(void)peripheralModelBlockOnDidReceiveWriteRequests:(void(^)(CBPeripheralManager *peripheral,NSArray *requests))block;

/**
 peripheralManager did subscribe to characteristic block
 */
-(void)peripheralModelBlockOnDidSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;

/**
peripheralManager did subscribe to characteristic block
*/
-(void)peripheralModelBlockOnDidUnSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;

更多参考

Clone this wiki locally