`
_wyh
  • 浏览: 59421 次
社区版块
存档分类
最新评论

ios CoreBluetooth中央设备的实现

    博客分类:
  • ios
阅读更多

中央设备的实现大体分为以下步骤:

1,创建中央管理类,CentralManager

2,扫描并发现外设。

3,连接外设。

4,扫描连接上外设的所有服务。

5,扫描所有搜索到的服务的特征。

6,读或写或订阅特征。

 

 

具体实现:

 

1,同外设相同,先引入CoreBluetooth。之后实现两个协议,分别是CBCentralManagerDelegate,CBPeripheralDelegate。在.h中声明一个CBCentralManager,名为centralManager。在声明一个可变数组,用来存取所有连接的外设。

#import <CoreBluetooth/CoreBluetooth.h>
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) NSMutableArray *peripherals;

 

2,在.m中初始化.h中声明过的centralManager.

centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: nil options:nil];

 

3,实现代理方法centralManagerDidUpdateState,和外围设备的实现一样,检测设备是否支持是否打开了蓝牙。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    switch (central.state) {
    case CBPeripheralManagerStatePoweredOn:
        [central scanForPeripheralsWithServices:nil options:nil];
        break;
    default:
        NSLog(@"不支持或未打开蓝牙功能.");
        break;
    }
}

    上面的scanForPeripheralsWithServices:nil是用来执行扫描外设的方法。

 

4,因为上面执行了scanForPeripheralsWithServices:nil,所以接下来应该实现扫描到外设后的方法。

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

    if ([peripheral.name isEqualToString:kPeripheralName]) {

        if(![self.peripherals containsObject:peripheral]){
            [self.peripherals addObject:peripheral];
        }        
        [self.centralManager connectPeripheral:peripheral options:nil];
    }
}

     如果数组中已经有了搜索到的设备,就直接connect。如果没有,就将该外设加入到数组中,然后再connect。connectPeripheral方法是用来连接搜索到的外设。

 

5,连接到外部设备

- (void)centralManager:(CBCentralManager *)central 
        didConnectPeripheral:(CBPeripheral *)peripheral
        {
    [self.centralManager stopScan];
    peripheral.delegate = self;
    [peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
}

    因为连接了外设,所以使用stopScan方法停止扫描。之后执行discoverService来扫描服务。

 

6,扫描到外设的服务后,会调用didDiscoverServices。所以实现didDiscoverServices方法。

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

    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    for (CBService *service in peripheral.services) {
        if([service.UUID isEqual:serviceUUID]){
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

    搜索到了服务后,执行discoverCharacteristics方法来搜索所有的特征。

 

7,扫描到了服务的特征之后,为特征实现读或写或订阅。

- (void)peripheral:(CBPeripheral *)peripheral 
        didDiscoverCharacteristicsForService:(CBService *)service 
             error:(NSError *)error
{
    CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
    if ([service.UUID isEqual:serviceUUID]) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:characteristicUUID]) {

                [peripheral setNotifyValue:YES forCharacteristic:characteristic];

            }
        } 
    }
}

      以上是搜索服务中的所有特征,如果某个特征符合我要寻找的特征,就对该特征执行一些操作。

      上面代码用的是订阅,还可以读,写,这取决于该特征在外设中定义时定义的类型。

readValueForCharacteristic   //读
setNotifyValue   //订阅
writeValue   //写

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics