iOS开发 CoreBluetooth 蓝牙4.0

本文详细介绍了使用iOS CBCentralManager API进行蓝牙设备的扫描、连接及特征值读写的步骤,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

github demo 下载地址,有问题请评论或者在git上提交问题

1. 用到的类,基本和HomeKit差不多

类名描述
CBCentralManager蓝牙管理,用于扫描和链接蓝牙硬件
CBPeripheral蓝牙设备
CBService蓝牙设备的服务
CBCharacteristic服务的描述特征值

2. 步骤

1. 创建管理者CBCentralManager,进行扫描
2. 获得扫描的设备CBPeripheral,进行连接
3. 扫描设备的服务CBService
4. 获取服务的特征值 CBCharacteristic
5. 特征值的读写
6. 断开连接

3. 代码实现

1. 创建管理者,并开始扫描
     CBCentralManager *blueToothManager =  [[CBCentralManager alloc]initWithDelegate:self queue:nil];
    [blueToothManager scanForPeripheralsWithServices:nil options:nil];
这个时候系统会自动提示打开蓝牙,不需要我们操作
获取到链接状态
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    switch (central.state) {
        case CBManagerStateUnknown:
            NSLog(@">>>CBCentralManagerStateUnknown");
            break;
        case CBManagerStateResetting:
            NSLog(@">>>CBCentralManagerStateResetting");
            break;
        case CBManagerStateUnsupported:
            NSLog(@">>>CBCentralManagerStateUnsupported");
            break;
        case CBManagerStateUnauthorized:
            NSLog(@">>>CBCentralManagerStateUnauthorized");
            break;
        case CBManagerStatePoweredOff:
            //如果是关闭,系统会提示打开,我们不用管
            NSLog(@">>>CBCentralManagerStatePoweredOff");
            break;
        case CBManagerStatePoweredOn:
            //打开的
            NSLog(@">>>CBCentralManagerStatePoweredOn");
            break;
        default:
            break;
    }
}
2. 在代理中获得,扫描设备

扫描service,遵循代理,走代理方法

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
//这里就加入数组,继续扫描,刷新表
    peripheral.delegate = self;
    [peripheral discoverServices:nil];
}
3. 扫描到服务,并扫描服务的特征值
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    if (error){
        NSLog(@"扫描外设服务出错:%@-> %@", peripheral.name, [error localizedDescription]);
        return;
    }
    NSLog(@"扫描到外设服务:%@ -> %@",peripheral.name,peripheral.services);
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:service];
        //添加到数组
        //[self.serviceArray addObject:service];
    }
    NSLog(@"开始扫描外设服务的特征 %@...",peripheral.name);

}
4. 扫描到服务的特征值
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
//扫描到service会走这个方法.然后我们监听并接收特征的改变
   for (CBCharacteristic *characteristic in service.characteristics){
        [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
5. 写属性

            [perpher writeValue:theData forCharacteristic:character type:CBCharacteristicWriteWithoutResponse];

4. 上demo

github demo 下载地址,有问题请评论或者在git上提交问题

  • demo实现了扫描周围的蓝牙设备,并链接
    可以查看的一些特征值
    小米手环的一些特征值做了识别和处理

扫描界面

特征值1

特征值2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值