51

現在、Bluetooth デバイスを使用できる iPhone アプリ (Xcode 4.3.1、IOS 5) を作成しています。このアプリケーションの主な目的は屋内ナビゲーションです (建物内の GPS はあまり正確ではありません)。

ここに表示される唯一の解決策 (アプリを AppStore に保持するため) は、利用可能な Bluetooth デバイスをスキャンすることです!

CoreBluetooth フレームワークを使用しようとしましたが、使用可能なデバイスのリストが表示されません! これらの機能を正しく使用していない可能性があります

#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

@interface AboutBhyperView : UIViewController <CBPeripheralDelegate, CBCentralManagerDelegate>
{
    CBCentralManager *mgr;
}
@property (readwrite, nonatomic) CBCentralManager *mgr;
@end



- (void)viewDidLoad
{
    [super viewDidLoad];

    mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}


- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {


    NSLog([NSString stringWithFormat:@"%@",[advertisementData description]]);
}

-(void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals{
    NSLog(@"This is it!");
}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ 
    NSString *messtoshow;

    switch (central.state) {
        case CBCentralManagerStateUnknown:
        {
            messtoshow=[NSString stringWithFormat:@"State unknown, update imminent."];
            break;
        }
        case CBCentralManagerStateResetting:
        {
            messtoshow=[NSString stringWithFormat:@"The connection with the system service was momentarily lost, update imminent."];
            break;
        }
        case CBCentralManagerStateUnsupported:
        {
            messtoshow=[NSString stringWithFormat:@"The platform doesn't support Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStateUnauthorized:
        {
            messtoshow=[NSString stringWithFormat:@"The app is not authorized to use Bluetooth Low Energy"];
            break;
        }
        case CBCentralManagerStatePoweredOff:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered off."];
            break;
        }
        case CBCentralManagerStatePoweredOn:
        {
            messtoshow=[NSString stringWithFormat:@"Bluetooth is currently powered on and available to use."];
            [mgr scanForPeripheralsWithServices:nil options:nil];
            //[mgr retrieveConnectedPeripherals];

//--- it works, I Do get in this area!

            break;
        }   

    }
    NSLog(messtoshow); 
} 

この行についてよくわかりません。正しいパラメータを渡す方法を教えてください。

[mgr scanForPeripheralsWithServices:nil options:nil];

私はアップルのリファレンスを調べました..そして私はまだそのCBUUIDが何であるかを理解していません??? すべてのデバイスには Bluetooth ID がありますか? どこで見つけることができますか?

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;

パラメーター serviceUUIDs - アプリが対象とする CBUUID の配列。 options - スキャンをカスタマイズするための辞書。CBCentralManagerScanOptionAllowDuplicatesKey を参照してください。

IOS で Bluetooth を使用する他の方法はありますか? つまり、BLE 4.0 を使用しない古いフレームワークです。

アドバイスをいただければ幸いです。

ありがとう!

4

5 に答える 5

23

このガイドは、Bluetooth 3.0 に有望に見えます。CoreBluetooth フレームワークは、Bluetooth Low Energy (4.0) 専用であることに注意してください。bluetooth.com の開発ページでは、グローバルに定義されたサービスの例をいくつか見ることができます。Guan Yang が言及しているように、心拍数サービスが 0x180D であることがわかります。ユニットの UUID はメーカーによって定義されています。

途中で役立つかもしれないコードスニペットを次に示します。

// Initialize a private variable with the heart rate service UUID    
CBUUID *heartRate = [CBUUID UUIDWithString:@"180D"];

// Create a dictionary for passing down to the scan with service method
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];

// Tell the central manager (cm) to scan for the heart rate service
[cm scanForPeripheralsWithServices:[NSArray arrayWithObject:heartRate] options:scanOptions]
于 2012-06-20T22:04:56.923 に答える
15

Bluetoothには「サービス」があります。デバイスは1..Nサービスを公開し、各サービスには1..M特性があります。各サービスには、 UUIDと呼ばれる一意の識別子があります。

たとえば、サービス「心拍数」を提供する心拍数 Bluetooth センサーは、UUID 0x180Dでサービスを公開します。

(その他のサービスはこちら)

そのため、検索を実行するときは、UUIDを基準として指定し、検索するサービスを指定する必要があります。

于 2012-10-11T10:47:44.620 に答える
10

例の1つを見てください。関連する行は次のとおりです。

[manager scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@"180D"]] options:nil];

(これはMac OS Xの例ですが、iOS CoreBluetooth APIは非常に似ています。)

CBUUIDは、デバイスではなく、関心のあるサービスを識別します。標準サービスには16ビットのUUIDがあり、この場合、心拍数モニターの場合は0x180d、デバイス情報の場合はおそらく0x180aですが、独自のサービスには128ビットのUUID(16バイト)があります。

ほとんどのデバイスはデバイス情報サービスを実装しているため、デバイスを探しているだけの場合は、を試すことができます[CBUUID UUIDWithString:@"180A"]

于 2012-04-17T14:45:57.907 に答える
3

デバイスをスキャンしているときにこれを与えてみてください

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

 [self.centralManager scanForPeripheralsWithServices:nil options:options];

didDiscoverPeripheralデリゲート メソッドでデバイスのリストを取得できるようになります

于 2013-06-27T20:29:40.230 に答える
0

落ち着いてhttps://github.com/l0gg3r/LGBluetoothを使用してください

あなたがする必要があるすべて

[[LGCentralManager sharedInstance] scanForPeripheralsByInterval:4
                                                         completion:^(NSArray *peripherals) {
     }];

于 2014-02-17T17:54:59.060 に答える