我希望我的第一个问题正确,如果我有任何错误,请纠正我。
我想在Mac中创建一个允许我连接到蓝牙设备的程序。我从基础开始,检测所有可用的蓝牙外围设备。
我做了两个测试代码,第一个用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;
@endBody:
#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;
@endBody:
#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发布于 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本身并不能做到这一点。
https://stackoverflow.com/questions/25224809
复制相似问题