iOS 藍牙下空中升級操作

涉及到藍牙操作的一定會接觸到升級 我是安照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ā)送完成后藍牙會斷開 然后

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容