public
class
MainActivity
extends
AppCompatActivity
implements
BeaconConsumer {
TextView textView;
private
BeaconManager beaconManager;
String beaconUUID=
"00000000000000000000000000000054"
;
private
static
final
int
PERMISSION_REQUEST_COARSE_LOCATION =
1
;
private
String TAG =
"CHS_BEA"
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.tv_message);
beaconManager = BeaconManager.getInstanceForApplication(
this
);
beaconManager.getBeaconParsers().add(
new
BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
beaconManager.getBeaconParsers().add(
new
BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_TLM_LAYOUT));
beaconManager.getBeaconParsers().add(
new
BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_URL_LAYOUT));
beaconManager.getBeaconParsers().add(
new
BeaconParser().setBeaconLayout(
"m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25\""
));
beaconManager.getBeaconParsers().add(
new
BeaconParser().setBeaconLayout(
"m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"
));
beaconManager.bind(
this
);
}
@Override
public
void
onBeaconServiceConnect() {
beaconManager.removeAllMonitorNotifiers();
Log.i(TAG,
"onBeaconServiceConnect Start"
);
beaconManager.addRangeNotifier(
new
RangeNotifier() {
@Override
public
void
didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
Log.i(TAG,
"didRangeBeaconsInRegion Start -> "
+ region.toString());
Log.i(TAG,
"didRangeBeaconsInRegion beacons -> "
+ beacons.size());
}
});
beaconManager.addMonitorNotifier(
new
MonitorNotifier() {
@Override
public
void
didEnterRegion(Region region) {
Log.i(TAG,
"I just saw an beacon for the first time!"
);
Toast.makeText(MainActivity.
this
,
"didEnterRegion - 비콘 연결됨"
, Toast.LENGTH_SHORT).show();
textView.setText(
"Beacon connected"
);
}
@Override
public
void
didExitRegion(Region region) {
Log.i(TAG,
"I no longer see an beacon"
);
Toast.makeText(MainActivity.
this
,
"didExitRegion - 비콘 연결 끊김"
, Toast.LENGTH_SHORT).show();
textView.setText(
"Beacon disconnected"
);
}
@Override
public
void
didDetermineStateForRegion(
int
state, Region region) {
Log.i(TAG,
"I have just switched from seeing/not seeing beacons: "
+state);
}
});
try
{
beaconManager.startMonitoringBeaconsInRegion(
new
Region(
"beacon"
,
null
,
null
,
null
));
}
catch
(RemoteException e) { }
try
{
beaconManager.startRangingBeaconsInRegion(
new
Region(
"beacon"
,
null
,
null
,
null
));
}
catch
(RemoteException e)
{
}
}
@Override
public
void
onDestroy() {
super
.onDestroy();
beaconManager.unbind(
this
);
}
@Override
public
void
onPointerCaptureChanged(
boolean
hasCapture) {
}
}