저는 Xcode와 Objective C를 처음 사용하지만 빠른 학습이 가능합니다. 여러 BLE 장치에서 데이터를 수집하는 Bluetooth LE 응용 프로그램을 쓰고 있습니다. 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
도움 사람을 찾고 있습니다 .... 나는 무엇을 놓치고 있습니까 ... 미리 감사드립니다 ....
,210UPDATE
그래서 나는
#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];
}
이 제공하는 모든 CoreBluetooth 설정 및 검색
Discovery.h을하는 두 번째 클래스가 다음 중 하나를 실행하고 있지 않습니다.
/어떻게'SensorDev'가 생성되고 시작 되었습니까? –
안녕하세요, 확실하지 않습니다. 대답은 그것이 아니라고 생각하며 코드와 지식이 실패하는 곳입니다 ... – duck1970
실제로 전화를 시작합니까? –