peripheral_model

coolnameismy edited this page Mar 14, 2016 · 2 revisions

Quick Example

let you apps be a BLE4.0 peripheral

mock a peripheral which has 2 service and 6 characteristic together

//import head files
#import "BabyBluetooth.h"
//define var
BabyBluetooth *baby;

-(void)viewDidLoad {
    [super viewDidLoad];

    //config first service
    CBMutableService *s1 = makeCBService(@"FFF0");
    //config s1‘s characteristic
    makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//can read
    makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//can write
    makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//can read,write ,uuid be automatically generate
    makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//default property is rw
    makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//can notiy
    //config seconed service s2
    CBMutableService *s2 = makeCBService(@"FFE0");
    //a static characteristic and has cached vuale ,it must be only can read.
    makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);
   
    //init BabyBluetooth
    baby = [BabyBluetooth shareBabyBluetooth];
    //config delegate
    [self babyDelegate];
    //let peripheral add services and start advertising
    baby.bePeripheral().addServices(@[s1,s2]).startAdvertising();
    //start with name (note,the peripheral.name is only decide by hardwave,only we can modify is CBAdvertisementDataLocalNameKey in the peripharal advertising)
    //baby.bePeripheralWithName(@"Name of yours").addServices(@[s1,s2]).startAdvertising();
}

//set baby peripheral model delegate
-(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);
    }];

    //.....
}

peripheral model api

----------------------------------------------------------------------------------------------------------------
//inter peripheral model
-(BabyPeripheralManager *(^)()) bePeripheral;

//inter peripheral model ,and provide a localname which will appear in peripheral advertising 
-(BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName;
----------------------------------------------------------------------------------------------------------------
/**
add services
 */
-(BabyPeripheralManager *(^)(NSArray *array))addServices;
----------------------------------------------------------------------------------------------------------------
/**
start advertising
 */
-(BabyPeripheralManager *(^)())startAdvertising;
----------------------------------------------------------------------------------------------------------------
/**
 make a CBService
 */
CBMutableService* makeCBService(NSString *UUID);
----------------------------------------------------------------------------------------------------------------

/**
 *  make a Characteristic,then add to 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);

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

/**
 generate a UUID
 */
NSString* genUUID();

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

peripheral model delegate

//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;

more reference

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.