简体   繁体   中英

Get notification from a BLE device

I am working on a BLE (bluetooth LE) app that connects with a hardware device . I am able to discover and connect to the device, read data from the device,write data to the device.

What i couldn't find on the BLE docs in Apple , is how can you get a notification when you come near a device , when the app is close .

I know how to register to characteristic notification, but this notification happens only when app is in the background .

I know that iBeacon can detect a bluetooth while the app is closed, and send notification, but i would like to get notification when a device discover a certain BLE with a UUID .

iBeacon,is using the BLE with UUID and major and minor fields,which i dont need/dont want . I would like just to register to notification from a certain UUID from a BLE.

I did this, without any respond :

 self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    [self initRegion];

- (void)initRegion
{
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"4AD3FADF-F179-4343-0000-000000000000"];
    self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:@"BLE-NAME"];
    [self.locationManager startMonitoringForRegion:self.beaconRegion];

}


- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
    NSLog(@"ENTER");

}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
    NSLog(@"EXIT");
}

While iBeacon and general BLE peripherals both use Bluetooth, they are handled differently within iOS.

iBeacons are supported by the CoreLocation framework (as per the code in your question), while BLE peripherals that implement the GATT profile are supported by the Core Bluetooth framework.

The Core Bluetooth Programming Guide describes how to discover and connect to a BLE peripheral. The guide also includes a section on background processing.

Essentially you can issue a "connect" to your target peripheral and iOS will complete the connection when the peripheral is seen, even if your application is in the background - calling your delegate methods to advise you of the connection.

However, while many iBeacons can be configured with the same UUID, a peripheral's UUID is unique, so unless you have previously discovered the peripheral you probably won't be able to issue the connect.

You can scan for peripherals that are advertising specific services in the background and use this to discover and connect to peripherals

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM