packagecom.example.takahiro.altbeaconsample;importandroid.os.RemoteException;importandroid.support.v7.app.ActionBarActivity;importandroid.os.Bundle;importandroid.util.Log;importandroid.view.Menu;importandroid.view.MenuItem;// 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;publicclassMainActivityextendsActionBarActivityimplementsBeaconConsumer{// BeaconConsumerインターフェースを実装privatestaticStringTAG="AltBeacon Sample";// BeaconManagerクラスの変数を定義privateBeaconManagerbeaconManager;// iBeaconのデータを認識するためのParserフォーマットpublicstaticfinalStringIBEACON_FORMAT="m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// staticメソッドでBeaconManagerクラスのインスタンスを取得beaconManager=BeaconManager.getInstanceForApplication(this);// BeaconParseをBeaconManagerに設定beaconManager.getBeaconParsers().add(newBeaconParser().setBeaconLayout(IBEACON_FORMAT));}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main,menu);returntrue;}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){// 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.intid=item.getItemId();//noinspection SimplifiableIfStatementif(id==R.id.action_settings){returntrue;}returnsuper.onOptionsItemSelected(item);}@OverridepublicvoidonBeaconServiceConnect(){// BeaconManagerクラスのモニタリング通知受取り処理beaconManager.setMonitorNotifier(newMonitorNotifier(){@OverridepublicvoiddidEnterRegion(Regionregion){// 領域進入時に実行Log.d(TAG,"didEnterRegion");}@OverridepublicvoiddidExitRegion(Regionregion){// 領域退出時に実行Log.d(TAG,"didExitRegion");}@OverridepublicvoiddidDetermineStateForRegion(inti,Regionregion){// 領域への侵入/退出のステータスが変化したときに実行Log.d(TAG,"didDetermineStateForRegion");}});try{// UUIDの作成Identifieridentifier=Identifier.parse("434B4666-E4B2-4B78-8C13-901AFEE3DBB6");// モニタリングの開始beaconManager.startMonitoringBeaconsInRegion(newRegion("unique-id-001",identifier,null,null));}catch(RemoteExceptione){// 例外が発生した場合の処理e.printStackTrace();}}@OverrideprotectedvoidonPause(){super.onPause();beaconManager.unbind(this);}@OverrideprotectedvoidonResume(){super.onPause();beaconManager.bind(this);}}