LEONBLManager.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(int, 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);
  34. /** 往特性中写入数据的回调 */
  35. typedef void(^LEWriteToCharacteristicBlock)(CBPeripheral *peripheral,CBCharacteristic *characteristic, NSError *error,DEVICE_TYPE type);
  36. /** RSSI*/
  37. typedef void(^RSSIBlock)(NSNumber *RSSI);
  38. @interface LEONBLManager : NSObject
  39. //蓝牙外设
  40. @property(nonatomic,copy)LEStateUpdateBlock stateUpdateBlock;
  41. @property(nonatomic,copy)LEDiscoverPeripheralBlock discoverPeripheralBlock;
  42. @property(nonatomic,copy)LEBluedConnectSuccessfulBlock successfulBlock;
  43. @property(nonatomic,copy)LEBluedConnectFailureBlock connectFailureBlock;
  44. @property(nonatomic,copy)LEBluedIsDisConnectBlock disConnectBlock;
  45. //蓝牙数据
  46. @property(nonatomic,copy)LEDiscoveredServicesBlock discoveredServicesBlock;//
  47. @property(nonatomic,copy)LEDiscoverCharacteristicsBlock discoverCharacteristicsBlock;
  48. @property(nonatomic,copy)LEReadValueForCharacteristicBlock readValueForCharacteristicBlock;
  49. @property(nonatomic,copy)LEWriteToCharacteristicBlock writeToCharacteristicBlock;//
  50. @property(nonatomic,copy)RSSIBlock rssiBlock;//
  51. @property (strong, nonatomic)CBCentralManager * centralManager;/**< 蓝牙中心管理器 */
  52. @property(nonatomic,strong)CBPeripheral * peripheral;//主设备
  53. @property(nonatomic,strong)CBPeripheral * vicePeripheral;//副设备
  54. //主设备 写入数据的特征 读取数据的特征
  55. @property(nonatomic,strong)CBCharacteristic * write;
  56. @property(nonatomic,strong)CBCharacteristic * read;
  57. //副设备 写入数据的特征 读取数据的特征
  58. @property(nonatomic,strong)CBCharacteristic * viceWrite;
  59. @property(nonatomic,strong)CBCharacteristic * viceRead;
  60. //防止同一条数据回调多次
  61. @property(assign,nonatomic)int main_read;
  62. @property(assign,nonatomic)int vice_read;
  63. /**测试参数start*/
  64. @property(assign,nonatomic)int totalPackages;
  65. @property(assign,nonatomic)int totalLoss;
  66. @property(assign,nonatomic)int tempTs;
  67. @property(nonatomic,assign) NSNumber *RSSI;//设备到手机的实时信号强度
  68. /**测试参数end*/
  69. /**
  70. 声明单例类
  71. */
  72. +(instancetype)sharedInstance;
  73. #pragma mark -------------- public methon ==>> 外部 操作CBCentralManager
  74. /**
  75. 初始外设管理类
  76. */
  77. -(void)initCBCentralManager;
  78. /**
  79. 开始扫描蓝牙外设
  80. @param serviceUUIDs 一个CBUUID对象表示要扫描的服务。
  81. @param options 一个可选的字典,指定扫描选项
  82. */
  83. - (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
  84. /**
  85. 停止扫描蓝牙外设
  86. */
  87. - (void)stopScan;
  88. /**
  89. 外部主动断开蓝牙链接
  90. @param peripheral 待链接的CBPeripheral对象
  91. @param options 一个可选的字典,指定连接行为选项
  92. */
  93. - (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
  94. /**
  95. 外部主动断开蓝牙链接
  96. @param peripheral 待断开的CBPeripheral对象
  97. */
  98. - (void)cancelPeripheralConnection:(int)type;
  99. #pragma mark -------------- public methon ==>> 外部 操作 CBPeripheral
  100. /**
  101. 主动向蓝牙写入数据
  102. @param data 数据流
  103. @param characteristic 可以写入的特征
  104. @param type 写入数据的类型
  105. */
  106. - (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic writeType:(CBCharacteristicWriteType)writeType deviceType:(DEVICE_TYPE)deviceType;
  107. @end
  108. NS_ASSUME_NONNULL_END