2017-05-31 48 views
1

我需要找到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); 
} 

}

回答

0

只需修改這一行:

beaconManager = BeaconManager.getInstanceForApplication(getActivity()); 

要添加以下行後:

beaconManager = BeaconManager.getInstanceForApplication(getActivity()); 
beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("x,s:0-1=feaa,m:2-2=20,d:3-3,d:4-5,d:6-7,d:8-11,d:12-15")); 
beaconManager.getBeaconParsers().add(new BeaconParser(). 
      setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19")); 

我只告訴你如何添加兩個佈局(EDDYSTONE-TLM和EDDYSTONE-UID)。您可能也想添加其他人。你可以看到那些的列表,你可以在這裏補充:

https://beaconlayout.wordpress.com/

相關問題