最初の質問が正しいことを願っています。間違いがあれば訂正してください。
Bluetooth デバイスに接続できるようにするプログラムを Mac OS X で作成したいと考えています。基本から始めて、利用可能なすべての Bluetooth 周辺機器を検出します。
2 つのテスト コードを作成しました。1 つ目は IOBluetooth ライブラリを使用し、2 つ目はユーザー インターフェイスを作成するための Apple の機能を備えた IOBluetoothUI を使用します。
1 つ目はデバイス (AppleTV) をほとんど検出できず、2 つ目は iPhone 5、iPad 2、および HC-05 を検出しましたが、AppleTV は検出しませんでした。最初のコードで、少なくとも HC-05 も検出できるはずです。
違いはありますか (UI 機能を備えたライブラリを除く)、または何か間違っていますか? いくつかの質問を確認しましたが、それについて何も見つかりませんでした。
iPhone は他の Apple 製品でしか検出できないと読みましたが、本当ですか?
よろしくお願いします。
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