我的应用不会检测到外围设备。我使用浅蓝色来模拟蓝牙低能量外设,我的应用程序只是不会感觉到它。我甚至在两个设备上安装了浅蓝色,以确保它正确地生成外设信号。有什么建议?我的标签正在更新,NSLog显示扫描正在开始。
提前致谢。
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *navDestination;
@end
#import "ViewController.h"
@implementation ViewController
- (IBAction)connect:(id)sender {
}
- (IBAction)navDestination:(id)sender {
NSString *destinationText = self.navDestination.text;
}
- (void)viewDidLoad {
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface BlueToothViewController : UIViewController
@property (strong, nonatomic) CBCentralManager *centralManager;
@property (strong, nonatomic) CBPeripheral *discoveredPerepheral;
@property (strong, nonatomic) NSMutableData *data;
@property (strong, nonatomic) IBOutlet UITextView *textview;
@property (weak, nonatomic) IBOutlet UILabel *charLabel;
@property (weak, nonatomic) IBOutlet UILabel *isConnected;
@property (weak, nonatomic) IBOutlet UILabel *myPeripherals;
@property (weak, nonatomic) IBOutlet UILabel *aLabel;
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral: (CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
-(void)cleanup;
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
@end
@interface BlueToothViewController ()
@end
@implementation BlueToothViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
_centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];
_data = [[NSMutableData alloc]init];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[_centralManager stopScan];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
//you should test all scenarios
if (central.state == CBCentralManagerStateUnknown) {
self.aLabel.text = @"I dont do anything because my state is unknown.";
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
//scan for devices
[_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
NSLog(@"Scanning Started");
}
if (central.state == CBCentralManagerStateResetting) {
self.aLabel.text = @"I dont do anything because my state is resetting.";
return;
}
if (central.state == CBCentralManagerStateUnsupported) {
self.aLabel.text = @"I dont do anything because my state is unsupported.";
return;
}
if (central.state == CBCentralManagerStateUnauthorized) {
self.aLabel.text = @"I dont do anything because my state is unauthorized.";
return;
}
if (central.state == CBCentralManagerStatePoweredOff) {
self.aLabel.text = @"I dont do anything because my state is powered off.";
return;
}
}
- (void)centralManger:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
self.myPeripherals.text = [NSString stringWithFormat:@"%@%@",peripheral.name, RSSI];
if (_discoveredPerepheral != peripheral) {
//save a copy of the peripheral
_discoveredPerepheral = peripheral;
//and connect
NSLog(@"Connecting to peripheral %@", peripheral);
[_centralManager connectPeripheral:peripheral options:nil];
self.aLabel.text = [NSString stringWithFormat:@"%@", peripheral];
}
}
-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
-(void)cleanup {
//see if we are subscribed to a characteristic on the peripheral
if (_discoveredPerepheral.services != nil) {
for (CBService *service in _discoveredPerepheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"508EFF8E-F541-57EF-BD82-B0B4EC504CA9"]]) {
if (characteristic.isNotifying) {
[_discoveredPerepheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[_centralManager cancelPeripheralConnection:_discoveredPerepheral];
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
self.isConnected.text = [NSString stringWithFormat:@"Connected"];
[_data setLength:0];
peripheral.delegate = self;
[peripheral discoverServices:nil];
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) { [self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
//discover other characteristics
}
-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) { [self cleanup];
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) { NSLog(@"Error");
return;
}
NSString *stringFromData = [[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding];
self.charLabel.text = [NSString stringWithFormat:@"%@", stringFromData];
//Have we got everything we need?
if ([stringFromData isEqualToString:@"EOM"]) {
[_textview setText:[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding]];
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
}
}
-(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if ([characteristic.UUID isEqual:nil]) {
return;
}
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
}
else {
[_centralManager cancelPeripheralConnection:peripheral];
}
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
_discoveredPerepheral = nil;
self.isConnected.text = [NSString stringWithFormat:@"Connecting..."];
[_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES}];
}
@end
答案 0 :(得分:1)
看起来您没有宣布实施CBCentralManagerDelegate协议。更改BlueToothViewController界面以添加CBCentralManagerDelegate。
@interface BlueToothViewController () <CBCentralManagerDelegate>