Search code examples
androidiosbluetoothibeaconeddystone

Android Device to Act as Beacon


I am sorry if this question is not an "SO approved" type of question.

I would like to develop an app that act as a beacon (among of other functionality). This app will be installed on an Android device.

I am very new at all these Bluetooth programming and I would like to get some answers on these questions:

  1. Can anyone point me on the right part as where to start?

  2. How client app get notification from the beacon? Can anyone explain how it works?

Thank you and sorry for these questions.


Solution

  • A few points:

    • Yes, you can make a beacon app that sends both an iBeacon and/or an Eddystone beacon transmission from an Android device. You need a device with Android 5.0+ that supports transmission (not all do). You can see a list of such devices here. The Android Beacon Library shows how to code transmission here. There is also an off-the-shelf Locate app that supports transmission here.

    • It is also possible to make a transmitter beacon app on iOS, but iOS only supports transmission of iBeacon packets (you cannot transmit Eddystone on iOS), and iOS cannot transmit when the app is in the background. Android can.

    • If you want your client app to detect quickly in the background on iOS, you are better off with iBeacon than Eddystone. Detection of iBeacon signals is optimized in the background on iOS and is built-in. Detection of Eddystone requires extra software and is not as fast.

    • To send a notification on beacon discovery you simply write code in the client app that detects the unique beacon identifier then creates a local notification message keyed off the beacon identifier and sends it to the user. The important part to understand us that the client code does all of the message sending locally. All the beacon app does is transmit a unique identifier that the client app receives.

    To illustrate the point about how you send local notifications based on beacon identifiers, here is some sample code for iOS. This code runs on the client app. The Beacon simply sends out a transmission with specific beacon identifiers, and the client app reads them and acts appropriately.

    var lastNotificationTime = NSDate(timeIntervalSince1970: 0) // Initialize last Notification time to a long time ago
    
    func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
      // Only send notification if we have not done so in the last hour (3600 seconds)
      if (lastNotificationTime.timeIntervalSinceNow < -3600.0) {
        for beacon in beacons {
          // Send a 20% off notification for beacon with identifiers major 1, minor 2
          if beacon.major.intValue == 1 && beacon.minor.intValue == 2 {
            let localNotification = UILocalNotification()
            localNotification.alertTitle = "Discount 20% on all items"
            dispatch_async(dispatch_get_main_queue()) {
              UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)
            }
          }
        }
      }
    }
    

    Full Disclosure: I am the lead developer on the Android Beacon Library open source project and the author of the Locate app.