Reputation: 17379
is it possible to configure a bluetooth-device in a way that my app automatically connects to it when near - without pairing etc.?
The device will be custom built and the app will be written by me, I first have to define some specs. It would be great if there's an option in the device that as soon as it's near my (opened) app that both are connected automatically without any setup process.
Upvotes: 1
Views: 3925
Reputation: 4817
Sure you can. If the device will be custom made and you know its characteristics
Code below (TRANSFER_SERVICE_UUID - GUID of your device):
_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];
}
}
Upvotes: 1