// // 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