Xcode 7.2 iOS中心端。绝对厌倦了查看信息并找不到为什么我无法从蓝牙中读取特征的答案。
我做了一个日志,反馈错误给我,所以我知道我得到的错误来自`错误域= CBATTErrorDomain Code = 2"不允许读取"这对我来说没有任何意义,因为代码是核心蓝牙API和nrf8001芯片的示例,它演示了如何将该产品与工作应用程序一起使用。
这个特征显然是可读的,因为它可以通过应用程序" Bluefruit LE。"
更多详情:
以下完整代码供参考,但搜索特征的第一个对象以供快速参考
//
// PotentiometerView.m
// Impulse
//
// Created by Rory Donovan on 12/20/15.
// Copyright © 2015 Impulse. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PotentiometerView.h"
@interface PotentiometerView ()
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *discoveredPeripheral;
@property BOOL bluetoothOn;
@property (weak, nonatomic) IBOutlet UITextView *outputTextView;
@property (weak, nonatomic) IBOutlet UIButton *connectButton;
@end
@implementation PotentiometerView
- (void)viewDidLoad {
[super viewDidLoad];
self.bluetoothOn = NO;
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state != CBCentralManagerStatePoweredOn)
{
self.bluetoothOn = NO;
}
else
{
self.bluetoothOn = YES;
}
}
-(void)tLog:(NSString *)msg
{
self.outputTextView.text = [@"\r\n\r\n" stringByAppendingString:self.outputTextView.text];
self.outputTextView.text = [msg stringByAppendingString:self.outputTextView.text];
}
#pragma mark -
#pragma mark Discovery
/****************************************************************************/
/* Discovery */
/****************************************************************************/
- (IBAction)ConnectButtonView:(id)sender
{
if(!self.bluetoothOn)
{
[self tLog:@"Bluetooth is OFF"];
return;
}
NSArray *serv_array = [NSArray arrayWithObject:[CBUUID UUIDWithString:SERVICE_UUID]];
[self.centralManager scanForPeripheralsWithServices:serv_array options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @NO}];
}
#pragma mark -
#pragma mark Connection/Disconnection
/****************************************************************************/
/* Connection/Disconnection */
/****************************************************************************/
-(void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(nonnull CBPeripheral *)peripheral advertisementData: (nonnull NSDictionary<NSString *,id> *)advertisementData RSSI:(nonnull NSNumber *)RSSI
{
self.discoveredPeripheral = peripheral;
[self.centralManager connectPeripheral:peripheral options:nil];
[self tLog:@"Discovered Peripheral"];
}
-(void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral
{
NSArray *serviceArray = [NSArray arrayWithObject:[CBUUID UUIDWithString:SERVICE_UUID]];
peripheral.delegate = self;
[peripheral discoverServices:serviceArray];
[self tLog:@"Connected Peripheral"];
}
-(void)centralManager:(CBCentralManager *)central
didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
[self tLog:@"Failed to connect"];
}
#pragma mark -
#pragma mark Services
/****************************************************************************/
/* Services */
/****************************************************************************/
-(void)peripheral:(CBPeripheral *)peripheral
didDiscoverServices:(NSError *)error
{
if(error)
{
[self tLog:[error description]];
return;
}
for(CBService *service in peripheral.services)
{
NSArray *characteristicArray = [NSArray arrayWithObject:[CBUUID UUIDWithString:TX_UUID]];
[self tLog:[NSString stringWithFormat:@"Discovered service: %@", [service description]]];
[peripheral discoverCharacteristics:characteristicArray forService:service];
}
}
#pragma mark -
#pragma mark Characteristics
/****************************************************************************/
/* Characteristics */
/****************************************************************************/
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if(error)
{
[self tLog:[error description]];
return;
}
for(CBCharacteristic *characteristic in service.characteristics)
{
[self tLog:[NSString stringWithFormat:@"Discovered characteristic: %@", [characteristic description]]];
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:TX_UUID]])
{
[peripheral readValueForCharacteristic:characteristic];
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if(error)
{
[self tLog:[error localizedDescription]];
[self tLog:@"Did not update notification state for characteristic"];
return;
}
}
-(void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error
{
if(error)
{
[self tLog:[error description]];
[self tLog:@"Error updating value for characteristic"];
return;
}
if (characteristic.isNotifying)
{
[self tLog:@"isNotifying = YES!!!"];
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
[self tLog:[NSString stringWithFormat:@"Characteristic updated: %@", stringFromData]];
} else {
[self.centralManager cancelPeripheralConnection:peripheral];
}
}
@end
*更新*
在setNotify被赋值为yes并重新定位读取值之后,通过读取特性来修复代码。虽然特性描述给出了属性,但我将以下代码添加为一种错误检查器。
for(CBCharacteristic *characteristic in service.characteristics)
{
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:RX_UUID]])
{
if (characteristic.properties==CBCharacteristicPropertyRead)
{
[peripheral readValueForCharacteristic:characteristic];
[self tLog:@"Properties is Read"];
}
else if(characteristic.properties==CBCharacteristicPropertyWrite)
{
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
[self tLog:@"Properties is Write"];
}
else if(characteristic.properties==CBCharacteristicPropertyNotify)
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
NSLog(@"the property :%lu",(unsigned long)characteristic.properties );
}
}