use*_*981 7 macos cocoa bluetooth objective-c core-bluetooth
我正在尝试编写一个简单的应用程序:
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
Run Code Online (Sandbox Code Playgroud)
这是我的实现:
@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
Run Code Online (Sandbox Code Playgroud)
就像我说的,我非常感谢你的帮助.我喜欢编程,但我对此感到沮丧,我相信你们中的一个人会看一眼并确切知道发生了什么.
谢谢,大卫
Tom*_*voy 10
好的快速概述您需要做什么才能开始:
1.)在您可以扫描任何内容之前,您需要分配CentralManager,采用其委托,并等待获取委托回调:
- 分配您在标题中声明的中心
central = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
Run Code Online (Sandbox Code Playgroud)
- 然后等待委托回调
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if(central.state == CBCentralManagerStatePoweredOn)
{
//okay your good to go and can now scan
}
else
{
//Unable to use CentralManager methods so print out the central.state and find out why
}
}
Run Code Online (Sandbox Code Playgroud)
2.)如果您想使用IBAction控制扫描,您还需要每次检查状态.
- (IBAction)startScan:(id)sender
{
if(central.state == CBCentralManagerStatePoweredOn)
{
//nil scans for all peripherals. Change to an array of service UUID's if you're looking for specific devices. Change NO to YES if you want to allow duplicates
[central scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObjectsAndKeys:@NO, CBCentralManagerScanOptionAllowDuplicatesKey, nil];
}
}
Run Code Online (Sandbox Code Playgroud)
3.)既然你已经设置了代理和扫描设备,只需等待didDiscoverPeripheral回调:
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
//you've found a peripheral so add it to your table
}
Run Code Online (Sandbox Code Playgroud)
4.)将您的发现存储CBPeripherals在数组中或仅存储外围设备名称(取决于您的使用案例).将其输入到表的数据中,并在每个新的外围设备发现上调用reloadData.您可以继续并允许重复,如果您想要跟踪当前附近的那些,您可以根据找到的时间/广告rssi删除/添加.
注意:理想情况下,Central将在Singleton类中设置,您可以在视图控制器中采用它的委托方法.(但你可以这样弄湿你的脚).
希望能帮助到你!
| 归档时间: |
|
| 查看次数: |
4818 次 |
| 最近记录: |