1

基本上我有两个应用程序,一个用于扫描蓝牙设备,另一个用于广告,我想做的是获取所有 iOS 设备的列表,在我附近的扫描 iOS 设备的应用程序上。

到目前为止,这是我的代码:

扫描.m:

// Scan for all available CoreBluetooth LE devices
    NSDictionary *scanOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@(YES)};
    NSArray *services = @[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033366"]];

    [self.centralManager scanForPeripheralsWithServices:services options:scanOptions];

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSLog(@"RSSI: %d", [RSSI intValue]);

}

// method called whenever the device state changes.
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    // Determine the state of the peripheral
    if ([central state] == CBCentralManagerStatePoweredOff) {
        NSLog(@"CoreBluetooth BLE hardware is powered off");
    }
    else if ([central state] == CBCentralManagerStatePoweredOn) {
        NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
    }
    else if ([central state] == CBCentralManagerStateUnauthorized) {
        NSLog(@"CoreBluetooth BLE state is unauthorized");
    }
    else if ([central state] == CBCentralManagerStateUnknown) {
        NSLog(@"CoreBluetooth BLE state is unknown");
    }
    else if ([central state] == CBCentralManagerStateUnsupported) {
        NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
    }
}

广告.m:

    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
    [self listenForRelays];


    /** Required protocol method.  A full app should take care of all the possible states,
 *  but we're just waiting for  to know when the CBPeripheralManager is ready
 */
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{

    if (peripheral.state == CBPeripheralManagerStatePoweredOn)
    {

        // We're in CBPeripheralManagerStatePoweredOn state...
        NSLog(@"self.peripheralManager powered on.");

        // ... so build our service.

    }
}


-(void) listenForRelays {

    if(self.peripheralManager.state == CBPeripheralManagerStatePoweredOn)
    {

        NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey:@"my-peripheral",
                                          CBAdvertisementDataServiceUUIDsKey:@[[CBUUID UUIDWithString:@"1CC024D6-E413-4B56-993C-831CAF033322"]]};

        // Start advertising over BLE
        [self.peripheralManager startAdvertising:advertisingData];
    }

}

当我在不同的 iphone 上运行不同的应用程序时,什么也没有发生!

这不会崩溃,但didDiscoverPeripheral永远不会被调用:(请帮帮我!是的,我已经阅读了文档,但仍然没有好处:((

干杯

4

1 回答 1

4

在您处于开机状态之前,您无法启动扫描 - 在您的扫描应用程序中使用类似于广告应用程序中的代码。

在 iOS 7 中,在开机前开始扫描会在控制台上发出警告,但它仍然有效。在 iOS 8 中它不起作用。

于 2014-10-12T02:23:21.763 回答