web-dev-qa-db-ja.com

利用可能なBluetoothデバイスのリストを取得する方法

現在、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];

私はApple参照..を調べましたが、そのCBUUIDが何であるかまだ理解していません???すべてのデバイスにはBluetooth IDがありますか?どこで見つけることができますか?

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

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

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

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

ありがとう!

49
mz87

このガイド Bluetooth 3.0に有望に見えます。 CoreBluetoothフレームワークは、Bluetooth Low Energy(4.0)専用です。 bluetooth.comのdev-pages では、グローバルに定義されたサービスの例を見ることができます。GuanYangが述べたように、心拍数サービスは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]
22
chwi

bluetoothには「サービス」があります。デバイスは1..Nサービス、および各サービスには1..M特性。各サービスには、[〜#〜] uuid [〜#〜]と呼ばれる一意の識別子があります。

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

(その他のサービス ここ

したがって、検索を実行するときは、[〜#〜] uuid [〜#〜]を基準として指定し、検索するサービスを指定する必要があります。

15
viktorvogh

例の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"]

10
Guan Yang

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

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

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

didDiscoverPeripheralデリゲートメソッドでデバイスのリストを取得できます。

3
Jes

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

必要なことはすべて

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