2

I'm working on a tracker iOS app to track the (GPS) position of a device (the device is currently an Arduino Uno with Adafruit Bluefruit LE UART Friend for Bluetooth connection and a Ublox GPS module for GPS coordinates). I've set up the Arduino and an iOS app and I successfully receive longitude and latitude through separate characteristics via a one service.

To add/update an Annotation in a MapkitView with the location of my tracker device I need to access the longitude and latitude in one function. However the BLEHandler and BLEHandlerDelegate both only send a values from one characteristic (latitude or longitude). I therefore think I need some function or adjustment where I can join these two values (latitude & longitude) to access in the ViewController.

Can you give some guidance or show some example code on how to make this work?

I'm kind of new in the programming world so forgive me of any naivety.

Many thanks in advance.

Here is some code sample: BLEHandler

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {

    var dataStrLat : String?
    var dataStrLong : String?

    if characteristic.UUID == UUID_CHAR_LAT {


        var charValue : NSData = characteristic.value

        dataStrLat  = NSString(data: charValue, encoding: NSASCIIStringEncoding) as? String
        //var dataStrLat : String? = NSString(data: charValue, encoding: NSASCIIStringEncoding) as? String

        if(dataStrLat == nil){
            dataStrLat = "blank"
        }


        NSLog("%@: Characteristic (gps lat) changed for %@ with value %@", TAG, peripheral.name, dataStrLat!)

        delegate.receivedStringValue(peripheral.name, dataStr: dataStrLat!)

        }

    if characteristic.UUID == UUID_CHAR_LONG {


        var charValue : NSData = characteristic.value

        dataStrLong = NSString(data: charValue, encoding: NSASCIIStringEncoding) as? String
        //var dataStrLong : String? = NSString(data: charValue, encoding: NSASCIIStringEncoding) as? String

        if(dataStrLong == nil){
            dataStrLong = "blank"
        }


        NSLog("%@: Characteristic (gps long) changed for %@ with value %@", TAG, peripheral.name, dataStrLong!)

        delegate.receivedLongitudeValue(peripheral.name, dataStr: dataStrLong!)

    }


}

BLEHandlerDelegate

func receivedStringValue(deviceName: String, dataStr : String)

func receivedLongitudeValue(deviceName: String, dataStr : String)
fbossong
  • 21
  • 3
  • Why don't you just represengt the pair lat,lon in one String: e.g "17.123456,58.123456". And then separate it later into lat and lon components – AlexWien Oct 28 '15 at 18:32
  • Hi, Thanks for your suggestion. I'm using a Bluefruit (NRF51) based BLE board which allows sending packets up to 20 bytes via Arduino. Since lat/lon takes up more bytes (chars) I had to split the packets. I've not figured out how to make it less than 20 bytes and decided to split the packets. If it is possible to send lat/lon in one packet/string I could indeed use them and split them if needed. – fbossong Oct 29 '15 at 20:10

1 Answers1

1

lat, lon each takes 4 bytes. So you need 8 byte for a lat lon coordinate:

You get 4 bytes if you convert to 4 byte integer, by multipying with 1E7 and rounding or casting to int.

int latInt = round(latitude * 1E7);

If you need to store it as characters.
you need 5 or 6 digits after comma in worst case this is

-129,12345 which is 10 digits for longitude
worst case for latitude:
-89,12345 which is 9.
So you would have one char for separator, or you work with fixed length.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Thanks! I'll give this a try. I'v also found a way to to send more characters via another GATT bluetooth service (BLUEUARTTX) [link](https://learn.adafruit.com/introducing-adafruit-ble-bluetooth-low-energy-friend/ble-services) which allows up to 240 characters. – fbossong Nov 02 '15 at 09:45