无需配对即可自动连接蓝牙设备

时间:2016-01-22 10:15:17

标签: ios bluetooth bluetooth-lowenergy

是否可以配置蓝牙设备,以便我的应用程序在接近时自动连接到它 - 没有配对等?

该设备将定制,应用程序将由我编写,我首先要定义一些规格。如果设备中有一个选项,只要它靠近我的(打开的)应用程序,它们都会自动连接而没有任何设置过程,那就太棒了。

1 个答案:

答案 0 :(得分:0)

当然可以。如果设备是定制的,你知道它的特点

以下代码(TRANSFER_SERVICE_UUID - 您设备的GUID):

 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];


- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// You should test all scenarios
 if (central.state != CBCentralManagerStatePoweredOn) {
    [self stop];
    return;
 }

if (central.state == CBCentralManagerStatePoweredOn) {
    // Scan for devices
    [_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
    NSLog(@"Scanning started");

    if(_delegate)
    {
        if([_delegate respondsToSelector:@selector(CB_changedStatus:message:)])
        {
            [_delegate CB_changedStatus:CBManagerMessage_ScanningStarted message:@"Scanning started"];
        }
    }
  }
}

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

NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);

if (_discoveredPeripheral != peripheral) {
    // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
    _discoveredPeripheral = peripheral;



    // And connect
    NSLog(@"Connecting to peripheral %@", peripheral);

    if(_delegate)
    {
        if([_delegate respondsToSelector:@selector(CB_changedStatus:message:)])
        {
            [_delegate CB_changedStatus:CBManagerMessage_ConnectingToPeripheral message:[NSString stringWithFormat:@"Connecting to peripheral %@", peripheral]];
        }
    }

    [_centralManager connectPeripheral:peripheral options:nil];

  }
}