123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- //
- // BTDataSend.m
- // Unity-iPhone
- //
- // Created by duowan123 on 2021/12/21.
- //
- #import "BTDataSend.h"
- @implementation BTDataSend
- //单例静态
- static BTDataSend* instance = nil;
- +(instancetype)sharedInstance{
- // NSLog(@"创建单例一次 1");
- return [[self alloc] init];
- }
- + (instancetype)allocWithZone:(struct _NSZone *)zone{
- // NSLog(@"创建单例一次 2");
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [super allocWithZone:zone];
- });
- return instance;
- }
- - (instancetype)init{
- // NSLog(@"创建单例一次 3");
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [super init];
- });
- return instance;
- }
- #pragma mark ===============================================>> 蓝牙发送各类数据
- //查询设备信息
- - (void)queryDevideInfo{
-
- //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
- NSMutableData * writeData = [NSMutableData new];
-
- Byte header = 0xaa;
- Byte length = 0x06;//长度A1->6 A2->5 A3->7
- Byte lengthNegation = ~length;
- Byte cmd = 0xA1;
- Byte data = 0x1;
- Byte byte[] = {header,length,lengthNegation,cmd,data};
- writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
- // [writeData appendData:msData];
- Byte bcc = [AlgorithmTool byteSumBBC:writeData];
- [writeData appendBytes:&bcc length:sizeof(bcc)];
- if (LEManager.peripheral!=nil&&LEManager.write!=nil){
- NSLog(@"发送的报文 主设备 查询设备信息 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
- }
- if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
- NSLog(@"发送的报文 副设备 查询设备信息 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
- }
-
- }
- //打开游戏模式
- - (void)startGameModel{
-
- //0xaa06f9a2014c
- //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
- NSMutableData * writeData = [NSMutableData new];
- Byte header = 0xaa;
- Byte length = 0x06;//长度A1->6 A2->5 A3->7
- Byte lengthNegation = ~length;
- Byte cmd = 0xA2;
- Byte data = 0x1;
- //计算校验位
- Byte byte[] = {header,length,lengthNegation,cmd,data};
- writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
- Byte bcc = [AlgorithmTool byteSumBBC:writeData];
- //传输数据 -->> NSData
- [writeData appendBytes:&bcc length:sizeof(bcc)];
- if (LEManager.peripheral!=nil&&LEManager.write!=nil){
- NSLog(@"发送的报文 主设备 开启游戏模式: %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
- }
-
- if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
- NSLog(@"发送的报文 副设备 开启游戏模式: %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
- }
-
- }
- //设置震动
- - (void)vibration:(DEVICE_TYPE)deviceType duration:(int)duration leftOrRight:(int)leftOrRight{
-
- //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
- NSMutableData * writeData = [NSMutableData new];
- Byte header = 0xaa;
- Byte length = 0x08;//长度A1->6 A2->5 A3->7
- Byte lengthNegation = ~length;
- Byte cmd = 0xA4;
-
- //振动时间 2字节
- short ms = 400;//400毫秒
- Byte mslow = (Byte) (0x00FF & ms);//定义第一个byte
- Byte mshigh = (Byte) (0x00FF & (ms>>8));//定义第二个byte
-
- Byte leftOrRightByte = 0x00;//全部
- // Byte leftOrRight = 0x01;//左脚
- // Byte leftOrRight = 0x02;//右脚
-
- //计算校验位
- Byte bytes[] = {header,length,lengthNegation,cmd,mshigh,mslow,leftOrRightByte};
- Byte bcc = [AlgorithmTool bbcByte:bytes];
- //传输数据 Byte -->> NSData
- Byte newByte[] = {header,length,lengthNegation,cmd,mshigh,mslow,leftOrRightByte,bcc};
- writeData = [[NSMutableData alloc] initWithBytes:newByte length:sizeof(newByte)];
- if (deviceType==DEVICETYPE_MAIN&&LEManager.write!=nil){
- NSLog(@"发送的报文 主设备 设备震动 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
- }else if (deviceType == DEVICETYPE_VICE&&LEManager.viceWrite!=nil){
- NSLog(@"发送的报文 副设备 设备震动 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
- }
-
- }
- //开启/关闭 激光模式
- - (void)laser:(int)state{
-
- //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
- NSMutableData * writeData = [NSMutableData new];
- Byte header = 0xaa;
- Byte length = 0x06;//长度A1->6 A2->5 A3->7
- Byte lengthNegation = ~length;
- Byte cmd = 0xA9;
- //激光状态 字节
- Byte data;
- if (state ==0){
- data = 0x00;
- }else if(state==1){
- data = 0x01;
- }
- //计算校验位
- Byte bytes[] = {header,length,lengthNegation,cmd,data};
- Byte bcc = [AlgorithmTool bbcByte:bytes];
- //传输数据 Byte -->> NSData
- Byte newByte[] = {header,length,lengthNegation,cmd,data,bcc};
- writeData = [[NSMutableData alloc] initWithBytes:newByte length:sizeof(newByte)];
- if (LEManager.peripheral!=nil){
- NSLog(@"发送的报文 主设备 开启激光: %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
- }
-
- if (LEManager.vicePeripheral!=nil){
- NSLog(@"发送的报文 副设备 开启激光: %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
- }
-
- }
- -(void)keepConnect{
-
- //头帧AA + 数据长度 + 长度取反 + cmd + 数据 + 校验
- NSMutableData * writeData = [NSMutableData new];
-
- Byte header = 0xaa;
- Byte length = 0x06;//长度A1->6 A2->5 A3->7
- Byte lengthNegation = ~length;
- Byte cmd = 0xB0;
- Byte data = 0x01;
- Byte byte[] = {header,length,lengthNegation,cmd,data};
- writeData = [[NSMutableData alloc] initWithBytes:byte length:sizeof(byte)];
- // [writeData appendData:msData];
- Byte bcc = [AlgorithmTool byteSumBBC:writeData];
- [writeData appendBytes:&bcc length:sizeof(bcc)];
- if (LEManager.peripheral!=nil&&LEManager.write!=nil){
- NSLog(@"发送的报文 主设备 保持链接 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.write writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_MAIN];
- }
- if (LEManager.vicePeripheral!=nil&&LEManager.viceWrite!=nil){
- NSLog(@"发送的报文 副设备 保持链接 %@", writeData);
- [LEManager writeValue:writeData forCharacteristic:LEManager.viceWrite writeType:CBCharacteristicWriteWithResponse deviceType:DEVICETYPE_VICE];
- }
-
- }
- @end
|