中央设备的实现大体分为以下步骤:
1,创建中央管理类,CentralManager
2,扫描并发现外设。
3,连接外设。
4,扫描连接上外设的所有服务。
5,扫描所有搜索到的服务的特征。
6,读或写或订阅特征。
具体实现:
1,同外设相同,先引入CoreBluetooth。之后实现两个协议,分别是CBCentralManagerDelegate,CBPeripheralDelegate。在.h中声明一个CBCentralManager,名为centralManager。在声明一个可变数组,用来存取所有连接的外设。
#import <CoreBluetooth/CoreBluetooth.h> @property (strong, nonatomic) CBCentralManager *centralManager; @property (strong, nonatomic) NSMutableArray *peripherals;
2,在.m中初始化.h中声明过的centralManager.
centralManager = [[CBCentralManager alloc] initWithDelegate:self queue: nil options:nil];
3,实现代理方法centralManagerDidUpdateState,和外围设备的实现一样,检测设备是否支持是否打开了蓝牙。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ switch (central.state) { case CBPeripheralManagerStatePoweredOn: [central scanForPeripheralsWithServices:nil options:nil]; break; default: NSLog(@"不支持或未打开蓝牙功能."); break; } }
上面的scanForPeripheralsWithServices:nil是用来执行扫描外设的方法。
4,因为上面执行了scanForPeripheralsWithServices:nil,所以接下来应该实现扫描到外设后的方法。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { if ([peripheral.name isEqualToString:kPeripheralName]) { if(![self.peripherals containsObject:peripheral]){ [self.peripherals addObject:peripheral]; } [self.centralManager connectPeripheral:peripheral options:nil]; } }
如果数组中已经有了搜索到的设备,就直接connect。如果没有,就将该外设加入到数组中,然后再connect。connectPeripheral方法是用来连接搜索到的外设。
5,连接到外部设备
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { [self.centralManager stopScan]; peripheral.delegate = self; [peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; }
因为连接了外设,所以使用stopScan方法停止扫描。之后执行discoverService来扫描服务。
6,扫描到外设的服务后,会调用didDiscoverServices。所以实现didDiscoverServices方法。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID]; CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID]; for (CBService *service in peripheral.services) { if([service.UUID isEqual:serviceUUID]){ [peripheral discoverCharacteristics:nil forService:service]; } } }
搜索到了服务后,执行discoverCharacteristics方法来搜索所有的特征。
7,扫描到了服务的特征之后,为特征实现读或写或订阅。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID]; CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID]; if ([service.UUID isEqual:serviceUUID]) { for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:characteristicUUID]) { [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } } } }
以上是搜索服务中的所有特征,如果某个特征符合我要寻找的特征,就对该特征执行一些操作。
上面代码用的是订阅,还可以读,写,这取决于该特征在外设中定义时定义的类型。
readValueForCharacteristic //读 setNotifyValue //订阅 writeValue //写
相关推荐
学习IOS基于corebluetooth.framework框架的蓝牙开发的简单例子
核心蓝牙演示 适用于iOS CoreBluetooth的简单演示
CoreBluetoothPeripheral, 在Mac和iOS上,使用CoreBluetooth演示 CoreBluetooth外设示例作者:Tse ( @liquidx )更新:4 2012年11月在OSX和iOS上使用 CoreBluetooth ( 使用 Bluetoothe 4.0 LE )的演示。在OSX端,Core
BLE 说明档,开发蓝牙4.0 时使用,必备资料
使用CoreBluetooth进行蓝牙搜索、连接、发送指令,后构建统一WiFi环境,并以此连接socket(借助GCDAsyncSocket),实现数据通信 代码只选取了目前程序中实现功能点的地方,示例图实在不知道改放啥
使用CoreBluetooth进行蓝牙搜索、连接、发送指令,然后构建统一WiFi环境,以此连接socket(借助GCDAsyncSocket),实现数据通信
IOS应用源码之CoreBlueTooth蓝牙通信.zip
iOS蓝牙4.0 CoreBluetooth 通讯(服务端+客户端) 希望给大家学习带来帮助。做的不是很好 还请大家多多点评
CoreBluetooth框架的基本使用方法和属性及常见使用协议
程序通过真机运行起来后,可以将所有正在进行广播的蓝牙设备显示出来。帮助刚开始接触iOS里CoreBluetooth来进行蓝牙开发的新手更好的学习,通过代码更快更好的进入蓝牙开发领域。
A simple iOS iPhone application that demonstrates how to use the CoreBluetooth Framework to connect to a Bluetooth LE peripheral and read, write and be notified of changes to the characteristics of ...
用CoreBluetooth实现的BLE周边和中央,资源包含两个工程,一个是周边工程,一个是中央工程,你需要用两个iOS设备同时Run两个工程,才会看到完整的BLE通讯效果。详细介绍:...
第8章我们介绍了定位技术,介绍了iOS设备如何实现定位应用开发,其中包括:定位服务编程、地理信息编码和地理信息反编码。此外,还介绍了苹果的微定位技术iBeacon,以及接近传感器。 第9章是项目实战,通过增强...
核心蓝牙示例 一些示例CoreBluetooth外围设备和中央应用程序。 依存关系 执照 版权所有(c)2016 Michael Reneer。 有关详细信息,请参见许可。
对CoreBluetooth进行简单的封装,从而不必各位在去阅读大量的CoreBluetooth的文档。 BluetoothCentral 实现三个回调。void(^successSearchAll)(void)成功搜索到周边类、 void(^successConnectBlock)(void)、成功连接...
使用iOS 蓝牙4.0技术,可以实现蓝牙设备扫描,连接,显示。传递数据,demo仅供参考
将iOS蓝牙CoreBluetooth进行一次简易的封装,实现只要改变蓝牙设备UUID,就能完成蓝牙外设读取等功能.
SwiftBLE外围设备使用CoreBluetooth,Swift 2.1,iOS Device创建外围BLE设备的示例代码。 从Obj-C转换为
LGBluetooth - 简单、基于块的轻量级CoreBluetooth库