藍(lán)牙讀取信息過(guò)程:連接-查詢服務(wù)-發(fā)現(xiàn)服務(wù)废菱、搜索該服務(wù)包含的特征值-發(fā)現(xiàn)特征值-讀取特征值(讀取所需外設(shè)信息,包含major、minor驶冒、rssi、電量等等)
藍(lán)牙外設(shè)類CBPeripheral (代碼中的所有特征值UUID均向硬件廠商獲却钌恕)
@property (strong, nonatomic) CBPeripheral *beaconPeripheral;
self.beaconServiceUUID = [CBUUID UUIDWithString:@"xxxxxxxxxxx"]; //服務(wù)UUID需要找硬件廠商獲取
連接代碼
self.beaconPeripheral.delegate = self;
[self.bluetoothManager connectPeripheral:self.beaconPeripheral options:nil];
連接代理-CBCentralManagerDelegate
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"藍(lán)牙連接成功 %@",peripheral.name);
[self.beaconPeripheral discoverServices:[NSArray arrayWithObject:self.beaconServiceUUID]];//連接成功后查詢beacon服務(wù)
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"藍(lán)牙斷開連接 %@",peripheral.name);
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"連接失敗 %@",peripheral.name);
}
發(fā)現(xiàn)beacon服務(wù)代理--CBPeripheralDelegate
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for(CBService *service in peripheral.services)
{
if ([service.UUID isEqual:self.beaconServiceUUID]) {
//發(fā)現(xiàn)服務(wù)只怎,搜索特征值
[self.beaconPeripheral discoverCharacteristics:nil forService:service];
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if ([service.UUID isEqual:self.beaconServiceUUID]) {
for(CBCharacteristic *characteristic in service.characteristics)
{
//majorminor值
if ([characteristic.UUID isEqual:self.majorMinorCharacteristicUUID]) {
self.majorMinorCharacteristic = characteristic;
[self.beaconPeripheral readValueForCharacteristic:characteristic];//讀取
}
if ([characteristic.UUID isEqual:self.rssiCharacteristicUUID]) {
self.rssiCharacteristic = characteristic;
[self.beaconPeripheral readValueForCharacteristic:characteristic];
}
}
}
}
//獲取特征值value
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if ([characteristic.UUID isEqual:self.majorMinorCharacteristicUUID]) {
NSString *major = [NSString stringWithFormat:@"%hu",[self decodeMajor:characteristic.value]];
NSString *minor = [NSString stringWithFormat:@"%hu",[self decodeMinor:characteristic.value]];
}
else if ([characteristic.UUID isEqual:self.rssiCharacteristicUUID]) {
NSString *rssi = [NSString stringWithFormat:@"%hhd",[self decodeRSSI:characteristic.value]];
}
}
寫入操作
寫入major、minor
uint16_t major = (uint16_t)majorVlue;
uint16_t minor = (uint16_t)minorValue;
uint8_t majorMinor[4];
majorMinor[0] = ((major >> 8) & 0xFF);
majorMinor[1] = (major & 0xFF);
majorMinor[2] = ((minor >> 8) & 0xFF);
majorMinor[3] = (minor & 0xFF);
NSData *data = [NSData dataWithBytes:majorMinor length:4];
[self.beaconPeripheral writeValue:data forCharacteristic:self.majorMinorCharacteristic type:CBCharacteristicWriteWithResponse];
寫入rssi
int8_t rssi = (int8_t)rssiValue;
NSLog(@"rssi before save: %hhd",rssi);
NSData *data = [NSData dataWithBytes:&rssi length:1];
[self.beaconPeripheral writeValue:data forCharacteristic:self.rssiCharacteristic type:CBCharacteristicWriteWithResponse];
寫入回調(diào)
-(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
//如果寫入成功怜俐,再讀取一次身堡,進(jìn)入didUpdateValueForCharacteristic回調(diào),刷新數(shù)據(jù)
[self.beaconPeripheral readValueForCharacteristic:characteristic];
}