基础知识了解git
BLE中的开发要使用CoreBluetooth框架 CoreBluetooth框架的核心实际上是两个东西,peripheral和central, 能够理解成外设和中心。对应他们分别有一组相关的API和类蓝牙设备的几种状态:github
做为中心模式流程:api
创建中心角色bash
扫描外设(discover)框架
链接外设(connect)工具
扫描外设中的服务和特征(discover)ui
与外设作数据交互(explore and interact)spa
订阅Characteristic的通知代理
断开链接(disconnect)code
做为外设模式流程:
具体实施以下
[BLEManager.shareInstance setDelegate:self];
复制代码
#pragma mark- 开始搜索
- (void)beginScan{
[self.cbCM scanForPeripheralsWithServices:nil options:nil];
}
复制代码
#pragma mark- 链接设备
- (void)connect:(CBPeripheral *)peripheral{
[self.cbCM stopScan];
[self.cbCM connectPeripheral:peripheral options:nil];
}
复制代码
#pragma mark- 链接成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
connectPeripheral = peripheral;
peripheral.delegate = self;
[peripheral discoverServices:nil];
if ([self.delegate respondsToSelector:@selector(didConnectPeripheral:)]) {
[self.delegate didConnectPeripheral:peripheral];
}
}
#pragma mark- 链接失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
if ([self.delegate respondsToSelector:@selector(didFailToConnectPeripheral:error:)]) {
[self.delegate didFailToConnectPeripheral:peripheral error:error];
}
}
复制代码
//发现服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
for (CBService *s in peripheral.services) {
if ([s.UUID.UUIDString isEqualToString:self->serviceUUIDString]) {
//这里能够经过service的UUID属性来辨识你要的服务
[peripheral discoverCharacteristics:nil forService:s];
}
}
}
//设置特征属性值
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->notify]]){
// 订阅, 实时接收
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->write]]){
self.writeCharacteristic = characteristic;
}
}
}
复制代码
#pragma mark- 发送数据
- (void)write:(NSData *)data withResponse:(BOOL)withResponse{
if(!_writeCharacteristic){
NSLog(@"writeCharacteristic is nil!");
return;
}
[connectPeripheral writeValue:data forCharacteristic:self.writeCharacteristic type:withResponse ? CBCharacteristicWriteWithResponse : CBCharacteristicWriteWithoutResponse];
}
复制代码
#pragma mark- 蓝牙数据接收
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:self->serviceUUIDString]]) {
NSData *data = characteristic.value;
if (data) {
if ([self.delegate respondsToSelector:@selector(bleGattServiceDataReceived:)]) {
[self.delegate bleGattServiceDataReceived:data];
}
}
}
}
复制代码
#pragma mark- 蓝牙状态的改变
- (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central {
switch (central.state) {
case CBManagerStateUnknown:
//未知状态
break;
case CBManagerStateResetting:
//蓝牙重置中
break;
case CBManagerStateUnsupported:
//蓝牙不支持
break;
case CBManagerStateUnauthorized:
//没有权限
break;
case CBManagerStatePoweredOff:
//蓝牙为开启
break;
case CBManagerStatePoweredOn:
//蓝牙已开启
break;
default:
break;
}
}
复制代码