iBeacon implementation

Hi,
I'm trying to implement iBeacon functionality and am not sure if I'm doing this properly. Since there is no longer an iOS sample and we need to use the wrapped C++ api, I'm not sure if I'm setting the uuid properly. Please have a look at my code snippet below. Any help would be appreciated. the variable "device" is a MetaWear object.

To disconnect from the board, do I call mbl_mw_debug_disconnect or device.cancelConnection()?

=========================================
var uuid = device.peripheral.identifier.uuid
withUnsafeMutableBytes(of: &uuid) {
rawPointer in
let typedPointer = rawPointer.baseAddress!.assumingMemoryBound(to: UInt8.self)
uuid_generate(typedPointer)
mbl_mw_ibeacon_set_uuid(device.board, typedPointer)
}

        mbl_mw_ibeacon_set_major(device.board, 1)             
         mbl_mw_ibeacon_set_minor(device.board, 1)
         mbl_mw_ibeacon_set_rx_power(device.board, -55)
         mbl_mw_ibeacon_set_tx_power(device.board, -12)
         mbl_mw_ibeacon_set_period(device.board, 240)
         mbl_mw_ibeacon_enable(device.board)

Comments

  • Is there nobody that has implemented iBeacon on iOS using the latest SDK? I have to say that the latest SDK has pretty thin documentation on iOS. the documentation was much better on previous SDK version. Any help from anyone would be appreciated. Thanks

  • @mlin34
    We are working on updating the documentation. It should be updated by next week.

  • Great to hear. In the meantime is there any guidance you can provide with my question above?

  • I put up example code for you, find it here: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS/tree/master/MetaWearApiTest

    Don't forget that once you setup iBeacon mode, you have to disconnect from the board so that it starts "beaconing"

  • Thanks I will have a look. At first glance the example just enabled and disables the board for iBeacon and doesn't set any of the parameters I mention above. As it's a C++ API, I'm not sure if I'm passing raw pointers in properly (as per the snippet on my original post). ****Also, to disconnect from the board, do I call mbl_mw_debug_disconnect or device.cancelConnection()? it's not clear on the documentation. Thanks in advance****

  • Please read our tutorials.

  • @Laura said:
    I put up example code for you, find it here: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS/tree/master/MetaWearApiTest

    Don't forget that once you setup iBeacon mode, you have to disconnect from the board so that it starts "beaconing"

    Hi Laura,
    The sample code doesn't show how to set the other iBeacon parameters. As per the comment, it's a "To do" task (code snippet from your sample below).
    Do you have a sample on how to set the uuid? This is the first issue I had posted in this forum. the iOS CLBeaconRegion call the uuid. Thanks in advance for your help.

    ===========
    @IBAction func startiBeaconPressed(_ sender: Any) {
    // TODO: Expose the other iBeacon parameters
    mbl_mw_ibeacon_enable(device.board)
    }

  • What other items are you looking for?

  • @Laura said:
    What other items are you looking for?

    Setting the uuid. Since what you provide is a wrapper for a C++ API, if you can provide a Swift example of how the parameter can be passed in as a pointer it would help. The signature of the C++ API is below. I'm looking for an example in Swift on how the UUID parameter is passed into this. Thanks.

    void mbl_mw_ibeacon_set_uuid(const MblMwMetaWearBoard *board, uint8_t ad_uuid[16]) {
    SET_PARAM_NBYTES(IBEACON_UUID, ad_uuid, 16);
    }

  • edited February 24

    You should be able to use the C++ calls directly. Most of the swift calls are just wrappers around the C++ (just take a look at the code).

    ...unless the C++ functions aren't exposed at all in the swift bridge...
    Let me know. I haven't checked.

    If they aren't exposed you will need to make a pull or change request (ideally a pull if you can code this yourself).

  • Hi Laura, I'm really not sure why the Swift support has taken a step back with the latest SDK. I had fully functioning code for iBeacon before the latest SDK. The code to set the UUID and parameters for iBeacon used to be straightforward as per below here:

    let uuid = device.identifier.uuidString
    device.iBeacon?.setUuid(CBUUID(string: uuid))
    device.iBeacon?.setMajor(1)
    device.iBeacon?.setMinor(1)
    device.iBeacon?.setCalibratedReceiverPower(-55)
    device.iBeacon?.setTransmitPower(.power0dBm)
    device.iBeacon?.setFrequency(240)

    device.iBeacon?.setBeaconOnAsync(true)

    Now the only call that has been provided in the example is this: mbl_mw_ibeacon_enable(device.board)

    Now I took the sample code that MBientLab has provided and added CLLocationManager code to start iBeacon monitoring. I'm assuming with the single call you provided in the sample to enable iBeacon, I can scan for the device.peripheral.identifier identifier. I've set the CLBeaconRegion with this identifier and nothing is transmitted (and yes I disconnected the device).

    Can you please provide either documentation or samples on how to call the C++ APIs (shown below) from Swift? I like the Mbient sensors and have been using the for a while...but the step back you have taken with the documentation for Swift on the latest version makes it difficult to develop on your device. Thanks.

    void mbl_mw_ibeacon_set_period(const MblMwMetaWearBoard *board, uint16_t period) {
    SET_PARAM_USHORT(IBEACON_PERIOD, &period);
    }

    void mbl_mw_ibeacon_set_tx_power(const MblMwMetaWearBoard *board, int8_t tx_power) {
    SET_PARAM_UBYTE(IBEACON_TX, static_cast(tx_power));
    }

    void mbl_mw_ibeacon_set_rx_power(const MblMwMetaWearBoard *board, int8_t rx_power) {
    SET_PARAM_UBYTE(IBEACON_RX, static_cast(rx_power));
    }

  • sorry for the large fonts..not sure how that happened

  • It's just the example code for the ibeacon that has been removed. All the sdk functionalities are still there and exactly the same as before. Nothing has changed.

  • Not sure you are being accurate. It's not just the sample code. Have a closer look. Your Swift interfaces are no longer there. it used to be done like this:

    uuid = device.identifier.uuidString
    device.iBeacon?.setUuid(CBUUID(string: uuid))
    device.iBeacon?.setMajor(1)
    device.iBeacon?.setMinor(1)
    device.iBeacon?.setCalibratedReceiverPower(-55)
    device.iBeacon?.setTransmitPower(.power0dBm)
    device.iBeacon?.setFrequency(240)

    with the latest SDK, you can NOT do that anymore because you removed the Swift support. Really if you have a sample or documentation on how to set the UUID for an iBeacon, please point me to it. I can't find it anywhere.

    The sample code you provided does not work with iBeacon as per my message above

    Appreciate it if you can support your user community. thx

  • Please post issue with code and screenshots of the error.... I am not able to replicate.

  • edited March 10

    I added more example code for you in the SDK API test: https://github.com/mbientlab/MetaWear-SDK-iOS-macOS-tvOS/blob/master/MetaWearApiTest/MetaWearApiTest/DeviceDetailViewController.swift

    Everything is working just fine for me. You can use my example if you want.

  • Hi Laura,
    I just downloaded your latest example, ran it on my device and got a run time error as your code tried to unwrap an optional value. I have uploaded the screenshot. There looks to be an issue (Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value) in how you are using UInt8.init() as the uuid variable had a value (I debugged it). I'm really not sure how you have it working?? I made zero modifications to your sample code.
    I have uploaded the screenshot.

  • edited March 11
        let uuid = UUID().uuidString
        var array: [UInt8] = Array(uuid.utf8)
        let up: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.init(&array)
    
Sign In or Register to comment.