我正在尝试编写一个简单的应用程序:
1. 扫描可用的 BTLE 设备,以及
2. 将它们放在下拉菜单中供用户查看。
到目前为止,我已经导入了 IOBluetooth 框架,并且我有一个 IBOutlet 到一个 NSPopUpButton(我想在其中显示结果)和两个 IBAction 用于调用 startScan 和 stopScan 的按钮。
我已经有一段时间了,我需要寻求帮助。我看过这个精彩论坛上的其他帖子,但我对 Objective-C 编程比较陌生,非常感谢您的帮助。
这是我的界面:
#import <Cocoa/Cocoa.h>
#import <IOBluetooth/IOBluetooth.h>
@interface AppDelegate : NSObject <NSApplicationDelegate, CBCentralManagerDelegate, CBPeripheralDelegate> {
CBCentralManager * central; // these properties are for the CBCentralManager
CBPeripheral * peripheral;
NSMutableDictionary * dictionary;
NSNumber * number;
}
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSPopUpButton *deviceList;
@property NSMutableArray * list; // this is the array to fill up the deviceList NSPopUpButton
- (IBAction)startScan:(id)sender;
- (IBAction)stopScan:(id)sender;
@end
这是我的实现:
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
CBCentralManager * central = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
NSLog(@"Central Manager's state is: %li", central.state);
}
- (IBAction)startScan:(id)sender {
// start scanning button
NSLog(@"Started scan. Discovering peripherals.");
NSArray * list;
}
- (IBAction)stopScan:(id)sender {
// stop scanning button
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// I'm not sure how to make this work
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSLog(@"Central manager's state is updated to: %@", central);
}
- (void)retrievePeripherals:(NSArray *)peripheralUUIDs{
}
@end
就像我说的,我会非常感谢你的帮助。我喜欢编程,但我对此感到沮丧,而且我相信你们中的一个人会看一看这个并确切地知道发生了什么。
谢谢,大卫