CoreBluetooth使用 (服务端中心设备)

自己写了一个Demo    下载地址:http://download.csdn.net/detail/i_k_o_x_s/9031359

CBCentralManager类

//初始化的时候 调用代理方法   返回当前设备的蓝牙状态

- (void)centralManagerDidUpdateState:(CBCentralManager *)central


//开始扫描周边蓝牙设备

 停止扫描

 serviceUUIDS  options  为nil  不指定搜索条件

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;

- (void)stopScan;



//  当设备搜索到周边蓝牙设备的时候  回调代理方法

   peripheral  设备        advertisementData   设备广告发出来的信息   RSSI  设备强度

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI


// 连接指定蓝牙设备

- (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options;

//取消连接指定蓝牙设备

- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;


// 和周边蓝牙设备连接成功  回调代理方法

-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

//设置代理

    peripheral.delegate =self;

    [peripheral discoverServices:nil];

}


// 和周边蓝牙设备连接失败  回调代理方法

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error



CBPeripheral类

CBPeripheralDelegate 代理

// 外部设备寻找到服务后   回调代理方法

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

// 外围设备查找指定服务中的特征  服务

- (void)discoverCharacteristics:(NSArray *)characteristicUUIDs forService:(CBService *)service;


//外围设备寻找到特征后    回调代理方法

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{


// 设置激活通知  到指定特征

- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;


//特征值被更新后  回调代理回调

-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{


// 订阅特征 更新数据   回调代理方法

characteristic.value 为特征发过来的数据

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

//客户端  CBPeripheralManager类 调用这个方法

                - (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(NSArray *)centrals;



//写入数据到指定特征   客户端会接收回调代理方法

- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

//客户端  CBPeripheralManager类 调用这个方法

-(void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests{




  //成功写入后回调的代理方法


- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

















你可能感兴趣的:(iOS-蓝牙模块)