Objective c 未运行的类
我不熟悉Xcode和Objective C,但学习速度很快。我正在编写一个蓝牙LE应用程序来收集来自多个BLE设备的数据。使用CoreBluetooth很开心,我能够获得我想要的功能并收集数据 然而,我在AppDelegate中完成了这一切,现在我想将代码的不同部分划分成整洁的类 代码编译正常,但除了AppDelegate之外,没有任何东西运行 类的示例-SensorDev.m:Objective c 未运行的类,objective-c,bluetooth-lowenergy,Objective C,Bluetooth Lowenergy,我不熟悉Xcode和Objective C,但学习速度很快。我正在编写一个蓝牙LE应用程序来收集来自多个BLE设备的数据。使用CoreBluetooth很开心,我能够获得我想要的功能并收集数据 然而,我在AppDelegate中完成了这一切,现在我想将代码的不同部分划分成整洁的类 代码编译正常,但除了AppDelegate之外,没有任何东西运行 类的示例-SensorDev.m: #import <Foundation/Foundation.h> #import <CoreBl
#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
正在寻找帮助…我错过了什么…提前谢谢
更新
因此,我有第二个类,负责所有的核心蓝牙设置和发现
发现
#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
#导入
#进口
#导入“SensorDev.h”
// -------------------------------------------------------------------------------
//用户界面设置/协议
// -------------------------------------------------------------------------------
@协议发现LEGATE
-(无效)发现IDREFRESH;
-(无效)发现STATEPOWEREDOFF;
@结束
@接口发现:NSObject
+(发现*)共享状态;
@属性(非原子,赋值)id discoveryDelegate;
@属性(非原子,赋值)id外围设备委托;
// -------------------------------------------------------------------------------
//行动
// -------------------------------------------------------------------------------
-(void)开始扫描UUIString:(NSString*)UUIString;
-(d)停止扫描;
-(void)connectPeripheral:(CBPeripheral*)peripheral;
-(无效)断开外围设备:(CBPeripheral*)外围设备;
// -------------------------------------------------------------------------------
//访问设备
// -------------------------------------------------------------------------------
@属性(只读、非原子)NSMutableArray*foundPeripherals;
@属性(保留,非原子)NSMutableArray*连接的外围设备;
@结束
Discover.m(摘录)
#导入“Discovery.h”
外部NSString*SR1Device9DofserviceUUIString//346D0000
外部NSString*SR1设备9DOFCharacteristicUUIString//346D0001-12A9-11CF-1279-81F2B7A91332
@接口发现(){
CBCentralManager*_centralManager;
布尔彭丁尼特;
}
@结束
@实现发现
#pragma标记-设置
+(发现*)共享状态
{
静态发现*this=nil;
如果(!这个)
this=[[Discovery alloc]init];
归还这个;
}
-(id)init
{
self=[super init];
如果(自我){
_pendingInit=是;
_centralManager=[[CBCentralManager alloc]initWithDelegate:自队列:nil选项:nil];
_foundPeripherals=[[NSMutableArray alloc]init];
_connectedPeripherals=[[NSMutableArray alloc]init];
}
回归自我;
}
#pragma标记-核心蓝牙服务
// -------------------------------------------------------------------------------
//核心蓝牙启动/停止扫描
// -------------------------------------------------------------------------------
-(无效)开始扫描UUIString:(NSString*)UUIString
{
NSLog(@“-(void)StartScanningForuUIString”);/--Debug
[_centralManager扫描外围设备服务:
[NSArray arrayWithObjects:SR1Device9DofServiceUUIString,无]选项:无];
}
-(无效)停止扫描
{
NSLog(@“-(无效)停止扫描”);/--调试
}
// -------------------------------------------------------------------------------
//核心蓝牙连接/断开
// -------------------------------------------------------------------------------
-(无效)连接外围设备:(CBPeripheral*)外围设备
{
NSLog(@“-(void)connectPeripheral”);/--Debug
if(peripheral.state==CBPeripheralStateDisconnected){
[_CentralManagerConnectPeripheral:外围设备选项:无];
}
}
-(无效)断开外围设备:(CBPeripheral*)外围设备
{
NSLog(@“-(无效)断开外围设备”);/--调试
[_centralManager取消外围设备连接:外围设备];
}
-(无效)中央管理器:(CBCentralManager*)中央数据连接外围设备:(CBPeripheral*)外围设备{
NSLog(@“-(void)didConnectPeripheral”);/--Debug
SensorDev*标签=零;
//创建一个服务实例。
tag=[[SensorDev alloc]initWithPeripheral:peripheral controller:_peripheraldegate];
[标签开始];
如果(![\u连接的外围设备包含对象:标记])
[_connectedperipheralsaddobject:tag];
[_PeripheralDelegateSensorTagDidChangeStatus:tag];
[_discoverydelegatediscoverydirefresh];
}
这也没有运行…类之所以没有运行,是因为没有调用它们。与视图控制器关联的类未调用该类: BLEController.h
#import <Foundation/Foundation.h>
@interface BLEController : NSObject {
enter code here
NSMutableArray *_periphralItems;
}
@end
#导入
@接口控制器:NSObject{
在这里输入代码
NSMutableArray*_外围设备;
}
@结束
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
#导入“SensorDev.h”
#导入“Discovery.h”
#导入“BLEController.h”
@接口控制器()
{
BOOL_自选;
}
@结束
@可编程控制器的实现
-(无效)从NIB中唤醒
{
[[Discovery sharedInstance]setDiscoveryDelegate:self];
[[Discovery sharedInstance]设置外围设备代理:self];
_periphralItems=[NSMutableArray new];
}
-(无效)发现IDREFRESH
{
}
-(无效)DiscoveryStatePower关闭
{
}
-(无效)sensorTagDidChangeStatus:(SensorTag*)标记
{
如果(tag.peripheral.state==CBPeripheralStateConnected){
//做点什么
}
}
@结束
awakeFromNib允许我调用其他类…SensorDev在何处/如何创建和启动?嗨,我不确定,我想答案是不是,这就是代码和我的知识失败的地方…你真的调用start吗?添加了额外的代码,call for start在另一个也没有运行的类中。因此,为了让它工作,是否有类似于
[[Discovery sharedInstance]centralManager:mgr didConnectPeripheral:p]
的调用?(可能与您的问题无关,但您的sharedInstance
方法不是线程安全的。)
#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];
}
#import <Foundation/Foundation.h>
@interface BLEController : NSObject {
enter code here
NSMutableArray *_periphralItems;
}
@end
#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