首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOBluetooth和IOBluetoothUI库检测不同的外设,它们之间有什么区别吗?

IOBluetooth和IOBluetoothUI库检测不同的外设,它们之间有什么区别吗?
EN

Stack Overflow用户
提问于 2014-08-10 01:51:01
回答 1查看 1.9K关注 0票数 0

我希望我的第一个问题正确,如果我有任何错误,请纠正我。

我想在Mac中创建一个允许我连接到蓝牙设备的程序。我从基础开始,检测所有可用的蓝牙外围设备。

我做了两个测试代码,第一个用IOBluetooth库,第二个用苹果做用户界面的IOBluetoothUI。

对于第一个设备( AppleTV ),我只能检测到一个设备( iPhone 5)、一个iPad 2和一个HC-05,而不是AppleTV。使用第一段代码,我还应该能够至少检测到HC-05。

有什么不同(除了一个是带有UI工具的库),还是我做错了什么?我复习了几个问题,但没有发现任何问题。

我读到iPhone只能被其他苹果产品检测到,是真的吗?

谢谢你的进阶。

使用IOBluetooth的示例

头:

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>
#import <IOBluetooth/IOBluetooth.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, CBCentralManagerDelegate, CBPeripheralDelegate>
{
    CBCentralManager *myCentralManager;
    CBPeripheral *myPeripheral;
    NSMutableArray *Peripherals;
}
@property (assign) IBOutlet NSWindow *window;

@end

Body:

代码语言:javascript
复制
#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    //We initialize the Central Manager
    myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"Discovered %@ %@", peripheral, advertisementData);
    if (!Peripherals){
        Peripherals =[[NSMutableArray alloc] initWithObjects:peripheral, nil];
    }else{
        [Peripherals addObject:peripheral];
    }

    NSLog(@"List of devices: %@",Peripherals);

}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    //CBCentralManagerStatePoweredOn = 5;
    NSLog(@"Bluetooth state %ld", myCentralManager.state);
    if (myCentralManager.state == CBCentralManagerStatePoweredOn){
        NSLog(@"Scanning");
        [myCentralManager scanForPeripheralsWithServices:nil options:nil];
    }
}

@end

使用IOBluetoothUI的示例

头:

代码语言:javascript
复制
#import <Cocoa/Cocoa.h>
#import <IOBluetoothUI/IOBluetoothUI.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>{
    IBOutlet NSButton *button;
}

@property (assign) IBOutlet NSWindow *window;

- (IBAction)showBrowser:(id)sender;

@end

Body:

代码语言:javascript
复制
#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

- (IBAction)showBrowser:(id)sender{
   // Creating and initializing the window
   IOBluetoothServiceBrowserController *browser = [IOBluetoothServiceBrowserController serviceBrowserController:0];
   // launching the window
   [browser runModal];

}
@end
EN

回答 1

Stack Overflow用户

发布于 2015-01-08 17:00:26

最近,我对这些框架进行了一些测试。如果您查看IOBluegin.h,您可以看到它导入了CoreBluetooth框架,该框架仅用于蓝牙低能。在我看来,IOBluetooth和IOBluetoothUI框架应该一起使用。IOBluetoothUI框架将UI呈现给用户,IOBluetooth框架用于从设备中执行查询等等。IOBluetooth和IOBluetoothUI框架仅用于蓝牙经典,而导入CoreBluetooth是为了方便。您的第一个示例(使用CBCentralManager等)是使用CoreBluetooth框架。

这就是你只能看到苹果电视的原因。它只会蓝牙,而不是蓝牙经典。您无法看到您的iOS设备使用CoreBluetooth (即使它们有BLE芯片)的原因是,iOS只是宣传自己为蓝牙经典设备。如果您想要使用CoreBluetooth框架找到它们,您首先必须使用CoreBluetooth编写一个应用程序,该应用程序将该设备广告为BLE设备。请查看CoreBluetooth编程指南:执行公共外围角色任务以了解如何做到这一点。

iOS设备肯定可以被苹果产品以外的设备发现为蓝牙经典设备。想想汽车收音机。就BLE而言,我不太确定,但我看不出为什么不。如果是这样的话,它需要一个应用程序来宣传它是一个BLE设备。iOS本身并不能做到这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25224809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档