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

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

CBCentralManager类

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


- (void)centralManagerDidUpdateState:(CBCentralManager *)central


//开始扫描周边蓝牙设备
停止扫描
serviceUUIDSoptions为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
















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





    推荐阅读