Search code examples
androidswiftcore-bluetoothbeacon

CoreBluetooth cannot be found when I use my Android device as ibeacon using AltBeacon


I have Ibeacon scanning app which use CoreBluetooth. I want to use my real android device as an beacon. I can scan my Ibeacons but can not found my android device.

import Foundation
import CoreBluetooth 

public class BeaconScanning: NSObject, BKCentralDelegate{
  let central = BKCentral()

  override init(){

    super.init()
    central.delegate = self
    do {
      let serviceUUID = CBUUID(string: "FF80") // For my used Ibeacon Service Data
      let characteristicUUID = CBUUID( )
      let configuration = BKConfiguration(dataServiceUUID: serviceUUID, dataServiceCharacteristicUUID: characteristicUUID)  // Configration for scanning
      try central.startWithConfiguration(configuration)
    } catch let error { }

    central.scanContinuouslyWithChangeHandler({ changes, discoveries in }, stateHandler: { newState in }, duration: 2, inBetweenDelay: 0.1, beaconHandler: { beacons in

      for beacon in beacons! {

         // get beacons properties

      }

    }, errorHandler: { error in })

  }

  public func central(_ central: BKCentral, remotePeripheralDidDisconnect remotePeripheral: BKRemotePeripheral) {

  } 
}

I can scan my Ibeacon devices in IOS app. But, it cannot be found when I use my Android device as Ibeacon using AltBeacon.

Beacon beacon = new Beacon.Builder().  
                .setId1("e2c56db5-dffb-48d2-b060-d0f5a71096e0") //uuid the same my Ibeacon
                .setId2("0") 
                .setId3("0")
                .setManufacturer(0x0118)
                .setTxPower(-59)
                .setDataFields(Arrays.asList(new Long[] {0l}))
                .build();
        BeaconParser beaconParser = new BeaconParser()
                .setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"); //altbeacon layout for parser transmitting as a beacon 
        BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
        beaconTransmitter.startAdvertising(beacon); 

Solution

  • Using CoreLocation:

    Beacon beacon = new Beacon.Builder()
                    .setManufacturer(0x004c) // Use Ibeacon Manufacturer
                    .setId1("e2c56db5-dffb-48d2-b060-d0f5a71096e0")
                    .setId2("0")
                    .setId3("0")
                    .setTxPower(-59)
                    .setDataFields(Arrays.asList(new Long[] {0l}))
                    .build();
            BeaconParser beaconParser = new BeaconParser()
                    .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25");//BeaconLayout startwith ("m:2-3=0215)
            BeaconTransmitter beaconTransmitter = new BeaconTransmitter(getApplicationContext(), beaconParser);
            beaconTransmitter.startAdvertising(beacon);