美文网首页
iOS蓝牙框架

iOS蓝牙框架

作者: iOS_tree | 来源:发表于2024-02-24 18:54 被阅读0次

1、概念介绍

本文所说的蓝牙开发是基于苹果<CoreBluetooth/CoreBluetooth.h>框架的蓝牙功能开发。
基本概念:
中心设备:用于扫描周边蓝牙外设的设备,在iOS开发中,手机就是我们的中心设备。
外部设备:被扫描的蓝牙设备,比如电子手表、蓝牙耳机或其他蓝牙设备。
广播:外部设备不断发送蓝牙蓝牙信号,供中心设备扫描到。
服务(CBService):设备之间进行数据交换的基础单位,设备连接后可以进行,读取服务和写入服务、更新服务。
特征(CBCharacteristic):服务下的子单位,数据在服务上的具体载体。一个服务会有多个特征,每个特征会有value来承载数据。
UUID:区分不同服务和特征的唯一标识符。
当我们进行蓝牙开发时,一般是在iPhone上,以中心设备的模式进行开发。步骤为:
1、检测蓝牙状态
2、扫描周边设备蓝牙
3、连接蓝牙
4、读取服务及特征数据,进行数据交互
外设设备开发:
1、检测蓝牙状态
2、创建蓝牙对象设置代理,等待被连接
3、写入特征和读取特征,与中心设备进行数据交互
我们可以使用苹果电脑作为外设,苹果手机作为中心设备,模拟两个设备的蓝牙交互流程。
为了更好的理解蓝牙功能开发,我们确定一个练习的目标:在苹果电脑上使用蓝牙传输一张图片数据到手机上,然后在手机上进行显示。
由于图片比较大,在传输过程中我们需要对数据进行分割传输。

2、模拟外部设备开发

1、首先创建工程,导入框架

#import <CoreBluetooth/CoreBluetooth.h>

2、创建外设CBPeripheralManager对象

@property(nonatomic,strong)CBPeripheralManager* peripheralManager;
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(0, 0)];

设置ViewController为代理,设置回调队列为主队列。
3、实现代理方法
当检测到蓝牙状态发生改变时,系统会调用状态改变的代理方法,通知我们蓝牙是否可用。

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    switch (peripheral.state) {
        case CBPeripheralManagerStatePoweredOff:
            NSLog(@"蓝牙关闭");
            break;
        case CBPeripheralManagerStatePoweredOn:
            NSLog(@"蓝牙打开");
            //广播数据
            [self startAdvertising];
            break;
        default:
            NSLog(@"蓝牙状态不明");
            break;
    }
}

我们在蓝牙打开的回调里面添加广播的数据,告诉中心设备,我们的特征。

- (void)startAdvertising {
    CBUUID* peripheralServiceUUID = [CBUUID UUIDWithString:@"AD01"];
    NSDictionary *para = @{
        CBAdvertisementDataLocalNameKey : @"macblue",
        CBAdvertisementDataServiceUUIDsKey : @[peripheralServiceUUID],
    };
//    开始广播
    [self.peripheralManager startAdvertising:para];
}

startAdvertising方法中的字典只能添加两个key,分别为CBAdvertisementDataLocalNameKey,CBAdvertisementDataServiceUUIDsKey。我们最多可用传递28个字节数据。
当中心设备扫描时,会扫描到我们设置的广播数据。
如下:

{
    kCBAdvDataIsConnectable = 1;
    kCBAdvDataLocalName = macblue;
    kCBAdvDataRxPrimaryPHY = 0;
    kCBAdvDataRxSecondaryPHY = 0;
    kCBAdvDataServiceUUIDs =     (
        AD01
    );
    kCBAdvDataTimestamp = "727518148.203617";
    kCBAdvDataTxPowerLevel = 12;
}

中心设备以kCBAdvDataLocalName = macblue和kCBAdvDataServiceUUIDs = (AD01)来判断设备是否为我们需要连接的测试设备。
此时,我们的外部设备以及准备好,等待中心设备扫描连接。

3、中心设备开发

导入蓝牙框架

#import <CoreBluetooth/CoreBluetooth.h>

创建蓝牙对象

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(0, 0)];

实现代理方法

//蓝牙状态发生改变调用的代理方法
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

各种状态如下,当蓝牙状态为CBManagerStatePoweredOn时可对蓝牙进行操作。

typedef NS_ENUM(NSInteger, CBManagerState) {
    CBManagerStateUnknown = 0,
    CBManagerStateResetting,
    CBManagerStateUnsupported,
    CBManagerStateUnauthorized,
    CBManagerStatePoweredOff,
    CBManagerStatePoweredOn,
} NS_ENUM_AVAILABLE(10_13, 10_0);

