2015-01-06 18 views
0

我是Xcode和Objective C的新手,但学习速度很快。我正在编写一个蓝牙LE应用程序来收集来自多个BLE设备的数据。对CoreBluetooth感到满意,能够获得我想要的功能并收集数据。类未运行

但是,我在AppDelegate中完成了所有这些工作,现在想要将不同的代码段划分为整齐的类。

代码编译没问题,但除AppDelegate之外没有其他任何代码运行。类的

示例 - SensorDev.m:类

#import <Foundation/Foundation.h> 
#import <CoreBluetooth/CoreBluetooth.h> 

@class SensorDev; 
@protocol SensorDevDelegate<NSObject> 
- (void) sensorDevDidChangeStatus:(SensorDev*)dev; 
@end 

@interface SensorDev : NSObject 

@property (nonatomic, assign, readonly) id<SensorDevDelegate> delegate; 
@property (nonatomic, readonly) CBPeripheral *peripheral; 

- (id)initWithPeripheral:(CBPeripheral *)peripheral controller:(id<SensorDevDelegate>)controller; 
- (void)start; 

@end 

示例 - SensorDev.h

#import "SensorDev.h" 

NSString *SR1Device9DOFServiceUUIDString =  @"346D0000"; 
NSString *SR1Device9DOFCharacteristicUUIDString = @"346D0001-12A9-11CF-1279-81F2B7A91332"; 

@interface SensorDev() <CBPeripheralDelegate> { 
    CBService *_temperatureService; 
    CBCharacteristic *_temperatureCharacteristic; 
} 
@end 

@implementation SensorDev 

#pragma mark - Setup 

- (id)initWithPeripheral:(CBPeripheral *)peripheral controller:(id<SensorDevDelegate>)controller 
{ 
    self = [super init]; 
    if (self) { 
     _peripheral = peripheral; 
     _peripheral.delegate = self; 
     _delegate = controller; 
    } 
    return self; 
} 

#pragma mark - Start 

// ------------------------------------------------------------------------------- 
// Startup 
// ------------------------------------------------------------------------------- 

- (void)start 
{ 
    NSLog(@"- (void) start"); //--Debug 
    CBUUID *serviceUUID = [CBUUID UUIDWithString:SR1Device9DOFServiceUUIDString]; 
    NSArray *serviceArray = [NSArray arrayWithObjects:serviceUUID, nil]; 
    [_peripheral discoverServices:serviceArray]; 
} 


@end 

我没有得到日志中的调试行:

NSLog(@"- (void) start"); //--Debug 

寻找帮助球员....我错过了什么......在此先感谢....

UPDATE

所以我必须做所有CoreBluetooth设置和发现

Discovery.h

#import <Foundation/Foundation.h> 
#import <CoreBluetooth/CoreBluetooth.h> 
#import "SensorDev.h" 

// ------------------------------------------------------------------------------- 
//UI Setup/Protocols 
// ------------------------------------------------------------------------------- 
@protocol DiscoveryDelegate <NSObject> 
- (void) discoveryDidRefresh; 
- (void) discoveryStatePoweredOff; 
@end 

@interface Discovery : NSObject 

+(Discovery*) sharedInstance; 

@property (nonatomic, assign) id<DiscoveryDelegate> discoveryDelegate; 
@property (nonatomic, assign) id<SensorTagDelegate> peripheralDelegate; 

// ------------------------------------------------------------------------------- 
// Actions 
// ------------------------------------------------------------------------------- 
- (void) startScanningForUUIDString:(NSString *)uuidString; 
- (void) stopScanning; 
- (void) connectPeripheral:(CBPeripheral*)peripheral; 
- (void) disconnectPeripheral:(CBPeripheral*)peripheral; 

// ------------------------------------------------------------------------------- 
// Access to the devices 
// ------------------------------------------------------------------------------- 
@property (readonly, nonatomic) NSMutableArray *foundPeripherals; 
@property (retain, nonatomic) NSMutableArray *connectedPeripherals; 

@end 

Discover.m(摘录)

#import "Discovery.h" 

