我需要找到near.I嘗試使用此代碼:https://altbeacon.github.io/android-beacon-library/samples.html 但我收集與零。我使用nRF Connect(來自市場),她向我展示了所有的信標。 我不明白如何設置信標佈局,它是什麼?請幫我如何找到你附近的所有信標(android)?
公共類FragmentMain擴展片段實現BeaconConsumer {
FloatingActionButton fabScan;
RecyclerView recyclerView;
BeaconListAdapter beaconListAdapter;
private BeaconManager beaconManager;
private static final int REQUEST_LOCATION = 1;
protected static final String TAG = "RangingActivity";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.rcvBeaconsList);
fabScan = (FloatingActionButton) v.findViewById(R.id.fabScan);
fabScan.setOnClickListener(onClickListenerScan);
beaconListAdapter = new BeaconListAdapter(getActivity());
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(beaconListAdapter);
setHasOptionsMenu(true);
beaconManager = BeaconManager.getInstanceForApplication(getActivity());
beaconManager.setForegroundScanPeriod(5000l);
beaconManager.setBackgroundScanPeriod(5000l);
beaconManager.setForegroundBetweenScanPeriod(1100l);
beaconManager.setBackgroundBetweenScanPeriod(1100l);
beaconManager.bind(this);
return v;
}
View.OnClickListener onClickListenerScan =new View.OnClickListener() {
@Override
public void onClick(View v) {
onStartScan();
}
};
private void onStartScan(){
onBeaconServiceConnect();
}
private void checkPermis(){
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.BLUETOOTH,
},
REQUEST_LOCATION);
} else {
onBeaconServiceConnect();
}
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(final Collection<Beacon> collection, Region region) {
if(collection.size() > 0){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
beaconListAdapter.setNewData((List<Beacon>) collection);
}
});
Log.i(TAG, "The first beacon I see is about "+collection.iterator().next().getDistance()+" meters away.");}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
@Override
public Context getApplicationContext() {
return getActivity().getApplicationContext();
}
@Override
public void unbindService(ServiceConnection serviceConnection) {
getActivity().unbindService(serviceConnection);
}
@Override
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
return getActivity().bindService(intent, serviceConnection, i);
}
@Override
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
}