码迷,mamicode.com
首页 > 移动开发 > 详细

蓝牙手机中心设备连接外围设备发现其服务,读取其特性,然后加以更新.

时间:2015-03-04 18:26:46      阅读:1309      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ViewController.m
//  BLE1
//
//  Created by Shawn on 15-1-29.
//  Copyright (c) 2015年 BIll-JC. All rights reserved.
//

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate,CBPeripheralManagerDelegate>
@property (nonatomic, strong)CBCentralManager *centralManager;
@property (nonatomic, strong)NSMutableArray *peripherals;
@end

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self scanPeripherals];
    
}
#pragma mark - 扫描外围设备
- (void)scanPeripherals{
    
    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
    if (self.centralManager.state == CBCentralManagerStatePoweredOn) {
        [self.centralManager scanForPeripheralsWithServices:nil options:nil];
        
    }
}
#pragma mark - CBCentralManagerDelegate方法,蓝牙设备状态开启的回调
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    if (central.state == CBCentralManagerStatePoweredOn) {
        [self.centralManager scanForPeripheralsWithServices:nil options:nil];
        
    }
}
#pragma mark - 连接外围的蓝牙设备.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    [self.centralManager stopScan];
    if (![self.peripherals containsObject:peripheral]) {
        peripheral.delegate = self;
        NSLog(@"peripheral:%@",peripheral);
        self.peripherals = [@[]mutableCopy];
        [self.peripherals addObject:peripheral];//连接外围设备之前应该要先保留之,否则,ARC编译器会释放外围设备对象而导致其无法连接,通过加入一个维护外围设备列表的数组来保留.
        [self.centralManager connectPeripheral:peripheral options:nil];
    }
    //知道外围设备的标示符,可以使用以下方法而不需要扫描.保存这个数组每次要扫描设备之前先尝试连接已知外围设备是个好习惯,扫描比较费电,应该尽可能避免.
    NSArray *peripherals1 = [self.centralManager retrievePeripheralsWithIdentifiers:@[[CBUUID UUIDWithString:@"7BDDC62C-D916-7E4B-4D09-285E11164936"]]];
    
}
#pragma mark - 获得外围设备提供的服务列表,这也是CBManagerDelegate中的一个委托方法,发现服务,如果连接成功,在以下方法中可以知道,下一步是发现外围设别提供的服务.
//从外围设备的services属性中可以获取服务列表
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    [peripheral discoverServices:nil];
}
#pragma mark - 得到服务之后就可以发现其特性了,一个服务通常有一个或多个特性
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    [peripheral.services enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        CBService *service = obj;
        if ([service.UUID isEqual:[CBUUID UUIDWithString:@"F000AA00-0451-4000-B000-000000000000"]]){
            //[peripheral discoverCharacteristics:nil forServices:service];
        }
                    }];//[peripheral discoverCharacteristics:nil forServices:service];
}
#pragma mark - 打开获取传感器读数特性的通知功能,返回已经发现特性的委托方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    [service.characteristics enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        CBCharacteristic *ch = obj;
        if ([ch.UUID isEqual:[CBUUID UUIDWithString:@"F000AA02-0451-4000-B000-000000000000"]]) {
            uint8_t data = 0x01;
            [peripheral writeValue:[NSData dataWithBytes:&data length:1] forCharacteristic:ch type:CBCharacteristicWriteWithResponse];
        }
        if ([ch.UUID isEqual:[CBUUID UUIDWithString:@"F000AA01-0451-4000-B000-000000000000"]]) {
            [peripheral setNotifyValue:YES forCharacteristic:ch];
        }
    }];
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    float temp = [self temperatureFromData:characteristic.value];
    NSLog(@"Room temperature:%f",temp);
    
}
- (float)temperatureFromData:(NSData *)data
{
    char scratchVal[data.length];
    int16_t ambTemp;
    [data getBytes:&scratchVal length:data.length];
    ambTemp = ((scratchVal[0] & 0xff) | ((scratchVal[3] << 8) & 0xff00));
    return (float)((float)ambTemp / (float)128);
}
//1100999077786678
@end

  

蓝牙手机中心设备连接外围设备发现其服务,读取其特性,然后加以更新.

标签:

原文地址:http://www.cnblogs.com/longxue1991/p/4313790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!