LEONBLManager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // LEBLManager.h
  3. // Unity-iPhone
  4. //
  5. // Created by Leon on 2021/4/23.
  6. //
  7. #import <Foundation/Foundation.h>
  8. #import <CoreBluetooth/CoreBluetooth.h>
  9. //设备类型
  10. typedef NS_ENUM(NSInteger, DEVICE_TYPE){
  11. DEVICETYPE_MAIN =0, //主设备
  12. DEVICETYPE_VICE, //副设备
  13. };
  14. NS_ASSUME_NONNULL_BEGIN
  15. #pragma mark ------------------- block的定义 --------------------------
  16. /** 蓝牙状态改变的block */
  17. typedef void(^LEStateUpdateBlock)(CBCentralManager *central);
  18. /** 发现一个蓝牙外设的block */
  19. typedef void(^LEDiscoverPeripheralBlock)(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI);
  20. ///** 连接完成的block,失败error就不为nil */
  21. //typedef void(^LEConnectCompletionBlock)(CBPeripheral *peripheral, NSError *error);
  22. /** 蓝牙连接成功的回调*/
  23. typedef void(^LEBluedConnectSuccessfulBlock)(CBPeripheral *peripheral);
  24. /** 蓝牙链接失败的回调 */
  25. typedef void(^LEBluedConnectFailureBlock)(NSError *error);
  26. /** 蓝牙链接已经断开的回调 */
  27. typedef void(^LEBluedIsDisConnectBlock)(CBPeripheral *peripheral,NSError *error);
  28. /** 搜索到服务block */
  29. typedef void(^LEDiscoveredServicesBlock)(CBPeripheral *peripheral, NSArray *services, NSError *error);
  30. /** 搜索到某个服务中的特性的block */
  31. typedef void(^LEDiscoverCharacteristicsBlock)(CBPeripheral *peripheral, CBService *service, NSArray *characteristics, NSError *error);
  32. /** 收到摸个特性中数据的回调 */
  33. typedef void(^LEReadValueForCharacteristicBlock)(CBPeripheral *peripheral,CBCharacteristic *characteristic, NSData *value, NSError *error,DEVICE_TYPE type);
  34. /** 往特性中写入数据的回调 */
  35. typedef void(^LEWriteToCharacteristicBlock)(CBPeripheral *peripheral,CBCharacteristic *characteristic, NSError *error,DEVICE_TYPE type);
  36. @interface LEONBLManager : NSObject
  37. //蓝牙外设
  38. @property(nonatomic,copy)LEStateUpdateBlock stateUpdateBlock;
  39. @property(nonatomic,copy)LEDiscoverPeripheralBlock discoverPeripheralBlock;
  40. @property(nonatomic,copy)LEBluedConnectSuccessfulBlock successfulBlock;
  41. @property(nonatomic,copy)LEBluedConnectFailureBlock connectFailureBlock;
  42. @property(nonatomic,copy)LEBluedIsDisConnectBlock disConnectBlock;
  43. //蓝牙数据
  44. @property(nonatomic,copy)LEDiscoveredServicesBlock discoveredServicesBlock;//
  45. @property(nonatomic,copy)LEDiscoverCharacteristicsBlock discoverCharacteristicsBlock;
  46. @property(nonatomic,copy)LEReadValueForCharacteristicBlock readValueForCharacteristicBlock;
  47. @property(nonatomic,copy)LEWriteToCharacteristicBlock writeToCharacteristicBlock;//
  48. @property(nonatomic,strong)CBPeripheral * peripheral;//主设备
  49. @property(nonatomic,strong)CBPeripheral * vicePeripheral;//副设备
  50. //主设备 写入数据的特征 读取数据的特征
  51. @property(nonatomic,strong)CBCharacteristic * write;
  52. @property(nonatomic,strong)CBCharacteristic * read;
  53. //副设备 写入数据的特征 读取数据的特征
  54. @property(nonatomic,strong)CBCharacteristic * viceWrite;
  55. @property(nonatomic,strong)CBCharacteristic * viceRead;
  56. /**
  57. 声明单例类
  58. */
  59. +(instancetype)sharedInstance;
  60. #pragma mark -------------- public methon ==>> 外部 操作CBCentralManager
  61. /**
  62. 初始外设管理类
  63. */
  64. -(void)initCBCentralManager;
  65. /**
  66. 开始扫描蓝牙外设
  67. @param serviceUUIDs 一个CBUUID对象表示要扫描的服务。
  68. @param options 一个可选的字典,指定扫描选项
  69. */
  70. - (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
  71. /**
  72. 停止扫描蓝牙外设
  73. */
  74. - (void)stopScan;
  75. /**
  76. 外部主动断开蓝牙链接
  77. @param peripheral 待链接的CBPeripheral对象
  78. @param options 一个可选的字典,指定连接行为选项
  79. */
  80. - (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
  81. /**
  82. 外部主动断开蓝牙链接
  83. @param peripheral 待断开的CBPeripheral对象
  84. */
  85. - (void)cancelPeripheralConnection;
  86. #pragma mark -------------- public methon ==>> 外部 操作 CBPeripheral
  87. /**
  88. 主动向蓝牙写入数据
  89. @param data 数据流
  90. @param characteristic 可以写入的特征
  91. @param type 写入数据的类型
  92. */
  93. - (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic writeType:(CBCharacteristicWriteType)writeType deviceType:(DEVICE_TYPE)deviceType;
  94. @end
  95. NS_ASSUME_NONNULL_END