extern NSString *SR1Device9DOFServiceUUIDString; //346D0000 
extern NSString *SR1Device9DOFCharacteristicUUIDString; //346D0001-12A9-11CF-1279-81F2B7A91332 

@interface Discovery() <CBCentralManagerDelegate, CBPeripheralDelegate> { 
    CBCentralManager *_centralManager; 
    BOOL _pendingInit; 
} 
@end 

@implementation Discovery 

#pragma mark - Setup 

+ (Discovery*) sharedInstance 
{ 
    static Discovery *this = nil; 

    if (!this) 
     this = [[Discovery alloc] init]; 

    return this; 
} 

- (id) init 
{ 
    self = [super init]; 
    if (self) { 
     _pendingInit = YES; 
     _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil]; 
     _foundPeripherals = [[NSMutableArray alloc] init]; 
     _connectedPeripherals = [[NSMutableArray alloc] init]; 
    } 
    return self; 
} 

#pragma mark - CoreBluetooth Services 

// ------------------------------------------------------------------------------- 
// CoreBluetooth Start/Stop Scanning 
// ------------------------------------------------------------------------------- 

- (void)startScanningForUUIDString:(NSString *)uuidString 
{ 
    NSLog(@"- (void) startScanningForUUIDString"); //--Debug 
    [_centralManager scanForPeripheralsWithServices: 
    [NSArray arrayWithObjects:SR1Device9DOFServiceUUIDString, nil] options:nil]; 
} 

- (void)stopScanning 
{ 
    NSLog(@"- (void) stopScanning"); //--Debug 
} 

// ------------------------------------------------------------------------------- 
// CoreBluetooth Connect/Disconnect 
// ------------------------------------------------------------------------------- 

- (void) connectPeripheral:(CBPeripheral*)peripheral 
{ 
    NSLog(@"- (void) connectPeripheral"); //--Debug 
    if (peripheral.state == CBPeripheralStateDisconnected) { 
     [_centralManager connectPeripheral:peripheral options:nil]; 
    } 
} 


- (void) disconnectPeripheral:(CBPeripheral*)peripheral 
{ 
    NSLog(@"- (void) disconnectPeripheral"); //--Debug 
    [_centralManager cancelPeripheralConnection:peripheral]; 
} 

- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { 

    NSLog(@"- (void) didConnectPeripheral"); //--Debug 

    SensorDev *tag = nil; 
    // Create a service instance. 
    tag = [[SensorDev alloc] initWithPeripheral:peripheral controller:_peripheralDelegate]; 
    [tag start]; 

    if (![_connectedPeripherals containsObject:tag]) 
     [_connectedPeripherals addObject:tag]; 

    [_peripheralDelegate sensorTagDidChangeStatus:tag]; 
    [_discoveryDelegate discoveryDidRefresh]; 
} 

这第二类也没有运行...

+0

何处/如何创建并启动SensorDev? –

+0

嗨,我不确定,我认为答案是不是,那就是代码和我的知识失败的地方... – duck1970

+0

你真的开始了吗? –

回答

0

The因为没有任何东西叫他们,所以班级没有跑的原因。相关的视图控制器类不调用类:

BLEController.h

#import <Foundation/Foundation.h> 

@interface BLEController : NSObject { 
enter code here 
NSMutableArray *_periphralItems; 

} 

@end 

BLEController.m

#import "SensorDev.h" 
#import "Discovery.h" 
#import "BLEController.h" 

@interface BLEController() <DiscoveryDelegate, SensorTagDelegate> 
{ 
    BOOL _selfSelection; 
} 

@end 

@implementation BLEController 

- (void)awakeFromNib 
{ 
    [[Discovery sharedInstance] setDiscoveryDelegate:self]; 
    [[Discovery sharedInstance] setPeripheralDelegate:self]; 
    _periphralItems = [NSMutableArray new]; 
} 

- (void) discoveryDidRefresh 
{ 
} 

- (void) discoveryStatePoweredOff 
{ 
} 

- (void)sensorTagDidChangeStatus:(SensorTag *)tag 
{ 
    if(tag.peripheral.state == CBPeripheralStateConnected) { 
     //Do something 
    } 
} 

@end 

的awakeFromNib让我打电话给其他类....