IOS 蓝牙与外设交互

 1, 使用  

 CBCentralManager *   _manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

 初始化一个中央设备(_manager);


判断手机是否开启了蓝牙只有central.state=CBCentralManagerStatePoweredOn  才能去链接蓝牙设备

//开始查看服务,蓝牙开启

-(void)centralManagerDidUpdateState:(CBCentralManager *)central

{

    switch (central.state) {

        case CBCentralManagerStatePoweredOn:

            [self updateLog:@"蓝牙已打开,请扫描外设"];

            break;

        default:

            break;

    }

}



调用

 [_manager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];

扫描外设蓝牙。


//查到外设后,停止扫描,连接设备

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

{

    [self updateLog:[NSString stringWithFormat:@"已发现 peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData]];

    _peripheral = peripheral;

    NSLog(@"%@",_peripheral);

//停止扫描,在这里可根据peripheral.UUID去链接想要的蓝牙设备

    [self.manager stopScan];

    //[_activity stopAnimating];

    BOOL replace = NO;

    // Match if we have this device from before

    for (int i=0; i < _nDevices.count; i++) {

        CBPeripheral *p = [_nDevices objectAtIndex:i];

        if ([p isEqual:peripheral]) {

            [_nDevices replaceObjectAtIndex:i withObject:peripheral];

            replace = YES;

        }

    }

    if (!replace) {

        [_nDevices addObject:peripheral];

        [_deviceTable reloadData];

    }

}


_deviceTable可显示多个蓝牙设备。


使用

        [self.manager connectPeripheral:_peripheral options:nil];

去链接蓝牙

断开蓝牙调用

[self.manager cancelPeripheralConnection:_peripheral];


//连接外设成功,开始发现服务

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

    [self updateLog:[NSString stringWithFormat:@"成功连接 peripheral: %@ with UUID: %@",peripheral,peripheral.UUID]];

    [self.peripheral setDelegate:self];

    [self.peripheral discoverServices:nil];

    [self updateLog:@"扫描服务"];

}


//连接外设失败

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

{

    NSLog(@"%@",error);

}


当rssi发生变化时

-(void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error

{

    //NSLog(@"%s,%@",__PRETTY_FUNCTION__,peripheral);

    int rssi = abs([peripheral.RSSI intValue]);

    CGFloat ci = (rssi - 49) / (10 * 4.);

    NSString *length = [NSString stringWithFormat:@"发现BLT4.0热点:%@,距离:%.1fm",_peripheral,pow(10,ci)];

    NSLog(@"距离:%@",length);

}



//已发现服务

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

    

    [self updateLog:@"发现服务."];

    int i=0;

    for (CBService *s in peripheral.services) {

        [self.nServices addObject:s];

    }

    for (CBService *s in peripheral.services) {

        [self updateLog:[NSString stringWithFormat:@"%d :服务 UUID: %@(%@)",i,s.UUID.data,s.UUID]];

        i++;

        [peripheral discoverCharacteristics:nil forService:s];

    }

}


//已搜索到Characteristics

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

    [self updateLog:[NSString stringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID]];

    

    for (CBCharacteristic *c in service.characteristics) {

        [self updateLog:[NSString stringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];

        

        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"2A06"]]) {

            _writeCharacteristic = c;

        }

        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {

            [_peripheral readValueForCharacteristic:c];

        }

        

        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FFA1"]]) {

            [_peripheral readRSSI];

        }

        [_nCharacteristics addObject:c];

    }

}



//获取外设发来的数据,不论是readnotify,获取数据都是从这个方法中读取。

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

{

    // BOOL isSaveSuccess;

    

    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A19"]]) {

        NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];

        _batteryValue = [value floatValue];

        NSLog(@"电量%f",_batteryValue);

    }

    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFA1"]]) {

        NSString *value = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];

        //_batteryValue = [value floatValue];

        NSLog(@"信号%@",value);

    }


    else

    NSLog(@"didUpdateValueForCharacteristic%@",[[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]);

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值