Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

macos - Turn Macbook into iBeacon

I know that I can turn iOS devices into iBeacons (Can an iOS7 device act as an iBeacon?). Unfortunately, I only have one device and my beacons have not arrived yet. So I was wondering how I could turn my MacBook Air (Mid-2011, does support Bluetooth 4.0) into an iBeacon for testing purposes. Are there any ready-made applications available like the airlocate for iOS? Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note: This only works in Mavericks, it does NOT work in Yosemite.

Mavericks doesn't have the iBeacon support in Core Location that was added to iOS 7. However, Mavericks does now have the ability to act as an BLE peripheral device. Given that an iBeacon is basically a peripheral it should be (and indeed is) possible to use Mavericks as an iBeacon.

In order to create an iBeacon on iOS you first create a CLBeaconRegion object and then use the peripheralDataWithMeasuredPower: method to get an NSDictionary containing the necessary advertisement data to broadcast. If you take the contents of this NSDictionary from an iOS device and use it on Mavericks then you get an iBeacon.

I have created a class to make this easier and allow you to generate the advertisement data dictionary directly on Mavericks. The source code is available at https://github.com/mttrb/BeaconOSX

The BLCBeaconAdvertisementData class take the proximityUUID, major, minor and calibrated power values and creates an NSDictionary that can be passed to the startAdvertising: method of CBPeripheralManager on Mavericks.

The BLCBeaconAdvertisementData class is quite simple. The main work is done by the following method:

- (NSDictionary *)beaconAdvertisement {
    NSString *beaconKey = @"kCBAdvDataAppleBeaconKey";

    unsigned char advertisementBytes[21] = {0};

    [self.proximityUUID getUUIDBytes:(unsigned char *)&advertisementBytes];

    advertisementBytes[16] = (unsigned char)(self.major >> 8);
    advertisementBytes[17] = (unsigned char)(self.major & 255);

    advertisementBytes[18] = (unsigned char)(self.minor >> 8);
    advertisementBytes[19] = (unsigned char)(self.minor & 255);

    advertisementBytes[20] = self.measuredPower;

    NSMutableData *advertisement = [NSMutableData dataWithBytes:advertisementBytes length:21];

    return [NSDictionary dictionaryWithObject:advertisement forKey:beaconKey];
}

I have a more detailed blog post about this at http://www.blendedcocoa.com/blog/2013/11/02/mavericks-as-an-ibeacon/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...