涉及到藍牙操作的一定會接觸到升級 我是安照TI 寫的不過只是驗證了藍牙升級的功能。
一開始首先需要把升級bin文件在iTunes上拖進APP 然后在APP里面讀出來轉(zhuǎn)成data
NSString*path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject];
NSString*fileName = [pathstringByAppendingPathComponent:@"KeyV8.bin"];
NSMutableData*reader = [NSMutableDatadataWithContentsOfFile:fileName];
這個數(shù)據(jù)就是你要準備升級的數(shù)據(jù)了
然后連接藍牙
如果是可以升級的藍牙會在服務下有兩個UUID
如圖:
FF12不用管
下面的才是有關(guān)升級的 那個identity的UUID是進行驗證的
BLOCK 是寫數(shù)據(jù)的 然后首先要驗證在連接成功后
-(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 里面發(fā)00 01 得到的數(shù)據(jù)用
-(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個字節(jié)和 第一個還有第7個字節(jié)和第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個字節(jié)拼接后面在加上s升級bin文件的第9個字節(jié)連續(xù)在加4后面補0補齊12個字節(jié)
03 00 00 9A 42 42 42 42 00 00 00 00
把他發(fā)向第一個UUID
然后在把文件發(fā)向第2個UUID
每包18個前兩個字節(jié)是吧包數(shù)用
-(NSInteger)lou:(NSInteger)a
{return(a &0xFF);}
-(NSInteger)hi:(NSInteger)a
{return(a >>8);}
這兩個方法算出
第一個字節(jié)lou 第二個hi這個方法算
后面16個 字節(jié)就是文件里的數(shù)據(jù)往下發(fā)送
每包30毫秒 發(fā)送完成后藍牙會斷開 然后