銀の弾丸、はじめました

Unityとかガジェットとか

BLEデバイス(Peripheral)のUUID一覧を取得する

iOS Objective-C のメモ

// セントラル
@property (nonatomic, strong) CBCentralManager *centralManager;
// ペリフェラル
@property (nonatomic, strong) CBPeripheral *peripheral;
@property (nonatomic) NSMutableArray *peripherals;
@end

NSMutableArray *a_peripheral;
NSString * const ServiceUUID = @"XXXXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";

@implementation PeripheralTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"PeripheralTableViewController - viwDidLoad");

    self.peripherals = [NSMutableArray array];  // データ用
    a_peripheral = [NSMutableArray array];      // 文字列用

    self.centralManager = [[CBCentralManager alloc] initWithDelegate:self
                                                               queue:nil];
    self.serviceUUID = [CBUUID UUIDWithString:ServiceUUID];
    self.characteristicUUID = [CBUUID UUIDWithString:CharacteristicUUIDHeartRateMeasurement];
}


// セントラルマネージャ状態が変化
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {

    NSLog(@"Updated state: %ld", (long)central.state);

    switch (central.state) {
        case CBCentralManagerStatePoweredOn:
        {
            // スキャン開始
            NSArray *services = @[self.serviceUUID];
            [self.centralManager scanForPeripheralsWithServices:services
                                                        options:nil];
            break;
        }
        default:
            break;
    }
}

// ペリフェラル発見
- (void)   centralManager:(CBCentralManager *)central
    didDiscoverPeripheral:(CBPeripheral *)peripheral
        advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"peripheral:%@, advertisementData:%@, RSSI:%@",
          peripheral, advertisementData, RSSI);

    // 単数の場合は以下で良いが,
    //self.peripheral = peripheral;
    // 複数のPeripheral取得には配列に格納する必要がある
    // strong属性の配列(NSMutableArray)に保持
    if (![self.peripherals containsObject:peripheral]) {
        NSLog(@"peripheral.identifier : %@", peripheral.identifier);

        [self.peripherals addObject:peripheral.identifier];

        // <__NSConcreteUUID 0x15e85410> 1174BF05-3D9A-2AD4-40D8-8A35E4D74A4A
        // <__NSConcreteUUID 0x15e8a1c0> 20B403AA-24E0-C70A-48A6-62BD9F40A1E3
        NSString *source = [NSString stringWithFormat:@"%@", peripheral.identifier];

        NSString *pattern = @"(^<.*> )";
        NSString *replacement = @"";

        NSRegularExpression *regexp = [NSRegularExpression
                                       regularExpressionWithPattern:pattern
                                       options:NSRegularExpressionCaseInsensitive
                                       error:nil
                                       ];

        NSString *str = [regexp
                         stringByReplacingMatchesInString:source
                         options:NSMatchingReportProgress
                         range:NSMakeRange(0, source.length)
                         withTemplate:replacement
                         ];
        NSLog(@"これがPeripheralのUUIDだ!!! : %@",  str);

        [a_peripheral addObject:str];

        NSLog(@"self.peripherals -> %@", self.peripherals);
        NSLog(@"a_peripheral -> %@", a_peripheral);
        NSLog(@"self.peripherals count : %d", [self.peripherals count]);
        NSLog(@"a_peripheral count : %d", [a_peripheral count]);
    }

    // スキャン停止
    //[self.centralManager stopScan];

    // 接続開始
    //[central connectPeripheral:peripheral options:nil];
}

加工した文字列用の配列を用意したのは,生のまま出力すると以下の用になってしまうため

(結果1)

self.peripherals -> (
    "<__NSConcreteUUID 0x15e85410> 1174BF05-3D9A-2AD4-40D8-8A35E4D74A4A",
    "<__NSConcreteUUID 0x15e8a1c0> 20B403AA-24E0-C70A-48A6-62BD9F40A1E3"
)

<__NSConcreteUUID 0x15e8a1c0>  の部分が不要で,気持ち悪いので
別途文字列にして加工してUUIDのみ配列に入れてる.

(結果2)

a_peripheral -> (
    "1174BF05-3D9A-2AD4-40D8-8A35E4D74A4A",
    "20B403AA-24E0-C70A-48A6-62BD9F40A1E3"
)

追記(2015/12/04)

どうやら,文字列操作しなくてもUUIDのNSStringを取得できるらしい…

[peripheral.identifier UUIDString]

stackoverflow.com

参考文献

iOS×BLE Core Bluetoothプログラミング

iOS×BLE Core Bluetoothプログラミング