0

2 つの iOS アプリを作成しました。1 つはサービスをアドバタイズする Bluetooth LE ペリフェラル、もう 1 つはアドバタイズされたサービスをスキャンする Bluetooth LE セントラルです。ペリフェラルは iPhone5s で実行され、セントラルは iPad Mini で実行されます。最初に、特定の宣伝されたサービスをスキャンするようにセントラルを設定しましたが、後で任意のサービスをリッスンするように変更しました。どちらの場合でも、中心として機能する iPad Mini アプリは、宣伝されているサービスをまったく検出しません。周辺機器マネージャーをアドバタイズするようにセットアップする方法に問題があるのか​​、中央マネージャーをスキャンするようにセットアップする方法に問題があるのか​​ 、それともデバイス構成の問題なのかはわかりません。これを機能させるために実行できる提案またはテストを提供してください。

以下は、周辺機器として機能する iPhone5s アプリに関連するコードです。

CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];

CBUUID *immediateAlertServiceUUID = [CBUUID UUIDWithString: IMMEDIATE_ALERT_SERVICE_UUID];
CBUUID *alertLevelCharacteristicUUID = [CBUUID UUIDWithString: ALERT_LEVEL_CHARACTERISTIC_UUID];
CBUUID *myCustomCharacteristicUUID = [CBUUID UUIDWithString: MY_CUSTOM_CHARACTERISTIC_UUID];

alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value: nil permissions:CBAttributePermissionsReadable];
myCustomCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:myCustomCharacteristicUUID
                                   properties:CBCharacteristicPropertyRead
                                        value: nil permissions:CBAttributePermissionsReadable];

NSArray *myCharacteristics = @[alertLevelCharacteristic, myCustomCharacteristic];

// Now setup the service
myService = [[CBMutableService alloc] initWithType:immediateAlertServiceUUID primary:YES];

// Finally, associate the characteristic with the service. This is an array of characteristics
myService.characteristics = myCharacteristics;

[peripheralManager addService:myService];

... wait for user to push button to start advertising ...

// Start Advertising
[peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey : @"My Service",
                                       CBAdvertisementDataServiceUUIDsKey : @[myService.UUID] }];

必要なデリゲート メソッドは次のとおりです。注:デリゲート メソッドperipheralManagerDidUpdateStateが起動し、「CoreBluetooth BLEハードウェアの電源がオンになり、準備ができている」ことを示します(中央側でも同じです)。デリゲート メソッドperipheralManager:didAddService:error がエラーなしで起動します (以下の出力を参照)。また、デリゲート メソッドperipheralManagerDidStartAdvertising:error がエラーなしで起動します)。didAddService から出力されたサービス情報は次のとおりです。

<CBMutableService: 0x17008efb0 Primary = YES, UUID = 1802, Included Services = (null), Characteristics = (
"<CBMutableCharacteristic: 0x1702c1500 UUID = 2A06, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>",
"<CBMutableCharacteristic: 0x1702c15e0 UUID = 66E613B5-7225-42C6-A9C2-11FADAE62899, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>")>

CBPeripheralManager Delegate メソッド (すべてのコードについては申し訳ありませんが、完成させようとしています。):

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {

// Determine the state of the peripheral
if ([peripheral state] == CBPeripheralManagerStatePoweredOff) {
    NSLog(@"CoreBluetooth BLE hardware is powered off");
}
else if ([peripheral state] == CBPeripheralManagerStatePoweredOn) {
    NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([peripheral state] == CBPeripheralManagerStateUnauthorized) {
    NSLog(@"CoreBluetooth BLE state is unauthorized");
}
else if ([peripheral state] == CBPeripheralManagerStateUnknown) {
    NSLog(@"CoreBluetooth BLE state is unknown");
}
else if ([peripheral state] == CBPeripheralManagerStateUnsupported) {
    NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
}
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
        didAddService:(CBService *)service
                error:(NSError *)error {

if (error) {
    NSLog(@"Error publishing service: %@", [error localizedDescription]);

    return;
}
else {
    NSLog(@"Hurray! Your Service has been successfully published as: %@", service);
}
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
                                   error:(NSError *)error {

if (error == nil) {
    NSLog(@"Your service is now advertising");
}
else {
    NSLog(@"In peripheralManagerDidStartAdvertising: Your service advertising failed with error: %@", error);
}

}

iPad Mini で実行される関連する中心的なコードは次のとおりです。

// Scan for all available CoreBluetooth LE devices
NSArray *services = @[[CBUUID UUIDWithString:IMMEDIATE_ALERT_SERVICE_UUID]];

CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

//[centralManager scanForPeripheralsWithServices:services options:nil];
[centralManager scanForPeripheralsWithServices:nil options:nil];
self.centralManager = centralManager;

そして、これは Central デリゲート メソッドの 1 つです。centralManagerDidUpdateState: を除いて、どのデリゲート メソッドも起動しません。

// CBPeripheralDelegate - Invoked when you discover the peripheral's available services.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
    NSLog(@"Did Discover Services");
    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:nil forService:service];
    }
}

// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did Discover Peripheral");
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if (![localName isEqual:@"My Service"]) {
    // We found the Device
    [self.centralManager stopScan];
    self.myPeripheral = peripheral;
    peripheral.delegate = self;
    [self.centralManager connectPeripheral:peripheral options:nil];
}
}

最後に、私のデバイスで BLE が動作するかどうか疑問に思います。iPhone と iPad Mini にいくつかの異なる iBeacon アプリをロードして、2 つのデバイスが iBeacon (一方は送信、もう一方は受信) を認識できるかどうかを確認しましたが、iBeacon も検出されませんでした。2台のiPhoneでも試してみました。また、Bluetoothをオフにしてからオンにしました。また、デバイスの電源をオフ/オンにしてみました。両方のデバイスがフォアグラウンドで実行されています。まだ運がありません。助けてください。

4

1 に答える 1

1

ここにすべてのコメントを連結します。

LightBlueBLE Utilityなどのアプリを使用すると、問題が周辺側にあるのか中央側にあるのかを特定するのに役立ちます。これは、両方の側を自分で開発しているためです。

を探す前にCBServices、 に接続する必要がありますCBPeripheral。あなたが事前に示した方法であり、明らかではなかったようです。

また、 でスキャンを開始する前に、CBCentralManagerをチェックする必要があり、 である必要がstateありますCBPeripheralManagerStatePoweredOn

于 2015-01-23T14:25:46.830 に答える