BabyBluetooth.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. BabyBluetooth
  3. 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮
  4. https://github.com/coolnameismy/BabyBluetooth
  5. version:0.7.0
  6. */
  7. // Created by 刘彦玮 on 15/3/31.
  8. // Copyright (c) 2015年 刘彦玮. All rights reserved.
  9. #import <Foundation/Foundation.h>
  10. #import <CoreBluetooth/CoreBluetooth.h>
  11. #import "BabyCentralManager.h"
  12. #import "BabyPeripheralManager.h"
  13. #import "BabyToy.h"
  14. #import "BabySpeaker.h"
  15. #import "BabyRhythm.h"
  16. #import "BabyDefine.h"
  17. @interface BabyBluetooth : NSObject
  18. #pragma mark - babybluetooth的委托
  19. //默认频道的委托
  20. /**
  21. 设备状态改变的block | when CentralManager state changed
  22. */
  23. - (void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block;
  24. /**
  25. 找到Peripherals的block | when find peripheral
  26. */
  27. - (void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;
  28. /**
  29. 连接Peripherals成功的block
  30. | when connected peripheral
  31. */
  32. - (void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;
  33. /**
  34. 连接Peripherals失败的block
  35. | when fail to connect peripheral
  36. */
  37. - (void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
  38. /**
  39. 断开Peripherals的连接的block
  40. | when disconnected peripheral
  41. */
  42. - (void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
  43. /**
  44. 设置查找服务的block
  45. | when discover services of peripheral
  46. */
  47. - (void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block;
  48. /**
  49. 设置查找到Characteristics的block
  50. | when discovered Characteristics
  51. */
  52. - (void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;
  53. /**
  54. 设置获取到最新Characteristics值的block
  55. | when read new characteristics value or notiy a characteristics value
  56. */
  57. - (void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
  58. /**
  59. 设置查找到Descriptors名称的block
  60. | when discover descriptors for characteristic
  61. */
  62. - (void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
  63. /**
  64. 设置读取到Descriptors值的block
  65. | when read descriptors for characteristic
  66. */
  67. - (void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block;
  68. /**
  69. 写Characteristic成功后的block
  70. | when did write value for characteristic successed
  71. */
  72. - (void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
  73. /**
  74. 写descriptor成功后的block
  75. | when did write value for descriptor successed
  76. */
  77. - (void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block;
  78. /**
  79. characteristic订阅状态改变的block
  80. | when characteristic notification state changed
  81. */
  82. - (void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
  83. /**
  84. 读取RSSI的委托
  85. | when did read RSSI
  86. */
  87. - (void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block;
  88. /**
  89. discoverIncludedServices的回调,暂时在babybluetooth中无作用
  90. | no used in babybluetooth
  91. */
  92. - (void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block;
  93. /**
  94. 外设更新名字后的block
  95. | when peripheral update name
  96. */
  97. - (void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block;
  98. /**
  99. 外设更新服务后的block
  100. | when peripheral update services
  101. */
  102. - (void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
  103. // channel的委托
  104. /**
  105. 设备状态改变的block
  106. | when CentralManager state changed
  107. */
  108. - (void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel
  109. block:(void (^)(CBCentralManager *central))block;
  110. /**
  111. 找到Peripherals的block
  112. | when find peripheral
  113. */
  114. - (void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel
  115. block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block;
  116. /**
  117. 连接Peripherals成功的block
  118. | when connected peripheral
  119. */
  120. - (void)setBlockOnConnectedAtChannel:(NSString *)channel
  121. block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block;
  122. /**
  123. 连接Peripherals失败的block
  124. | when fail to connect peripheral
  125. */
  126. - (void)setBlockOnFailToConnectAtChannel:(NSString *)channel
  127. block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
  128. /**
  129. 断开Peripherals的连接的block
  130. | when disconnected peripheral
  131. */
  132. - (void)setBlockOnDisconnectAtChannel:(NSString *)channel
  133. block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block;
  134. /**
  135. 设置查找服务的block
  136. | when discover services of peripheral
  137. */
  138. - (void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel
  139. block:(void (^)(CBPeripheral *peripheral,NSError *error))block;
  140. /**
  141. 设置查找到Characteristics的block
  142. | when discovered Characteristics
  143. */
  144. - (void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel
  145. block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block;
  146. /**
  147. 设置获取到最新Characteristics值的block
  148. | when read new characteristics value or notiy a characteristics value
  149. */
  150. - (void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel
  151. block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block;
  152. /**
  153. 设置查找到Characteristics描述的block
  154. | when discover descriptors for characteristic
  155. */
  156. - (void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel
  157. block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block;
  158. /**
  159. 设置读取到Characteristics描述的值的block
  160. | when read descriptors for characteristic
  161. */
  162. - (void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel
  163. block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block;
  164. /**
  165. 写Characteristic成功后的block
  166. | when did write value for characteristic successed
  167. */
  168. - (void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel
  169. block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
  170. /**
  171. 写descriptor成功后的block
  172. | when did write value for descriptor successed
  173. */
  174. - (void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel
  175. block:(void (^)(CBDescriptor *descriptor,NSError *error))block;
  176. /**
  177. characteristic订阅状态改变的block
  178. | when characteristic notification state changed
  179. */
  180. - (void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel
  181. block:(void (^)(CBCharacteristic *characteristic,NSError *error))block;
  182. /**
  183. 读取RSSI的委托
  184. | when did read RSSI
  185. */
  186. - (void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel
  187. block:(void (^)(NSNumber *RSSI,NSError *error))block;
  188. /**
  189. discoverIncludedServices的回调,暂时在babybluetooth中无作用
  190. | no used in babybluetooth
  191. */
  192. - (void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel
  193. block:(void (^)(CBService *service,NSError *error))block;
  194. /**
  195. 外设更新名字后的block
  196. | when peripheral update name
  197. */
  198. - (void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel
  199. block:(void (^)(CBPeripheral *peripheral))block;
  200. /**
  201. 外设更新服务后的block
  202. | when peripheral update services
  203. */
  204. - (void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel
  205. block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block;
  206. #pragma mark - babybluetooth filter
  207. /**
  208. 设置查找Peripherals的规则
  209. | filter of discover peripherals
  210. */
  211. - (void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
  212. /**
  213. 设置连接Peripherals的规则
  214. | setting filter of connect to peripherals peripherals
  215. */
  216. - (void)setFilterOnConnectToPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
  217. /**
  218. 设置查找Peripherals的规则
  219. | filter of discover peripherals
  220. */
  221. - (void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel
  222. filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
  223. /**
  224. 设置连接Peripherals的规则
  225. | setting filter of connect to peripherals peripherals
  226. */
  227. - (void)setFilterOnConnectToPeripheralsAtChannel:(NSString *)channel
  228. filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter;
  229. #pragma mark - babybluetooth Special
  230. /**
  231. babyBluettooth cancelScan方法调用后的回调
  232. | when after call cancelScan
  233. */
  234. - (void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block;
  235. /**
  236. babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
  237. | when did all peripheral disConnect
  238. */
  239. - (void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block;
  240. /**
  241. babyBluettooth cancelScan方法调用后的回调
  242. | when after call cancelScan
  243. */
  244. - (void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel
  245. block:(void(^)(CBCentralManager *centralManager))block;
  246. /**
  247. babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调
  248. | when did all peripheral disConnect
  249. */
  250. - (void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel
  251. block:(void(^)(CBCentralManager *centralManager))block;
  252. /**
  253. 设置蓝牙运行时的参数
  254. | set ble runtime parameters
  255. */
  256. - (void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
  257. connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
  258. scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
  259. discoverWithServices:(NSArray *)discoverWithServices
  260. discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
  261. /**
  262. 设置蓝牙运行时的参数
  263. | set ble runtime parameters
  264. */
  265. - (void)setBabyOptionsAtChannel:(NSString *)channel
  266. scanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions
  267. connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions
  268. scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices
  269. discoverWithServices:(NSArray *)discoverWithServices
  270. discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics;
  271. #pragma mark - 链式函数
  272. /**
  273. 查找Peripherals
  274. */
  275. - (BabyBluetooth *(^)()) scanForPeripherals;
  276. /**
  277. 连接Peripherals
  278. */
  279. - (BabyBluetooth *(^)()) connectToPeripherals;
  280. /**
  281. 发现Services
  282. */
  283. - (BabyBluetooth *(^)()) discoverServices;
  284. /**
  285. 获取Characteristics
  286. */
  287. - (BabyBluetooth *(^)()) discoverCharacteristics;
  288. /**
  289. 更新Characteristics的值
  290. */
  291. - (BabyBluetooth *(^)()) readValueForCharacteristic;
  292. /**
  293. 获取Characteristics的名称
  294. */
  295. - (BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic;
  296. /**
  297. 获取Descriptors的值
  298. */
  299. - (BabyBluetooth *(^)()) readValueForDescriptors;
  300. /**
  301. 开始执行
  302. */
  303. - (BabyBluetooth *(^)()) begin;
  304. /**
  305. sec秒后停止
  306. */
  307. - (BabyBluetooth *(^)(int sec)) stop;
  308. /**
  309. 持有对象
  310. */
  311. - (BabyBluetooth *(^)(id obj)) having;
  312. /**
  313. 切换委托的频道
  314. */
  315. - (BabyBluetooth *(^)(NSString *channel)) channel;
  316. /**
  317. 谓词,返回self
  318. */
  319. - (BabyBluetooth *) and;
  320. /**
  321. 谓词,返回self
  322. */
  323. - (BabyBluetooth *) then;
  324. /**
  325. 谓词,返回self
  326. */
  327. - (BabyBluetooth *) with;
  328. /**
  329. * enjoy 祝你使用愉快,
  330. *
  331. * 说明:enjoy是蓝牙全套串行方法的简写(发现服务,发现特征,读取特征,读取特征描述),前面可以必须有scanForPeripherals或having方法,channel可以选择添加。
  332. enjoy() 等同于 connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
  333. 它可以让你少敲很多代码
  334. ## 例子:
  335. - 扫描后来个全套(发现服务,发现特征,读取特征,读取特征描述)
  336. ` baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics()
  337. .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
  338. `
  339. - 直接使用已有的外设连接后全套(发现服务,发现特征,读取特征,读取特征描述)
  340. ` baby.having(self.peripheral).connectToPeripherals().discoverServices().discoverCharacteristics()
  341. .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin();
  342. `
  343. enjoy后面也可以加stop()方法
  344. */
  345. - (BabyBluetooth *(^)()) enjoy;
  346. #pragma mark - 工具方法
  347. /**
  348. * 单例构造方法
  349. * @return BabyBluetooth共享实例
  350. */
  351. + (instancetype)shareBabyBluetooth;
  352. /**
  353. 断开连接
  354. */
  355. - (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
  356. /**
  357. 断开所有连接
  358. */
  359. - (void)cancelAllPeripheralsConnection;
  360. /**
  361. 停止扫描
  362. */
  363. - (void)cancelScan;
  364. /**
  365. 更新Characteristics的值
  366. */
  367. - (BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails;
  368. /**
  369. 设置characteristic的notify
  370. */
  371. - (void)notify:(CBPeripheral *)peripheral
  372. characteristic:(CBCharacteristic *)characteristic
  373. block:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block;
  374. /**
  375. 取消characteristic的notify
  376. */
  377. - (void)cancelNotify:(CBPeripheral *)peripheral
  378. characteristic:(CBCharacteristic *)characteristic;
  379. /**
  380. 获取当前连接的peripherals
  381. */
  382. - (NSArray *)findConnectedPeripherals;
  383. /**
  384. 获取当前连接的peripheral
  385. */
  386. - (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName;
  387. /**
  388. 获取当前corebluetooth的centralManager对象
  389. */
  390. - (CBCentralManager *)centralManager;
  391. /**
  392. 添加断开自动重连的外设
  393. */
  394. - (void)AutoReconnect:(CBPeripheral *)peripheral;
  395. /**
  396. 删除断开自动重连的外设
  397. */
  398. - (void)AutoReconnectCancel:(CBPeripheral *)peripheral;
  399. /**
  400. 根据外设UUID对应的string获取已配对的外设
  401. 通过方法获取外设后可以直接连接外设,跳过扫描过程
  402. */
  403. - (CBPeripheral *)retrievePeripheralWithUUIDString:(NSString *)UUIDString;
  404. #pragma mark - peripheral model
  405. //进入外设模式
  406. - (BabyPeripheralManager *(^)()) bePeripheral;
  407. - (BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName;
  408. @property (nonatomic, readonly) CBPeripheralManager *peripheralManager;
  409. //peripheral model block
  410. /**
  411. PeripheralManager did update state block
  412. */
  413. - (void)peripheralModelBlockOnPeripheralManagerDidUpdateState:(void(^)(CBPeripheralManager *peripheral))block;
  414. /**
  415. PeripheralManager did add service block
  416. */
  417. - (void)peripheralModelBlockOnDidAddService:(void(^)(CBPeripheralManager *peripheral,CBService *service,NSError *error))block;
  418. /**
  419. PeripheralManager did start advertising block
  420. */
  421. - (void)peripheralModelBlockOnDidStartAdvertising:(void(^)(CBPeripheralManager *peripheral,NSError *error))block;
  422. /**
  423. peripheralManager did receive read request block
  424. */
  425. - (void)peripheralModelBlockOnDidReceiveReadRequest:(void(^)(CBPeripheralManager *peripheral,CBATTRequest *request))block;
  426. /**
  427. peripheralManager did receive write request block
  428. */
  429. - (void)peripheralModelBlockOnDidReceiveWriteRequests:(void(^)(CBPeripheralManager *peripheral,NSArray *requests))block;
  430. /**
  431. peripheralManager did subscribe to characteristic block
  432. */
  433. - (void)peripheralModelBlockOnDidSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;
  434. /**
  435. peripheralManager did subscribe to characteristic block
  436. */
  437. - (void)peripheralModelBlockOnDidUnSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block;
  438. @end