固定的!!!是的,这是可能的。我在这里发布示例代码:我希望有人觉得这很有用
问候。
public class MyRegionApp extends Application implements BootstrapNotifier, BluetoothAdapter.LeScanCallback {
private final String DebugTag="HOLAREGION";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private BeaconManager mBeaconManager;
private static final String UUID = "01122334-4556-6778-899a-abbccddeeff0";
private long time=0;
private BluetoothAdapter mBluetoothAdapter;
private String Minor;
private String Major;
private String UUID1;
private BluetoothManager bluemanager;
@Override
public void onCreate() {
super.onCreate();
time=System.currentTimeMillis();
Log.d(DebugTag, "App started up");
bluemanager= (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluemanager.getAdapter();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
//BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
mBeaconManager.setBackgroundScanPeriod(1100l);
mBeaconManager.setBackgroundBetweenScanPeriod(1100l);
Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
//Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
//Region region = new Region("allbeacons", null , null, null);
backgroundPowerSaver = new BackgroundPowerSaver(this);
regionBootstrap = new RegionBootstrap(this, region);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// TODO Auto-generated method stub
Log.d(DebugTag, "Got a didDetermineStateForRegion call with region:"+arg1.toString() );
}
@Override
public void didEnterRegion(Region arg0) {
// TODO Auto-generated method stub
time=System.currentTimeMillis()-time;
Log.d(DebugTag, "Got a didEnterRegion call region:"+arg0.getUniqueId());
Log.d(DebugTag, "Got a didEnterRegion call region TIME: "+time);
mBluetoothAdapter.startLeScan(this);
//Descomentar la siguiente línea si se desea que únicamente se lance la actividad únicamente la primera vez que la beacon es vista
//regionBootstrap.disable();
Toast.makeText(getApplicationContext(), "ENTRA EN LA REGION!!!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region arg0) {
// TODO Auto-generated method stub
Log.d(DebugTag, "Got a didExitRegion call");
Log.d(DebugTag, "Got a didExitRegion call with region:"+arg0.getUniqueId() );
}
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// TODO Auto-generated method stub
Log.d(DebugTag, "Entra en onLeScan");
Major="";
Minor="";
UUID1="";
for (int i = 0; i<scanRecord.length; i++){
if(i>8 && i<25)
UUID1 += String.format("%02x", scanRecord[i]);
else if(i>24 && i<27)
Major += String.format("%02x", scanRecord[i]);
else if(i>26 && i<29)
Minor += String.format("%02x", scanRecord[i]);
}
stop();
Log.d(DebugTag, "Got a didExitRegion call with MAJOR:"+Major+" MINOR: "+Minor+" and UUID: "+UUID1 );
}
public void stop(){
mBluetoothAdapter.stopLeScan(this);
Log.d(DebugTag+": scanLeDevice->"," REGION Stopped");
Log.d(DebugTag, "Got a didExitRegion call with MAJOR:"+Major+" MINOR: "+Minor );
}
}
在 davidgyoung 的建议下,我得到了我想要的东西,如下所示。也许我犯了错误,但它对我有用,我想获得带有 iBeacon 信息的日志:
@Override
public void onCreate() {
super.onCreate();
time=System.currentTimeMillis();
Log.d(DebugTag, "App started up");
//bluemanager= (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);
//mBluetoothAdapter = bluemanager.getAdapter();
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBeaconManager.setBackgroundScanPeriod(8000l);
mBeaconManager.setBackgroundBetweenScanPeriod(1100l);
Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
//Region region = new Region("allbeacons", Identifier.parse(UUID) , null, null);
//Region region = new Region("allbeacons", null , null, null);
backgroundPowerSaver = new BackgroundPowerSaver(this);
regionBootstrap = new RegionBootstrap(this, region);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1) {
// TODO Auto-generated method stub
Log.d(DebugTag, "Got a didDetermineStateForRegion call with region:"+arg1.toString() );
}
@Override
public void didEnterRegion(Region arg0) {
// TODO Auto-generated method stub
time=System.currentTimeMillis()-time;
Log.d(DebugTag, "Got a didEnterRegion call region:"+arg0.getUniqueId());
Log.d(DebugTag, "Got a didEnterRegion call region TIME: "+time);
if(enabler==true)
{
try {
mBeaconManager.startRangingBeaconsInRegion(arg0);
enabler=false;
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0,
Region arg1) {
// TODO Auto-generated method stub
if (arg0.size() > 0) {
Log.i(DebugTag, "The first beacon I see has UUID: "+arg0.iterator().next().getId1()+" major id:"+arg0.iterator().next().getId2()+" minor id: "+arg0.iterator().next().getId3());
}
try {
mBeaconManager.stopRangingBeaconsInRegion(arg1);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public void didExitRegion(Region arg0) {
// TODO Auto-generated method stub
Log.d(DebugTag, "Got a didExitRegion call");
Log.d(DebugTag, "Got a didExitRegion call with region:"+arg0.getUniqueId() );
enabler=true;
}
}