Beacon - mesosfer/Mesosfer-Cubeacon-iOS GitHub Wiki
Mesosfer provide a specialized bucket Beacon that automatically handles much of the functionality required for beacon management.
Objects and Types
Here is some predefined Beacon object :
- name =>
NSString
, define the name of a beacon. - uuid =>
NSUUID
, define the proximity UUID of a beacon. Most commonly represented as a string, e.g.CB10023F-A318-3394-4199-A8730C7C1AEC
. - major =>
NSNumber
, define the major number of a beacon. An unsigned short integer, i.e., an integer ranging from1
to65535
, (0
is a reserved value). - minor =>
NSNumber
, define the minor number of a beacon. Also an unsigned short integer, like the major number. - metadata =>
NSDictionary
, define custom object of a beacon.
Saving Beacons
Let’s say you want to save the MFBeacon
described above to the Mesosfer Cloud. The interface is similar to a NSDictionary
, plus the saveAsyncWithBlock:
method:
// create object beacon with name
MFBeacon *beacon = [MFBeacon beaconWithName:@"Cubeacon"];
// set proximity UUID of beacon
beacon.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"CB10023F-A318-3394-4199-A8730C7C1AEC"];
// set major number of beacon
beacon.major = @1;
// set minor number of beacon
beacon.minor = @284;
// set custom objects of beacon
beacon[@"identifier"] = @"Beacon-Red-Front-Gate-1-284";
beacon[@"timestamp"] = [NSDate date];
beacon[@"object"] = [NSDictionary dictionary];
beacon[@"array"] = [NSArray array];
// execute save async using block
[beacon saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if (!succeeded) {
NSLog(@"Failed to save beacon, cause: %@", error);
return;
}
NSLog(@"Beacon saved.");
}];
Retrieve Beacons
If you need to fetch a Beacon with the latest data that is in the cloud, you can call the fetchAsyncWithBlock:
method like so:
// create beacon from existing objectId
MFBeacon *beacon = [MFBeacon beaconWithObjectId:@"beaconObjectId"];
// fetching the beacon data
[beacon fetchAsyncWithBlock:^(id _Nullable object, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to fetch beacon, cause: %@", error);
return;
}
NSLog(@"Beacon fetched.");
}];
This will automatically update beacon
with the latest data from cloud.
Querying Beacon
There are many other ways to retrieve data with MFQuery
. You can retrieve many data at once, put conditions on the data you wish to retrieve.
Basic Query
In many cases, there is a condition that need to specify which datas you want to retrieve. The MFQuery
offers different ways to retrieve an array of MFBeacon
s. The general pattern is to create a MFQuery
, put conditions on it, and then retrieve a NSArray
of matching MFBeacon
s using the findAsyncWithBlock:
method. For example, to retrieve MFBeacon
s data with a name
, use the whereKey:equalTo:
method to constrain the value
for a key
:
MFQuery *query = [MFBeacon query];
[query whereKey:BEACON_KEY_NAME equalTo:@"Beacon-Red-Front-Gate-1-284"];
[query findAsyncWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
if (error) {
NSLog(@"Failed to query beacon, cause: %@", error);
return;
}
NSLog(@"Found beacon: %lu", objects.count);
}];
Query Constraint
Refer to this link to learn more about query constraint.
Update Beacon
After getting the MFBeacon
data, you can update your data that stored in cloud using method saveAsyncWithBlock:
.
MFBeacon *beacon; // fetched beacon data
// update beacon data
beacon.proximityUUID = [[NSUUID alloc] initWithUUIDString:@"11111111-2222-3333-4444-555555555555"];
beacon.major = @6;
beacon.minor = @7;
beacon[@"identifier"] = @"Beacon-Red-Waiting-Room-6-7";
beacon[@"timestamp"] = [NSDate date];
// execute save data
[beacon saveAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if (!succeeded) {
NSLog(@"Failed to save beacon, cause: %@", error);
return;
}
NSLog(@"Beacon saved.");
}];
Delete Beacon
To delete a MFBeacon
from the Mesosfer Cloud, use method deleteAsyncWithBlock:
:
// the current beacon object to delete
MFBeacon *beacon;
// execute delete beacon async
[beacon deleteAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if (!succeeded) {
NSLog(@"Failed to delete beacon, cause: %@", error);
return;
}
NSLog(@"Beacon deleted.");
}]