iOS 蓝牙连接的流程

iOS 蓝牙连接的流程:

一、在 .h 文件中

1、加入头文件 #import <CoreBluetooth/CoreBluetooth.h>

2、声明以下变量

@property (nonatomic, strong)   CBCentralManager *m_manger; //管理者
@property (nonatomic, strong)   CBService        *m_service; //服务
@property (nonatomic, strong)   CBPeripheral     *m_peripheral;  //外设


二、在 .m 文件中

1、初始化管理者,扫描外设,连接指定外设

_m_manger = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[_m_manger scanForPeripheralsWithServices:nil options:nil];
[self.m_manger connectPeripheral:peripheral options:description];

2、判断系统蓝牙是否开启

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

3、实现发现外设的回调

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI

4、实现已连接外设的回调

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    _m_peripheral = peripheral;
    _m_peripheral.delegate = self;
    [_m_peripheral discoverServices:nil];
}

5、实现发现服务的回调

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:service];
    }
}


6、实现发现特征值的回调,并监听特征值

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    if ([service.UUID isEqual:[UUIDTool serviceUUID01]]) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:[UUIDTool characteristic01UUID]]) {
                
            }
	    // 监听特征值
            [_m_peripheral setNotifyValue:YES forCharacteristic:characteristic];
        }
    }
}

7、实现特征值改变的回调

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

8、实现发送指令反馈信息的回调

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    if (!error) {
        NSLog(@"发送指令成功");
    }
    else{
        NSLog(@"发送指令失败");
    }
}

9、实现外设连接失败的回调

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

10、实现外设连接后,再失去连接的回调

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

iOS平台上实现蓝牙连接与数据传输,主要依赖于苹果提供的Core Bluetooth框架。该框架支持开发者与蓝牙低功耗(BLE)设备进行交互,涵盖从扫描、连接、服务发现到数据读写等全过程。以下内容将围绕蓝牙连接相关的参数配置和开发指导展开。 ### 蓝牙状态管理 在开始蓝牙操作之前,必须对蓝牙状态进行监听和管理。`CBCentralManager`是Core Bluetooth框架中的核心类之一,用于管理蓝牙适配器的状态和扫描外设设备。通过实现`centralManagerDidUpdateState:`方法,可以监听蓝牙状态的变化,并据此执行相应的操作: ```objective-c - (void)centralManagerDidUpdateState:(CBCentralManager *)central { switch (central.state) { case CBManagerStateUnknown: NSLog(@">>>未知"); break; case CBManagerStateResetting: NSLog(@">>>重置"); break; case CBManagerStateUnsupported: NSLog(@">>>设备不支持"); break; case CBManagerStateUnauthorized: NSLog(@">>>设备未授权"); break; case CBManagerStatePoweredOff: NSLog(@">>>设备关闭"); break; case CBManagerStatePoweredOn: NSLog(@">>>设备打开"); [self.centralManager scanForPeripheralsWithServices:nil options:nil]; break; default: break; } } ``` ### 扫描外设设备 当蓝牙状态为`CBManagerStatePoweredOn`时,可以调用`scanForPeripheralsWithServices:options:`方法开始扫描外设设备。如果希望扫描所有可见设备,可以将第一个参数设为`nil`,表示不指定服务UUID;第二个参数为扫描选项,例如是否允许重复扫描已发现设备。 ### 连接外设 扫描到目标设备后,可以通过调用`connectPeripheral:options:`方法与设备建立连接连接成功后,系统会回调`centralManager:didConnectPeripheral:`方法,此时可以开始发现服务和特性。 ```objective-c [self.centralManager connectPeripheral:peripheral options:nil]; ``` ### 发现服务与特性 一旦连接建立,需要调用`discoverServices:`方法来获取外设提供的服务。服务发现完成后,系统会回调`peripheral:didDiscoverServices:`方法。接着,可以进一步调用`discoverCharacteristics:forService:`方法来发现特定服务下的特性。 ### 数据读写与通知订阅 对于支持读取的特性,可以通过`readValueForCharacteristic:`方法获取其值。若特性支持通知或指示,可调用`setNotifyValue:forCharacteristic:`方法订阅通知,以便在特性值发生变化时接收更新。 ### 断开连接 完成数据交互后,可以通过调用`cancelPeripheralConnection:`方法断开与外设的连接,释放资源。 ```objective-c [self.centralManager cancelPeripheralConnection:peripheral]; ``` ### 权限配置 为了确保应用能够正常使用蓝牙功能,需在项目的`Info.plist`文件中添加必要的权限声明。对于iOS 13及更高版本,还需请求用户授权访问蓝牙硬件: ```xml <key>NSBluetoothAlwaysUsageDescription</key> <string>需要您的同意以访问蓝牙设备</string> ``` 此外,应用还需要在`Info.plist`中声明后台模式支持,以便在应用进入后台时仍能维持蓝牙连接: ```xml <key>UIBackgroundModes</key> <array> <string>bluetooth-central</string> </array> ``` 通过上述步骤,开发者可以有效地配置iOS蓝牙连接参数并进行开发。这些步骤不仅涵盖了蓝牙状态的管理、设备扫描与连接、服务与特性的发现,还包括了数据的读写、通知订阅以及断开连接的操作,构成了一个完整的蓝牙通信流程
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值