第一:現(xiàn)在項(xiàng)目中導(dǎo)入CoreBluetooth.framework框架绵脯。
第二:藍(lán)牙的使用。
在使用的類中導(dǎo)入#import<CoreBluetooth/CoreBluetooth.h>
步驟:
<1>.首先創(chuàng)建和初始化中心設(shè)備CBCentralManager休里。
dispatch_queue_t centralQueue = dispatch_queue_create("定義線程的名字", DISPATCH_QUEUE_SERIAL);
bluetoothManager = [[CBCentralManager alloc] initWithDelegate:self queue:centralQueue];
-(void)centralManagerDidUpdateState:(CBCentralManager *)central{
NSLog(@"[ScanTableViewController] [centralManagerDidUpdateState] state === %ld", (long)central.state);
if (central.state == CBCentralManagerStatePoweredOn) {
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
} else {
NSLog(@"藍(lán)牙沒有正常打開");
}
}
<2>.對外圍設(shè)備進(jìn)行掃描蛆挫,顯示到掃描結(jié)果的頁面,并且對藍(lán)牙進(jìn)行連接妙黍。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
dispatch_async(dispatch_get_main_queue(), ^{
// Add the sensor to the list and reload deta set
ScannedPeripheral* sensor = [ScannedPeripheral initWithPeripheral:peripheral rssi:RSSI.intValue isPeripheralConnected:NO];
if (![peripherals containsObject:sensor]) {
[peripherals addObject:sensor];
} else {
sensor = [peripherals objectAtIndex:[peripherals indexOfObject:sensor]];
sensor.RSSI = RSSI.intValue;
}
});
//然后反應(yīng)到相應(yīng)的View中
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"[ScanTableViewController] [didConnectPeripheral]");
isConnectingOneDevice = NO;
[timer invalidate];
CachedBLEDevice* device = [CachedBLEDevice defaultInstance];
if ([[DBRecorder getFMDBDeviceParameters] count]) {
[DBRecorder deleteFMDBDevice:device.mDeviceIdentifier];
}
device.mDeviceName = [peripheral name];
device.mDeviceIdentifier = [[peripheral identifier] UUIDString];
device.mAlertEnabled = false;
device.mRangeAlertEnabled = true;
device.mRangeType = RANGE_ALERT_OUT;
device.mRangeValue = RANGE_ALERT_FAR;
device.mDisconnectEnabled = true;
device.mRingtoneEnabled = true;
device.mVibrationEnabled = true;
device.mConnectionState = CONNECTION_STATE_CONNECTED;
[device setDevicePeripheral:peripheral];
NSLog(@"保存到數(shù)據(jù)庫中的值:%@", [device getDevicePeripheral]);
[device persistData:1];
iSmartWatchConfiguration *configuration = [iSmartWatchConfiguration sharedConfiguration];
configuration.software.paired = YES;
[configuration save];
if (nil != connectingCell) {
[connectingCell showIndicator:NO];
}
MainTabBarViewController *mainTVC = [[MainTabBarViewController alloc] init];
[self.navigationController presentViewController:mainTVC animated:YES completion:NULL];
});
}
<3>.對已經(jīng)連接的設(shè)備發(fā)現(xiàn)服務(wù)悴侵。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"[RecordController] [didDiscoverServices]");
if (!error) {
NSLog(@"services discovered %lu",(unsigned long)[peripheral.services count] );
for (CBService *service in peripheral.services) {
NSLog(@"service discovered: %@", service.UUID);
if ([service.UUID isEqual:batteryServiceUUID]) {
NSLog(@"Battery service found");
[self.recordPeripheral discoverCharacteristics:nil forService:service];
} else if ([service.UUID isEqual:dogpServiceUUID]) {
NSLog(@"Dogp service found");
kisfinishFile = 0;
[self.recordPeripheral discoverCharacteristics:nil forService:service];
} else if ([service.UUID isEqual:proximityLinkLossServiceUUID]) {
NSLog(@"Linkloss service is found");
[self.recordPeripheral discoverCharacteristics:nil forService:service];
}
else if ([service.UUID isEqual:proximityImmediateAlertServiceUUID]) {
NSLog(@"Immidiate Alert service is found");
[self.recordPeripheral discoverCharacteristics:nil forService:service];
}
}
} else {
NSLog(@"error in discovering services on device: %@", self.recordPeripheral.name);
}
}
<4>.對服務(wù)進(jìn)行UUID判斷,發(fā)現(xiàn)相應(yīng)的特征拭嫁。
-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (!error) {
if ([service.UUID isEqual:dogpServiceUUID]) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:dogpReadCharacteristicUUID]) {
NSLog(@"Dogp service read characteristic is found");
[self.recordPeripheral setNotifyValue:YES forCharacteristic:characteristic];
}
if ([characteristic.UUID isEqual:dogpWriteCharacteristicUUID]) {
NSLog(@"Dogp service write characteristic is found");
dogpWriteCharacteristic = characteristic;
}
}
}
<5>.對發(fā)現(xiàn)的特征進(jìn)行判斷可免,進(jìn)行讀取操作。
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if ([characteristic.UUID isEqual:dogpReadCharacteristicUUID]) {
NSLog(@"獲取到讀的UUID:%@, ---%@",characteristic.UUID, characteristic.value);
NSString *content = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@"%s", characteristic.value.bytes);
NSLog(@"----> dogp content === %@", content);
if ([content isEqualToString:@"ios indication"]) {
[self.recordPeripheral readValueForCharacteristic:characteristic];
dogpReadCharacteristic = characteristic;
return;
}else{
NSString *hexStr = [self convertDataToHexStr:characteristic.value];
NSLog(@"手表收到的十六進(jìn)制的數(shù)據(jù):%@", hexStr);
}
}
}
<6>.對讀取到的數(shù)據(jù)進(jìn)行數(shù)據(jù)處理做粤。
if ([characteristic.UUID isEqual:dogpReadCharacteristicUUID]) {
NSLog(@"獲取到讀的UUID:%@, ---%@",characteristic.UUID, characteristic.value);
NSString *content = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@"%s", characteristic.value.bytes);
NSLog(@"----> dogp content === %@", content);
if ([content isEqualToString:@"ios indication"]) {
[self.recordPeripheral readValueForCharacteristic:characteristic];
dogpReadCharacteristic = characteristic;
return;
}else{
NSString *hexStr = [self convertDataToHexStr:characteristic.value];
NSLog(@"手表收到的十六進(jìn)制的數(shù)據(jù):%@", hexStr);
}
}
第三:與手表或者其他藍(lán)牙設(shè)備進(jìn)行通信巴元。
NSInteger length = [hexStr length];
NSString *contentOfDataStr = [hexStr substringWithRange:NSMakeRange(0, length-2)];
NSString *checkStr = [hexStr substringWithRange:NSMakeRange(length-2, 2)];
//check len of data
NSLog(@"除去校驗(yàn)值的值:%@, 校驗(yàn)值:%@", contentOfDataStr, checkStr);
NSString *getCheckSum = [self checkTheString:contentOfDataStr];
NSString *comToLenStr = [hexStr substringWithRange:NSMakeRange(2, 14)];
NSString *splitCommdStr = @"";
NSFileManager *fileManager = [NSFileManager defaultManager];
//check the code
if ([[checkStr uppercaseString] isEqualToString:getCheckSum]) {
NSLog(@"check checkSum is right");
NSString *data_typeStr = [hexStr substringWithRange:NSMakeRange(10, 2)];
if ([data_typeStr isEqualToString:@"00"]) {
//The name of file
NSString *fileName = [hexStr substringWithRange:NSMakeRange(20, hexStr.length - 22)];
NSLog(@"創(chuàng)建的文件名:%@", fileName);
data_number = 0;
//判斷文件是否存在
filePath = [self createFile:fileName type:@"1"];
NSMutableArray *dataArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"fitName"];
NSString *compare_fileName = [NSString stringWithFormat:@"FIT_%@.fit", fileName];
[dataArray addObject:@"1"];
NSLog(@"保存文件路徑的數(shù)組:%@", dataArray);
NSLog(@"文件路徑:%@", compare_fileName);
if (([dataArray containsObject:compare_fileName] && dataArray != NULL) || ([filePath isEqualToString:@""])) {
kisStat = @"02";
}else{
NSString *tempFile = [self createFile:[fileName stringByAppendingString:@"0"] type:@"0"];
if ([tempFile isEqualToString:@""]) {
[fileManager removeItemAtPath:tempPath error:nil];
kisStat = @"01";
}else{
tempPath = tempFile;
[fileManager createFileAtPath:tempPath contents:nil attributes:nil];
kisStat = @"00";
}
}
}else if ([data_typeStr isEqualToString:@"01"]){
//insert data into Local file
if ([self checkLenOfData:hexStr]) {
NSLog(@"data len is right");
kisStat = @"00";
data_number += 1;
NSInteger lengOfhex = hexStr.length;
NSString *dataStr = [hexStr substringWithRange:NSMakeRange(20, lengOfhex-22)];
NSLog(@"每次寫入文件的數(shù)-------:%@", [dataStr uppercaseString]);
[self insertData:dataStr toFile:tempPath];
}else{
NSLog(@"data len is wrong");
kisStat = @"01";
}
}else if ([data_typeStr isEqualToString:@"02"]){
//check infomation
NSString *fileNumberStr = [hexStr substringWithRange:NSMakeRange(20, 4)];
if ([self checkInformation:[fileNumberStr uppercaseString]]){
NSLog(@"check successful");
NSString *sizeOffileStr = [hexStr substringWithRange:NSMakeRange(24, 8)];
long localLength = [self getCacheFileSize:tempPath];
long watch_length = [self getNumberOfString:[sizeOffileStr uppercaseString]];
NSLog(@"localLength:%ld, -----watch_length:%ld", localLength, watch_length);
if (localLength == watch_length) {
BOOL isChange = [fileManager moveItemAtPath:tempPath toPath:filePath error:nil];
NSLog(@"%d", isChange);
NSLog(@"temp文件大小:%ld-----file文件大小:%ld", [self getCacheFileSize:tempPath], [self getCacheFileSize:filePath]);
if (isChange) {
[fileManager removeItemAtPath:tempPath error:nil];
}else{
NSLog(@"isChange fail");
}
NSLog(@"size of file is right");
kisStat = @"00";
}else{
NSLog(@"size of file is wrong");
kisStat = @"01";
}
}else{
NSLog(@"check fail");
kisStat = @"01";
}
}else if ([data_typeStr isEqualToString:@"03"]){
//sync end
kisfinishFile = 1;
NSString *statStr = [hexStr substringWithRange:NSMakeRange(20, 2)];
if ([statStr isEqualToString:@"01"]) {
[fileManager removeItemAtPath:tempPath error:nil];
}
NSLog(@"parse file");
//獲取到多少個(gè)fit文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDiectory = [paths objectAtIndex:0];
NSString *appDirectory = [documentDiectory stringByAppendingPathComponent:@"rftech-smartwatch-fit"];
NSArray *fileArray = [fileManager subpathsAtPath:appDirectory];
NSLog(@"同步獲取到的fit文件%@", fileArray);
CustomAlertWindow *customAlertWindow = [CustomAlertWindow shareCustomAlertWindow];
if (fileArray.count != 0) {
[customAlertWindow setNotes:@"正在解析數(shù)據(jù)"];
[customAlertWindow show];
[self pareQueueManagercustom:customAlertWindow andFileManager:fileManager andFileArray:fileArray andAppDirectory:appDirectory];
}else{
[customAlertWindow dismiss];
}
}
}else{
NSLog(@"check checkSum is wrong");
kisStat = @"01";
}
if (kisfinishFile == 0) {
splitCommdStr = [self getCommandStr:comToLenStr andStat:kisStat];
NSLog(@"split of CommdStr:%@", splitCommdStr);
[self replyFit:[self convertHexStrToData:splitCommdStr]];
}