BTDataSend.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // BTDataSend.m
  3. // Unity-iPhone
  4. //
  5. // Created by duowan123 on 2021/12/21.
  6. //
  7. #import "BTDataSend.h"
  8. @implementation BTDataSend
  9. //单例静态
  10. static BTDataSend* instance = nil;
  11. +(instancetype)sharedInstance{
  12. // NSLog(@"创建单例一次 1");
  13. return [[self alloc] init];
  14. }
  15. + (instancetype)allocWithZone:(struct _NSZone *)zone{
  16. // NSLog(@"创建单例一次 2");
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. instance = [super allocWithZone:zone];
  20. });
  21. return instance;
  22. }
  23. - (instancetype)init{
  24. // NSLog(@"创建单例一次 3");
  25. static dispatch_once_t onceToken;
  26. dispatch_once(&onceToken, ^{
  27. instance = [super init];
  28. });
  29. return instance;
  30. }
  31. #pragma mark ===============================================>> 蓝牙发送各类数据
  32. //查询设备信息
  33. - (void)queryDevideInfo{
  34. //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
  35. NSMutableData * writeData = [NSMutableData new];
  36. Byte header = 0xaa;
  37. Byte length = 0x06;//长度A1->6 A2->5 A3->7
  38. Byte lengthNegation = ~length;
  39. Byte cmd = 0xA1;
  40. Byte data = 0x1;
  41. Byte byte[] = {header,length,lengthNegation,cmd,data};
  42. writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
  43. // [writeData appendData:msData];
  44. Byte bcc = [AlgorithmTool byteSumBBC:writeData];
  45. [writeData appendBytes:&bcc length:sizeof(bcc)];
  46. if (LEManager.peripheral!=nil&&LEManager.write!=nil){
  47. NSLog(@"发送的报文 主设备 查询设备信息 %@", writeData);
  48. [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
  49. }
  50. if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
  51. NSLog(@"发送的报文 副设备 查询设备信息 %@", writeData);
  52. [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
  53. }
  54. }
  55. //打开游戏模式
  56. - (void)startGameModel{
  57. //0xaa06f9a2014c
  58. //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
  59. NSMutableData * writeData = [NSMutableData new];
  60. Byte header = 0xaa;
  61. Byte length = 0x06;//长度A1->6 A2->5 A3->7
  62. Byte lengthNegation = ~length;
  63. Byte cmd = 0xA2;
  64. Byte data = 0x1;
  65. //计算校验位
  66. Byte byte[] = {header,length,lengthNegation,cmd,data};
  67. writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
  68. Byte bcc = [AlgorithmTool byteSumBBC:writeData];
  69. //传输数据 -->> NSData
  70. [writeData appendBytes:&bcc length:sizeof(bcc)];
  71. if (LEManager.peripheral!=nil&&LEManager.write!=nil){
  72. NSLog(@"发送的报文 主设备 开启游戏模式: %@", writeData);
  73. [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
  74. }
  75. if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
  76. NSLog(@"发送的报文 副设备 开启游戏模式: %@", writeData);
  77. [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
  78. }
  79. }
  80. //设置震动
  81. - (void)vibration:(DEVICE_TYPE)deviceType duration:(int)duration leftOrRight:(int)leftOrRight{
  82. //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
  83. NSMutableData * writeData = [NSMutableData new];
  84. Byte header = 0xaa;
  85. Byte length = 0x08;//长度A1->6 A2->5 A3->7
  86. Byte lengthNegation = ~length;
  87. Byte cmd = 0xA4;
  88. //振动时间 2字节
  89. short ms = 400;//400毫秒
  90. Byte mslow = (Byte) (0x00FF & ms);//定义第一个byte
  91. Byte mshigh = (Byte) (0x00FF & (ms>>8));//定义第二个byte
  92. Byte leftOrRightByte = 0x00;//全部
  93. // Byte leftOrRight = 0x01;//左脚
  94. // Byte leftOrRight = 0x02;//右脚
  95. //计算校验位
  96. Byte bytes[] = {header,length,lengthNegation,cmd,mshigh,mslow,leftOrRightByte};
  97. Byte bcc = [AlgorithmTool bbcByte:bytes];
  98. //传输数据 Byte -->> NSData
  99. Byte newByte[] = {header,length,lengthNegation,cmd,mshigh,mslow,leftOrRightByte,bcc};
  100. writeData = [[NSMutableData alloc] initWithBytes:newByte length:sizeof(newByte)];
  101. if (deviceType==DEVICETYPE_MAIN&&LEManager.write!=nil){
  102. NSLog(@"发送的报文 主设备 设备震动 %@", writeData);
  103. [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
  104. }else if (deviceType == DEVICETYPE_VICE&&LEManager.viceWrite!=nil){
  105. NSLog(@"发送的报文 副设备 设备震动 %@", writeData);
  106. [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
  107. }
  108. }
  109. //开启/关闭 激光模式
  110. - (void)laser:(int)state{
  111. //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
  112. NSMutableData * writeData = [NSMutableData new];
  113. Byte header = 0xaa;
  114. Byte length = 0x06;//长度A1->6 A2->5 A3->7
  115. Byte lengthNegation = ~length;
  116. Byte cmd = 0xA9;
  117. //激光状态 字节
  118. Byte data;
  119. if (state ==0){
  120. data = 0x00;
  121. }else if(state==1){
  122. data = 0x01;
  123. }
  124. //计算校验位
  125. Byte bytes[] = {header,length,lengthNegation,cmd,data};
  126. Byte bcc = [AlgorithmTool bbcByte:bytes];
  127. //传输数据 Byte -->> NSData
  128. Byte newByte[] = {header,length,lengthNegation,cmd,data,bcc};
  129. writeData = [[NSMutableData alloc] initWithBytes:newByte length:sizeof(newByte)];
  130. if (LEManager.peripheral!=nil){
  131. NSLog(@"发送的报文 主设备 开启激光: %@", writeData);
  132. [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
  133. }
  134. if (LEManager.vicePeripheral!=nil){
  135. NSLog(@"发送的报文 副设备 开启激光: %@", writeData);
  136. [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
  137. }
  138. }
  139. -(void)keepConnect{
  140. //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
  141. NSMutableData * writeData = [NSMutableData new];
  142. Byte header = 0xaa;
  143. Byte length = 0x06;//长度A1->6 A2->5 A3->7
  144. Byte lengthNegation = ~length;
  145. Byte cmd = 0xB0;
  146. Byte data = 0x01;
  147. Byte byte[] = {header,length,lengthNegation,cmd,data};
  148. writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
  149. // [writeData appendData:msData];
  150. Byte bcc = [AlgorithmTool byteSumBBC:writeData];
  151. [writeData appendBytes:&bcc length:sizeof(bcc)];
  152. if (LEManager.peripheral!=nil&&LEManager.write!=nil){
  153. NSLog(@"发送的报文 主设备 保持链接 %@", writeData);
  154. [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
  155. }
  156. if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
  157. NSLog(@"发送的报文 副设备 保持链接 %@", writeData);
  158. [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
  159. }
  160. }
  161. @end