涉及到蓝牙操作的一定会接触到升级 我是安照TI 写的不过只是验证了蓝牙升级的功能。
一开始首先需要把升级bin文件在iTunes上拖进APP 然后在APP里面读出来转成data
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
NSString*fileName = [pathstringByAppendingPathComponent:@"KeyV8.bin"];
NSMutableData*reader = [NSMutableDatadataWithContentsOfFile:fileName];
这个数据就是你要准备升级的数据了
然后连接蓝牙
如果是可以升级的蓝牙会在服务下有两个UUID
如图:
FF12不用管
下面的才是有关升级的 那个identity的UUID是进行验证的
BLOCK 是写数据的 然后首先要验证在连接成功后
-(void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(NSError*)error
{
if(error)
{
NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [errorlocalizedDescription]);
return;
}
for(CBService*serviceinperipheral.services)
{
// NSLog(@"service2service2 %@",service);
if([service.UUID.UUIDStringisEqualToString:@"F000FFC0-0451-4000-B000-000000000000"])
{
[peripheraldiscoverCharacteristics:nilforService:service];
}
}
}
在这个代理里面遍历这个服务
拿到那两个UUID
-(void)peripheral:(CBPeripheral*)peripheral didDiscoverCharacteristicsForService:(nonnullCBService*)service error:(nullableNSError*)error
{
//NSLog(@"_disper.state%ld",(long)_disper.state);
if(error)
{
NSLog(@"error Discovered characteristics for %@ with error: %@", service.UUID, [errorlocalizedDescription]);
return;
}
if(service.characteristics.count==2) {
for(CBCharacteristic*characteristicinservice.characteristics)
{
_writechar= characteristic;
if([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"F000FFC1-0451-4000-B000-000000000000"]])
{
_writechar1= characteristic;
if(_readChar!= characteristic) {
_readChar= characteristic;
}
[peripheralreadValueForCharacteristic:_readChar];
[peripheralsetNotifyValue:YESforCharacteristic:_readChar];
}
elseif([characteristic.UUIDisEqual:[CBUUIDUUIDWithString:@"F000FFC2-0451-4000-B000-000000000000"]])
{
if(_writechar!= characteristic) {
_writechar= characteristic;
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSData*data0 = [TOOL_datadataWithHexString:@"00"];
[_disperwriteValue:data0forCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSData*data0 = [TOOL_datadataWithHexString:@"01"];
[_disperwriteValue:data0forCharacteristic:characteristictype:CBCharacteristicWriteWithoutResponse];
});
});
}
}
}
那两个服务都是可读可写的 先想identity 里面发00 01 得到的数据用
-(NSInteger)please:(Byte)byte1 and:(Byte)byte0
{
inta = ((byte1<<8)&0XFFFF);
return(a + (byte0 &0xff));
}
这个方法计算
Byteb0 = ((Byte*)([characteristic.valuebytes]))[0];
Byteb1 = ((Byte*)([characteristic.valuebytes]))[1];
NSLog(@"%c%c",b0,b1);
NSInteger c =[selfplease:b1and:b0];
Byteb7 = ((Byte*)([characteristic.valuebytes]))[3];
Byteb6 = ((Byte*)([characteristic.valuebytes]))[2];
NSInteger c2 = [selfplease:b7and:b6];
NSInteger type = ((c &1) ==1) ?'B':'A';
NSLog(@"typetype%d",type);
type 是升级哪个服务 值是A或B的ASCII值 c1 和 c2 一个是版本和长度
这拿TI一看就懂
然后把文件哪个data第0个字节和 第一个还有第7个字节和第6个运算
Byteb0 = ((Byte*)([readerbytes]))[5];
Byteb1 = ((Byte*)([readerbytes]))[4];
NSIntegerver =[selfboplease:b0andbo:b1];
NSLog(@"cc %d",ver);
Byteb7 = ((Byte*)([readerbytes]))[7];
Byteb6 = ((Byte*)([readerbytes]))[6];
NSIntegerlen =[selfboplease:b7andbo:b6];
NSLog(@"cc12 %d",len);
-(NSInteger)lou:(NSInteger)a
{return(a &0xFF);}
-(NSInteger)hi:(NSInteger)a
{return(a >>8);}
NSInteger buf0 = [selflou:ver];
NSInteger buf1 = [selfhi:ver];
NSInteger buf2 = [selflou:len];
NSInteger buf3 = [selfhi:len];
把这4个字节拼接后面在加上s升级bin文件的第9个字节连续在加4后面补0补齐12个字节
03 00 00 9A 42 42 42 42 00 00 00 00
把他发向第一个UUID
然后在把文件发向第2个UUID
每包18个前两个字节是吧包数用
-(NSInteger)lou:(NSInteger)a
{return(a &0xFF);}
-(NSInteger)hi:(NSInteger)a
{return(a >>8);}
这两个方法算出
第一个字节lou 第二个hi这个方法算
后面16个 字节就是文件里的数据往下发送
每包30毫秒 发送完成后蓝牙会断开 然后