IOS BLE蓝牙4.0

时间:2023-03-09 08:17:41
IOS BLE蓝牙4.0

前言:

自己做的项目里面有这么一个功能,总结归纳一下。

先导入必要的框架  CoreBluetooth.framework

在要用到蓝牙的文件里面导入以下头文件

#import <CoreBluetooth/CoreBluetooth.h>
#import<CoreBluetooth/CBService.h>

接着需要实现两个协议,CBCentralManagerDelegate ,CBPeripheralDelegate

IOS BLE蓝牙4.0

1.先实例化

CBCentralManager

[self cMgr];
- (CBCentralManager *)cMgr
{
if (!_cMgr) {
_cMgr = [[CBCentralManager alloc] initWithDelegate:self
queue:dispatch_get_main_queue() options:nil];
}
return _cMgr;
}

判断一下设备的蓝牙是否打开

 if (self.cMgr.state == CBCentralManagerStatePoweredOn) {}

开始扫描蓝牙设备

[self.cMgr scanForPeripheralsWithServices:nil // 通过某些服务筛选外设
options:nil]; // dict,条件

-   (void)centralManagerDidUpdateState:(CBCentralManager *)central

这个方法主要是来检查IOS设备的蓝牙硬件的状态的,比如说你的设备不支持蓝牙4.0,

如果周围有正在广播的外设,且被扫描到了的话会触发

- (void)centralManager:(CBCentralManager *)central // 中心管理者

 didDiscoverPeripheral:(CBPeripheral *)peripheral // 外设

     advertisementData:(NSDictionary *)advertisementData // 外设携带的数据

                  RSSI:(NSNumber *)RSSI

在这个方法里面,可以对扫描到的设备进行筛选出自己想要的目标外设,进行连接

[self.cMgr connectPeripheral:self.peripheral options:nil];

连接成功/失败/丢失连接的话会触发相应的

- (void)centralManager:(CBCentralManager *)central // 中心管理者

  didConnectPeripheral:(CBPeripheral *)peripheral

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

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

连接已经成功,我们就该考虑是不是要停止中心管理设备的扫描动作了,要不然在你和已经连接好的外设进行数据沟通时,如果又有一个外设进行广播且符合你的连接条件,那么你的IOS设备就会也去连接这个设备(因为IOS BLE4.0是支持一对多连接的),导致数据的混乱。所以我们在这个方法里面停止扫描动作:

[self.cMgr stopScan];

接下来是让我们的第二个实例对像出场了  CBPeripheral

连接成功后 调

[self.peripheral discoverServices:nil];

可以进一步了解到Serverce里面的Characteristic

之后会回调

// 发现外设服务里的特征的时候调用的代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

接着我们可以在这个方法里面继续了解到某个CBService 里面的特征值UUID:

就会回调下面的方法:

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

接下来就是一些对某个特征值的读写操作了,当然我们首先要找到我们需要写数据的特征值UUID了,这里我们可以简单地通过循环来找出外设含有的某个特征值UUID:

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if (error) {
NSLog(@"%s, line = %d, error = %@", __FUNCTION__, __LINE__, error.localizedDescription);
NSLog(@"%@",error.localizedDescription);
return;
}
for (CBCharacteristic *cha in service.characteristics) {
if ([[cha UUID] isEqual:[CBUUID UUIDWithString:@"FFE4"]])
{
[peripheral setNotifyValue:YES forCharacteristic:cha];
}
else if ([[cha UUID] isEqual:[CBUUID UUIDWithString:@"FFE9"]])
{ self.sendCharacteristic = cha;
[self dealWithCmd];
} }
}

// 更新特征的value的时候会调用

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

// 更新特征的value的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
//接收到数据的话,解析数据,根据sign判断
if ([[characteristic UUID] isEqual:[CBUUID UUIDWithString:@"FFE4"]]) {
NSLog(@"更新特征%@",characteristic.value);

}
// 更新特征的描述的值的时候会调用
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__); [peripheral readValueForDescriptor:descriptor];
} // 发现外设的特征的描述数组
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__); // 在此处读取描述即可
for (CBDescriptor *descriptor in characteristic.descriptors) {
// 它会触发
[peripheral readValueForDescriptor:descriptor];
}
}