CoreBluetooth框架用于和第三方蓝牙设备的通讯,不如蓝牙灯泡的控制,职能手环,手表,防丢器等的运用
1;通讯流程
扫瞄-连接-通讯-断开
2;常用方法
#import <CoreBluetooth/CoreBluetooth.h>//引用框架
@interface BLEDemoViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>//所需要实现的协议
@property(nonatomic,retain)CBCentralManager *mycentralManager;
@property(nonatomic,retain)CBPeripheral *myperipheral;
@property(nonatomic,retain)CBCharacteristic *mycharacteristic;
@end
@implementation BLEDemoViewController
#pragma mark - 转化UUID格式 为字符串格式
-(NSString *)getdeviceIdForstring:(CFUUIDRef)UUID{
NSString *uuid=[NSString stringWithCString:[self UUIDToString:UUID]
encoding:NSUTF8StringEncoding];
return uuid;
}
-(const char *) UUIDToString:(CFUUIDRef)UUID{
if (!UUID) return "NULL";
CFStringRef s = CFUUIDCreateString(NULL, UUID);
return CFStringGetCStringPtr(s, 0);
}
- (IBAction)scanForPeripheralsWithServices:(id)sender{
//搜索蓝牙设备
NSDictionary *options=[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
if (!self.mycentralManager) {
//实例中央服务
self.mycentralManager =[[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];
}
[self.mycentralManager scanForPeripheralsWithServices:nil options:options];//开始扫瞄
}
- (IBAction)disconnection:(id)sender{
if (self.mycentralManager) {
[self.mycentralManager cancelPeripheralConnection:self.myperipheral];//断开连接
[self.mycentralManager stopScan];//停止扫瞄
}
}
- (IBAction)send:(id)sender{
Byte header[4]={128,32,90,255};//对蓝牙设备写入数据
NSData *data=[[NSData alloc]initWithBytes:&header length:4];
[self.myperipheral writeValue:data forCharacteristic:self.mycharacteristic type:CBCharacteristicWriteWithResponse];
}
#pragma mark - CBCentralManagerDelegate协议方法
//判断设备蓝牙状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
if ([central state] == CBCentralManagerStatePoweredOff) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"蓝牙已关闭" message:@"是否打开蓝牙?" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
//发现外围设备
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
if (!self.myperipheral.isConnected) {
if (!peripheral.isConnected) {
self.myperipheral = [peripheral retain];//此处必须要retain
[self.mycentralManager connectPeripheral:peripheral options:nil];//连接设备 连接成功调用相应的协议(如下协议)
}
}
//连接外围设备
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
peripheral.delegate=self;
[peripheral discoverServices:nil];//连接设备,读取服务uuid等信息
}
//取消连接设备
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
NSLog(@"the BLE is disConnected!");
}
#pragma mark - CBPeripheralDelegate 协议方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for (CBService *service in [peripheral services]) {//扫瞄所有的服务uuid
// NSLog(@"service UUID:%@",service.UUID);
[peripheral discoverCharacteristics:nil forService:service];//搜索服务uuid中的特征 搜索成功调用对应的协议
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for (CBCharacteristic *characteristic in [service characteristics]) {//遍历特征
// NSLog(@"characteristic UUID===:%@",characteristic.UUID);
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"ffb2"]]) {
self.mycharacteristic = [characteristic retain];
[peripheral readValueForCharacteristic:characteristic];//读取特征数值
[peripheral setNotifyValue:YES forCharacteristic:characteristic];//监听特征值的变化 比如心率变化的监听
}
}
}
//读取返回指定特性的数据
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
// NSString *values=[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
// NSLog(@"UUID:%@",characteristic.UUID);
// NSLog(@"UUID:%@====Value:%@",characteristic.UUID,values);
}
//对蓝牙设备写入数据
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
// NSLog(@"characteristic :%@",characteristic.value);
[self.myperipheral readValueForCharacteristic:characteristic];
}
@end