7

私は簡単なアプリケーションを作成しようとしています:
1. 利用可能な BTLE デバイスをスキャンし、
2. ユーザーが表示できるようにそれらをドロップダウン メニューに配置します。

これまでのところ、IOBluetooth フレームワークをインポートし、NSPopUpButton (結果を表示したい場所) への 1 つの IBOutlet と、startScan および stopScan というボタン用の 2 つの IBActions を用意しました。

私はしばらくそれをしていて、助けを求める必要があります. この素晴らしいフォーラムの他の投稿を見てきましたが、私は Objective-C プログラミングに比較的慣れていないので、あなたの助けにとても感謝しています。

これが私のインターフェースです:

#import <Cocoa/Cocoa.h>
#import <IOBluetooth/IOBluetooth.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, CBCentralManagerDelegate, CBPeripheralDelegate> {
    CBCentralManager * central; // these properties are for the CBCentralManager
    CBPeripheral * peripheral;
    NSMutableDictionary * dictionary;
    NSNumber * number;
}

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSPopUpButton *deviceList;
@property NSMutableArray * list; // this is the array to fill up the deviceList NSPopUpButton

- (IBAction)startScan:(id)sender;
- (IBAction)stopScan:(id)sender;

@end

これが私の実装です:

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    CBCentralManager * central = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];
    NSLog(@"Central Manager's state is: %li", central.state);
}

- (IBAction)startScan:(id)sender {
    // start scanning button
    NSLog(@"Started scan. Discovering peripherals.");
    NSArray * list;
}

- (IBAction)stopScan:(id)sender {
    // stop scanning button
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    // I'm not sure how to make this work
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  NSLog(@"Central manager's state is updated to: %@", central);
}

- (void)retrievePeripherals:(NSArray *)peripheralUUIDs{
}

@end

私が言ったように、私はあなたの助けにとても感謝しています. 私はプログラミングが大好きですが、これには不満があります。あなたの 1 人がこれを見て、何が起こっているのかを正確に知っていると確信しています。

ありがとう、デビッド

4

2 に答える 2

10

始めるために何をする必要があるかについての簡単な概要:

1.) 何かをスキャンする前に、CentralManager を割り当て、そのデリゲートを採用し、デリゲート コールバックを取得するのを待つ必要があります。

-ヘッダーで宣言したセントラルを割り当てます

central = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

-次に、デリゲート コールバックを待ちます

- (void)centralManagerDidUpdateState:(CBCentralManager *)central 
{
   if(central.state == CBCentralManagerStatePoweredOn)
   {
    //okay your good to go and can now scan
   }
   else
   {
    //Unable to use CentralManager methods so print out the central.state and find out why
   }
}

2.) IBAction を使用してスキャンを制御する場合は、毎回状態も確認する必要があります。

- (IBAction)startScan:(id)sender
{
    if(central.state == CBCentralManagerStatePoweredOn)
    {
        //nil scans for all peripherals. Change to an array of service UUID's if you're looking for specific devices. Change NO to YES if you want to allow duplicates
        [central scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObjectsAndKeys:@NO, CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    }
}

3.) デリゲートとデバイスのスキャンを既にセットアップしているので、didDiscoverPeripheral コールバックを待ちます。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
//you've found a peripheral so add it to your table
}

4.) 発見CBPeripheralsしたものを配列内に保存するか、ペリフェラル名だけを保存します (ユースケースによって異なります)。これをテーブルのデータにフィードし、新しい周辺機器が検出されるたびに reloadData を呼び出します。現在近くにあるものを追跡したい場合は、先に進んで重複を許可し、見つかった時間/広告 rssi に応じて削除/追加できます。

注: セントラルは、ビュー コントローラー内でデリゲート メソッドを採用するシングルトン クラス内にセットアップするのが理想的です。(ただし、この方法で足を濡らすことができます)。

それが役に立てば幸い!

于 2013-09-09T19:30:05.980 に答える
1

Apple の BTLE サンプル アプリを確認してください: https://developer.apple.com/library/mac/samplecode/HeartRateMonitor/Introduction/Intro.html Mac でアプリケーションを起動し、LightBlueまたは iPhone/iPad/iPod のBLE ユーティリティアプリを使用して、心拍センサーをシミュレートします。

補足: Mac では WiFi と BLE が干渉する可能性があります。テスト中はイーサネット接続を使用して、この問題がテストを妨げないことを確認してください。さらに優れたソフトウェア ソリューションが William Henderson http://lists.apple.com/archives/bluetooth-dev/2013/Aug/msg00023.htmlから入手できます。

数人が回避策を確認したので、次のとおりです。

sudo defaults write /Library/Preferences/com.apple.airport.bt.plist bluetoothCoexMgmt Hybrid

その後、再起動する必要があります。私は試しましたが、これまでのところ、再起動せずにこれを有効にする方法を見つけることができませんでした.

于 2013-09-09T21:45:24.950 に答える