0

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

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

我做了两个测试代码,第一个使用 IOBluetooth 库,第二个使用 IOBluetoothUI 和苹果的工具来制作用户界面。

第一个我几乎检测不到设备(AppleTV),第二个检测到 iPhone 5、iPad 2 和 HC-05,但没有检测到 AppleTV。使用第一个代码,我也应该至少能够检测到 HC-05。

有什么区别(除了一个是带有 UI 设施的图书馆)还是我做错了什么?我已经审查了几个问题,但我没有找到任何关于它的信息。

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

提前谢谢你。


IOBluetooth 示例

标题:

#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

身体:

#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 示例

标题:

#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

身体:

#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
4

1 回答 1

1

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

这就是您只能看到 Apple TV 的原因。它仅支持蓝牙 LE,而不支持蓝牙经典。您看不到使用 CoreBluetooth 的 iOS 设备(即使它们具有 BLE 芯片)的原因是因为 iOS 仅将自己宣传为蓝牙经典设备。如果您想使用 CoreBluetooth 框架找到它们,您首先必须使用 CoreBluetooth 编写一个应用程序,将设备宣传为 BLE 设备。请参阅CoreBluetooth Programming Guide: Performing Common Peripheral Role Tasks了解如何做到这一点。

iOS 设备绝对可以被 Apple 产品以外的设备发现为蓝牙经典设备。想想汽车收音机。就BLE而言,我不太确定,但我不明白为什么不这样做。如果是这样,它需要一个应用程序将其宣传为 BLE 设备。iOS 不会自行执行此操作。

于 2015-01-08T17:00:26.683 回答