CoreBlueTooth的使用要求设备必须支持蓝牙4.0,并且手机型号必须是iPhone4以上。
CoreBlueTooth关键在于两个名词,周边和中央,每个IOS设备都可以是一个周边,也可以是一个中央,但不能既是周边又是中央。
周边可以广播数据,可以告诉周围其他设备来接受数据,中央就是接收数据的那个设备。
如果一个ios手机,它既能当作外围设备,又能当作中央设备,那么在代码中就必须实现外围设备和中央设备这两种代码。
外围设备的实现大体分为以下步骤:
1,创建周边管理类,Peripheral Manager
2,为这个Peripheral Manager添加它自己的信息,比如各种服务,以及服务里面包括的各种特征。
3,广播服务,告知其他设备该外围的存在。
4,中央设备连接之后,与中央设备进行交互。
具体实现:
1,在一个类中引入CoreBluetooth,并实现CBPeripheralManagerDelegate协议,在.h文件中声明CBPeripheralManager ,以及需要添加的服务和特征
#import <CoreBluetooth/CoreBluetooth.h> @property (nonatomic, strong) CBPeripheralManager *manager; @property (nonatomic, strong) CBMutableService*cutomService; @property (nonatomic, strong) CBMutableCharacteristicclass*customCharacteristic;
2,在viewDidLoad中初始化manager。
self.manager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
3,实现peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { switch (peripheral.state) { case CBPeripheralManagerStatePoweredOn: [self setupService]; break; default: NSLog(@"Peripheral Manager did change state"); break; } }
这个代理方法在设备打开蓝牙或关闭蓝牙时运行,如果设备不支持蓝牙,做一些提醒。setupService是自己实现的添加服务的方法。
4,添加服务以及服务的特征。实现上面的setupService方法。
- (void)setupService { CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharaUUID]; self.customCharacteristic = [[CBMutableCharacteristic alloc] initWithType: characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable]; CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID]; self.customService = [[CBMutableService alloc] initWithType:serviceUUID primary:YES]; [self.customService setCharacteristics:@[self.customCharacteristic]]; [self.peripheralManager addService:self.customService]; }
CBCharacteristicPropertyNotify 表示该特征可以被其他设备订阅。
5,实现peripheralManager:didAddService:error:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error { if (error == nil) { // Starts advertising the service [self.peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey : @"ICServer", CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:kServiceUUID]] }]; } }
如果[self.peripheralManager addService:self.customService]执行了,也就是外围设备添加了服务,就会执行peripheralManager:didAddService:error: 然后执行startAdvertising开始广播。
6,收到中央设备的消息后运行的方法。
//订阅 -(void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didSubscribeToCharacteristic:(CBCharacteristic )characteristic; //取消订阅 -(void)peripheralManager:(CBPeripheralManager )peripheral central:(CBCentral )central didUnsubscribeFromCharacteristic:(CBCharacteristic )characteristic; //读消息 -(void)peripheralManager:(CBPeripheralManager )peripheral didReceiveReadRequest:(CBATTRequest )request; //写消息 -(void)peripheralManager:(CBPeripheralManager )peripheral didReceiveWriteRequests:(NSArray<CBATTRequest > *)requests;
相关推荐
学习IOS基于corebluetooth.framework框架的蓝牙开发的简单例子
核心蓝牙演示 适用于iOS CoreBluetooth的简单演示
CoreBluetoothPeripheral, 在Mac和iOS上,使用CoreBluetooth演示 CoreBluetooth外设示例作者:Tse ( @liquidx )更新:4 2012年11月在OSX和iOS上使用 CoreBluetooth ( 使用 Bluetoothe 4.0 LE )的演示。在OSX端,Core
核心蓝牙示例 一些示例CoreBluetooth外围设备和中央应用程序。 依存关系 执照 版权所有(c)2016 Michael Reneer。 有关详细信息,请参见许可。
BLE 说明档,开发蓝牙4.0 时使用,必备资料
使用CoreBluetooth进行蓝牙搜索、连接、发送指令,后构建统一WiFi环境,并以此连接socket(借助GCDAsyncSocket),实现数据通信 代码只选取了目前程序中实现功能点的地方,示例图实在不知道改放啥
使用CoreBluetooth进行蓝牙搜索、连接、发送指令,然后构建统一WiFi环境,以此连接socket(借助GCDAsyncSocket),实现数据通信
IOS应用源码之CoreBlueTooth蓝牙通信.zip
iOS蓝牙4.0 CoreBluetooth 通讯(服务端+客户端) 希望给大家学习带来帮助。做的不是很好 还请大家多多点评
CoreBluetooth框架的基本使用方法和属性及常见使用协议
程序通过真机运行起来后,可以将所有正在进行广播的蓝牙设备显示出来。帮助刚开始接触iOS里CoreBluetooth来进行蓝牙开发的新手更好的学习,通过代码更快更好的进入蓝牙开发领域。
SwiftBLE外围设备使用CoreBluetooth,Swift 2.1,iOS Device创建外围BLE设备的示例代码。 从Obj-C转换为
osx-ble-peripheral, 在外围模式中,使用CoreBluetooth的示例 osx-ble-peripheral 在外围模式中使用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 ...
第8章我们介绍了定位技术,介绍了iOS设备如何实现定位应用开发,其中包括:定位服务编程、地理信息编码和地理信息反编码。此外,还介绍了苹果的微定位技术iBeacon,以及接近传感器。 第9章是项目实战,通过增强...
对CoreBluetooth进行简单的封装,从而不必各位在去阅读大量的CoreBluetooth的文档。 BluetoothCentral 实现三个回调。void(^successSearchAll)(void)成功搜索到周边类、 void(^successConnectBlock)(void)、成功连接...
使用iOS 蓝牙4.0技术,可以实现蓝牙设备扫描,连接,显示。传递数据,demo仅供参考
将iOS蓝牙CoreBluetooth进行一次简易的封装,实现只要改变蓝牙设备UUID,就能完成蓝牙外设读取等功能.
LGBluetooth - 简单、基于块的轻量级CoreBluetooth库