<省略>// AltBeacon関連のライブラリをimportimportorg.altbeacon.beacon.BeaconConsumer;importorg.altbeacon.beacon.BeaconManager;importorg.altbeacon.beacon.BeaconParser;importorg.altbeacon.beacon.Identifier;importorg.altbeacon.beacon.MonitorNotifier;importorg.altbeacon.beacon.Region;classMainActivity:ActionBarActivity(),BeaconConsumer{privatevalTAG="AltBeacon Sample"// BeaconManagerクラスの変数を定義privatevarbeaconManager:BeaconManager?=nullpublicvalIBEACON_FORMAT="m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// staticメソッドでBeaconManagerクラスのインスタンスを取得beaconManager=BeaconManager.getInstanceForApplication(this)// BeaconParseをBeaconManagerに設定beaconManager!!.beaconParsers.add(BeaconParser().setBeaconLayout(IBEACON_FORMAT))}overridefunonCreateOptionsMenu(menu:Menu):Boolean{// Inflate the menu; this adds items to the action bar if it is present.menuInflater.inflate(R.menu.menu_main,menu)returntrue}overridefunonOptionsItemSelected(item:MenuItem):Boolean{// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.valid=item.itemId//noinspection SimplifiableIfStatementif(id==R.id.action_settings){returntrue}returnsuper.onOptionsItemSelected(item)}overridefunonBeaconServiceConnect(){// BeaconManagerクラスのモニタリング通知受取り処理beaconManager!!.setMonitorNotifier(object:MonitorNotifier{overridefundidEnterRegion(region:Region){// 領域進入時に実行Log.d(TAG,"didEnterRegion")}overridefundidExitRegion(region:Region){// 領域退出時に実行Log.d(TAG,"didExitRegion")}overridefundidDetermineStateForRegion(i:Int,region:Region){// 領域への侵入/退出のステータスが変化したときに実行Log.d(TAG,"didDetermineStateForRegion")}})try{// UUIDの作成validentifier=Identifier.parse("434B4666-E4B2-4B78-8C13-901AFEE3DBB6")// モニタリングの開始beaconManager!!.startMonitoringBeaconsInRegion(Region("unique-id-001",identifier,null,null))}catch(e:RemoteException){// 例外が発生した場合の処理e.printStackTrace()}}overridefunonPause(){super.onPause()beaconManager!!.unbind(this)}overridefunonResume(){super.onPause()beaconManager!!.bind(this)}}