我需要找到附近的所有信标 . 我尝试使用以下代码:https://altbeacon.github.io/android-beacon-library/samples.html但我的集合为零 . 我使用nRF Connect(来自市场),她向我展示了所有信标 . 我不明白如何设置Beacon Layout,它是什么?请帮我
public class FragmentMain extends Fragment实现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);
}
}
2 回答
只需修改此行:
之后添加以下行:
我只向您展示了如何添加两个布局(EDDYSTONE-TLM和EDDYSTONE-UID的布局) . 您可能也想添加其他内容 . 您可以在此处看到可以添加的列表:
https://beaconlayout.wordpress.com/
您肯定需要使用布局管理器 . 您使用的是Eddystone,iBeacon还是什么样的信标?我在这里写了一个例子,看看它:https://solidgeargroup.com/beacons-on-android