蓝牙的简单应用
之前也是刚接触蓝牙,就自己试着查找蓝牙的相关文档,说实话,第一次接触的时候是真的是头大,不过自己也不是轻言放弃的人。写了一个小小的连接,希望对你们有用。
蓝牙的相关知识
1 //电池的service uuid 2 static NSString *BATTERY_SERVICE = 3 //电池的特征值的UUID 4 static NSString *BATTERY_READ_CHAR = 5 //体温数据的service UUID 6 static NSString *HEALTH_SERVICE =; 7 //体温特征值的UUID 8 static NSString *HEALTH_READ_CHAR = 9 10 11 @interface ViewController () <CBCentralManagerDelegate,CBPeripheralDelegate> 12 @property (weak, nonatomic) IBOutlet UILabel *labelState; 13 @property (weak, nonatomic) IBOutlet UILabel *labelTemperature; 14 @property (weak, nonatomic) IBOutlet UILabel *labelBattery; 15 16 @property(nonatomic,strong) CBCentralManager *bleManager; 17 @property(nonatomic,strong) NSMutableArray *multArray; 18 @property(nonatomic,strong) CBPeripheral *myPeripheral; 19 20 - (IBAction)startScan:(UIButton *)sender; 21 - (IBAction)stopScan:(UIButton *)sender; 22 23 @end 24 25 @implementation ViewController 26 27 - (void)viewDidLoad { 28 [super viewDidLoad]; 29 _bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; 30 } 31 32 - (void)centralManagerDidUpdateState:(CBCentralManager *)central{ 33 NSLog(@"centralManagerDidUpdateState"); 34 switch (central.state) { 35 case CBCentralManagerStatePoweredOff: 36 NSLog(@"蓝牙已关闭"); 37 break; 38 case CBCentralManagerStatePoweredOn: 39 NSLog(@"蓝牙已打开"); 40 break; 41 case CBCentralManagerStateUnsupported: 42 NSLog(@"不支持蓝牙"); 43 break; 44 45 default: 46 break; 47 } 48 } 49 -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{ 50 NSLog(@"peripheral--:%@",peripheral); 51 if ([peripheral.name containsString:@"Thermometer"]) { 52 53 self.labelState.text = @"已发现设备"; 54 peripheral.delegate = self; 55 NSLog(@"开始连接设备"); 56 self.myPeripheral = peripheral; 57 [self.bleManager connectPeripheral:self.myPeripheral options:nil]; 58 59 } 60 } 61 /** 62 *连接上设备的信息回调,连接上设备后,开始发现服务 63 */ 64 -(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ 65 NSLog(@"设备连接成功,准备扫描服务"); 66 67 [peripheral discoverServices:nil]; 68 69 self.labelState.text = @"设备连接成功"; 70 } 71 -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ 72 self.labelState.text = @"设备已经断开"; 73 if (error) { 74 NSLog(@"didDisconnectPeripheral with error : %@",error); 75 return; 76 } 77 } 78 79 80 /** 81 扫描外设服务的数据回调 82 */ 83 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ 84 85 if (error) { 86 NSLog(@"didDiscoverServices with error : %@",error); 87 return; 88 } 89 90 91 for (CBService *service in peripheral.services) { 92 NSLog(@"service uuid: %@",service.UUID.UUIDString); 93 if ([service.UUID.UUIDString isEqualToString:BATTERY_SERVICE]) { 94 NSLog(@"已经发现电池对应的服务"); 95 [peripheral discoverCharacteristics:nil forService:service]; 96 } 97 98 if ([service.UUID.UUIDString isEqualToString:HEALTH_SERVICE]) { 99 NSLog(@"已经发现体温数据对应的服务"); 100 [peripheral discoverCharacteristics:nil forService:service]; 101 } 102 } 103 } 104 105 -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ 106 107 NSLog(@"发现服务 %@, 特性数: %ld", service.UUID, [service.characteristics count]); 108 if (error) { 109 NSLog(@"didDiscoverCharacteristicsForService with error : %@",error); 110 return; 111 } 112 113 //电池电量的数据直接读对应的特征值即可 114 if ([service.UUID.UUIDString isEqualToString:BATTERY_SERVICE]) { 115 for (CBCharacteristic *character in service.characteristics) { 116 if ([character.UUID.UUIDString isEqualToString:BATTERY_READ_CHAR]) { 117 NSLog(@"已找到電池对应服务中的 特征值"); 118 [peripheral readValueForCharacteristic:character]; 119 120 } 121 } 122 } 123 124 125 //体温数据需要通过notify的方式进行获取 126 if ([service.UUID.UUIDString isEqualToString:HEALTH_SERVICE]) { 127 for (CBCharacteristic *character in service.characteristics) { 128 if ([character.UUID.UUIDString isEqualToString:HEALTH_READ_CHAR]) { 129 NSLog(@"已找到体温对应服务中的 特征值"); 130 [peripheral setNotifyValue:YES forCharacteristic:character]; 131 } 132 } 133 } 134 135 136 } 137 138 -(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ 139 if (error) { 140 NSLog(@"didUpdateValueForCharacteristic with error : %@",error); 141 return; 142 } 143 144 NSData *data = characteristic.value; 145 NSLog(@"收到数据啦 %@---%@", data,characteristic.UUID); 146 147 //电量数据 148 if ([characteristic.UUID.UUIDString isEqualToString:BATTERY_READ_CHAR]) { 149 int battery; 150 [data getBytes: &battery length: sizeof(battery)]; 151 self.labelBattery.text = [NSString stringWithFormat:@"%d",battery]; 152 } 153 //体温数据 154 if ([characteristic.UUID.UUIDString isEqualToString:HEALTH_READ_CHAR]) { 155 Byte *testByte = (Byte *)[data bytes]; 156 Byte orgin[4]; 157 orgin[0] = testByte[1]; 158 orgin[1] = testByte[2]; 159 orgin[2] = testByte[3]; 160 161 NSData *chidata=[NSData dataWithBytes:orgin length:4]; 162 int code; 163 [chidata getBytes:&code length:4]; 164 //NSInteger temp = [self byteToInt:orgin]; 165 self.labelTemperature.text = [NSString stringWithFormat:@"%.2f",(0.1+0.01*code)]; 166 } 167 } 168 - (IBAction)startScan:(UIButton *)sender { 169 [self.bleManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)}]; 170 NSLog(@"开始扫描"); 171 } 172 - (IBAction)stopScan:(UIButton *)sender { 173 NSLog(@"停止扫描"); 174 [self.bleManager stopScan ]; 175 [self.bleManager cancelPeripheralConnection:self.myPeripheral]; 176 177 self.labelBattery.text = @""; 178 self.labelState.text = @""; 179 self.labelTemperature.text = @""; 180 }