0

最初の質問が正しいことを願っています。間違いがあれば訂正してください。

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
4

1 に答える 1

1

私は最近、これらのフレームワークでいくつかのテストを行っています。IOBluetooth.h を見ると、Bluetooth Low Energy 専用の CoreBluetooth フレームワークがインポートされていることがわかります。IOBluetooth と IOBluetoothUI フレームワークを一緒に使用する必要があるように思えます。UI をユーザーに提示するための IOBluetoothUI フレームワークと、デバイスからクエリなどを実行するための IOBluetooth フレームワーク。IOBluetooth および IOBluetoothUI フレームワークは Bluetooth Classic にのみ使用されますが、便宜上 CoreBluetooth をインポートします。最初の例 (CBCentralManager など) は、CoreBluetooth フレームワークを使用しています。

これが、Apple TV しか表示されない理由です。Bluetooth LE のみに対応しており、Bluetooth Classic には対応していません。CoreBluetooth を使用している iOS デバイスが (BLE チップを搭載していても) 表示されない理由は、iOS が自身を Bluetooth Classic デバイスとしてのみアドバタイズするためです。CoreBluetooth フレームワークを使用してそれらを見つけたい場合は、最初に CoreBluetooth を使用してデバイスを BLE デバイスとしてアドバタイズするアプリを作成する必要があります。その方法については、CoreBluetooth プログラミング ガイド: 一般的な周辺機器の役割のタスクの実行を参照してください。

iOS デバイスは、Apple 製品以外のデバイスによって Bluetooth Classic デバイスとして確実に検出されます。カーラジオを考えてみてください。BLE に関する限り、よくわかりませんが、そうでない理由はわかりません。その場合、BLE デバイスとしてアドバタイズするアプリが必要です。iOS はそれ自体でそれを行いません。

于 2015-01-08T17:00:26.683 に答える