BabySpeaker.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. BabyBluetooth
  3. 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮
  4. https://github.com/coolnameismy/BabyBluetooth
  5. */
  6. // Created by 刘彦玮 on 15/9/2.
  7. // Copyright (c) 2015年 刘彦玮. All rights reserved.
  8. //
  9. #import "BabySpeaker.h"
  10. #import "BabyDefine.h"
  11. typedef NS_ENUM(NSUInteger, BabySpeakerType) {
  12. BabySpeakerTypeDiscoverPeripherals,
  13. BabySpeakerTypeConnectedPeripheral,
  14. BabySpeakerTypeDiscoverPeripheralsFailToConnect,
  15. BabySpeakerTypeDiscoverPeripheralsDisconnect,
  16. BabySpeakerTypeDiscoverPeripheralsDiscoverServices,
  17. BabySpeakerTypeDiscoverPeripheralsDiscoverCharacteristics,
  18. BabySpeakerTypeDiscoverPeripheralsReadValueForCharacteristic,
  19. BabySpeakerTypeDiscoverPeripheralsDiscoverDescriptorsForCharacteristic,
  20. BabySpeakerTypeDiscoverPeripheralsReadValueForDescriptorsBlock
  21. };
  22. @implementation BabySpeaker {
  23. //所有委托频道
  24. NSMutableDictionary *channels;
  25. //当前委托频道
  26. NSString *currChannel;
  27. //notifyList
  28. NSMutableDictionary *notifyList;
  29. }
  30. - (instancetype)init {
  31. self = [super init];
  32. if (self) {
  33. BabyCallback *defaultCallback = [[BabyCallback alloc]init];
  34. notifyList = [[NSMutableDictionary alloc]init];
  35. channels = [[NSMutableDictionary alloc]init];
  36. currChannel = KBABY_DETAULT_CHANNEL;
  37. [channels setObject:defaultCallback forKey:KBABY_DETAULT_CHANNEL];
  38. }
  39. return self;
  40. }
  41. - (BabyCallback *)callback {
  42. return [channels objectForKey:KBABY_DETAULT_CHANNEL];
  43. }
  44. - (BabyCallback *)callbackOnCurrChannel {
  45. return [self callbackOnChnnel:currChannel];
  46. }
  47. - (BabyCallback *)callbackOnChnnel:(NSString *)channel {
  48. if (!channel) {
  49. [self callback];
  50. }
  51. return [channels objectForKey:channel];
  52. }
  53. - (BabyCallback *)callbackOnChnnel:(NSString *)channel
  54. createWhenNotExist:(BOOL)createWhenNotExist {
  55. BabyCallback *callback = [channels objectForKey:channel];
  56. if (!callback && createWhenNotExist) {
  57. callback = [[BabyCallback alloc]init];
  58. [channels setObject:callback forKey:channel];
  59. }
  60. return callback;
  61. }
  62. - (void)switchChannel:(NSString *)channel {
  63. if (channel) {
  64. if ([self callbackOnChnnel:channel]) {
  65. currChannel = channel;
  66. BabyLog(@">>>已切换到%@",channel);
  67. }
  68. else {
  69. BabyLog(@">>>所要切换的channel不存在");
  70. }
  71. }
  72. else {
  73. currChannel = KBABY_DETAULT_CHANNEL;
  74. BabyLog(@">>>已切换到默认频道");
  75. }
  76. }
  77. //添加到notify list
  78. - (void)addNotifyCallback:(CBCharacteristic *)c
  79. withBlock:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block {
  80. [notifyList setObject:block forKey:c.UUID.description];
  81. }
  82. //添加到notify list
  83. - (void)removeNotifyCallback:(CBCharacteristic *)c {
  84. [notifyList removeObjectForKey:c.UUID.description];
  85. }
  86. //获取notify list
  87. - (NSMutableDictionary *)notifyCallBackList {
  88. return notifyList;
  89. }
  90. //获取notityBlock
  91. - (void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))notifyCallback:(CBCharacteristic *)c {
  92. return [notifyList objectForKey:c.UUID.description];
  93. }
  94. @end