开始扫描周围蓝牙设备

- (void)scanDeviceWithServicerUUID:(NSString *)servicerUUID {
    NSLog(@"开始扫描服务:%@",servicerUUID);
    if (servicerUUID) {
        CBUUID* servicesUUID = [CBUUID UUIDWithString:servicerUUID];
        [self.centralManager scanForPeripheralsWithServices:@[servicesUUID] options:nil];
    }else {
        [self.centralManager scanForPeripheralsWithServices:nil options:nil];
    }
}

扫描到目标蓝牙设备时,停止蓝牙设备的扫描

    [self.centralManager stopScan];

连接目标蓝牙设备

    [self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey:@1}];

蓝牙连接成功和失败都有代理方法回调

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral timestamp:(CFAbsoluteTime)timestamp isReconnecting:(BOOL)isReconnecting error:(nullable NSError *)error;
- (void)centralManager:(CBCentralManager *)central connectionEventDidOccur:(CBConnectionEvent)event forPeripheral:(CBPeripheral *)peripheral CB_CM_API_AVAILABLE;
- (void)centralManager:(CBCentralManager *)central didUpdateANCSAuthorizationForPeripheral:(CBPeripheral *)peripheral NS_AVAILABLE_IOS(13_0);

连接上目标蓝牙设备后,读取蓝牙服务数据和特征数据

- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;

读取到服务后会调用对应的代理方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;

从服务中读取特征值

- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;

找到特征后会出发找到特征的代理方法

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

可以根据CBCharacteristic对象的characteristics数组,处理不同的特征服务。
类型如下:

typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
    CBCharacteristicPropertyBroadcast                                               = 0x01,
    CBCharacteristicPropertyRead                                                    = 0x02,
    CBCharacteristicPropertyWriteWithoutResponse                                    = 0x04,
    CBCharacteristicPropertyWrite                                                   = 0x08,
    CBCharacteristicPropertyNotify                                                  = 0x10,
    CBCharacteristicPropertyIndicate                                                = 0x20,
    CBCharacteristicPropertyAuthenticatedSignedWrites                               = 0x40,
    CBCharacteristicPropertyExtendedProperties                                      = 0x80,
    CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0)   = 0x100,
    CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(10_9, 6_0) = 0x200
};

主要有CBCharacteristicPropertyRead、CBCharacteristicPropertyWrite、CBCharacteristicPropertyNotify。方便为读、写、通知。读主要是读取设备蓝牙数据,写是向设备蓝牙写入数据,通知是接收设备蓝牙的通知。
读取到特征值后会调用代理方法

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

写入特征值后也会调用代理方法告诉用户是否写入正确

 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;

订阅状态发生改变也会调用代理方法

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;

相关文章

  • iOS蓝牙开发(CoreBluetooth)

    目前在iOS中蓝牙开发框架主要有以下几种 GameKit.framework:iOS7之前的蓝牙通讯框架,从iOS...

  • iOS蓝牙总结

    ios中蓝牙的几种实现 ios中提供了4个框架用于实现蓝牙连接GameKit.framework只能用于ios设备...

  • iOS-蓝牙(CoreBlueTooth)

    iOS蓝牙须知 iOS提供了4个框架用于实现蓝牙连接 1.gameKit.framework(用法简单) 只能用于...

  • iOS蓝牙编程CoreBluetooth

    iOS的蓝牙框架是支持蓝牙4.0协议的。理解iOS CoreBluetooth两个很重要的概念,Central 和...

  • iOS蓝牙实现

    iOS 中使用 Core Bluetooth 框架实现蓝牙通信。Core Bluetooth 是基于蓝牙 4.0 ...

  • iOS 蓝牙开发 - 学习

    iOS 中使用 Core Bluetooth 框架实现蓝牙通信。Core Bluetooth 是基于蓝牙 4.0 ...

  • iOS 关于蓝牙通信的4种框架

    iOS中蓝牙的实现方案 iOS中提供了4个框架用于实现蓝牙连接 一、GameKit.framework(用法简单)...

  • iOS中的蓝牙

    iOS中的蓝牙 概述 iOS中提供了4个框架用于实现蓝牙连接 1.GameKit.framework(用法简单)只...

  • 重点 (三十一) : 蓝牙/传感器

    iOS中蓝牙的实现方案 iOS中提供了4个框架用于实现蓝牙连接 GameKit.framework(用法简单) 只...

  • 蓝牙开发

    蓝牙开发使用CoreBluetooth框架实现两个iOS设备、iOS设备与非iOS蓝牙设备的交互。要注意的一点是目...

网友评论

      本文标题:iOS蓝牙框架

      本文链接:https://www.haomeiwen.com/subject/cxwxodtx.html