如何使用altbeacon库解决ibeacon列表上的android bug?

时间:2019-04-23 07:03:49

标签: android ibeacon altbeacon ibeacon-android

我正在使用altbeacon库在android上扫描ibeacon设备。我有2个ibeacon设备。我已经可以扫描ibeacon,但这不在列表中。它只显示1个设备,但我想显示2个设备。

这是我的代码(MainActivity)

public class MainActivity extends AppCompatActivity implements BeaconConsumer {

protected static final String TAG = "MainActivity";
private BeaconManager beaconManager;

private BluetoothAdapter bluetoothAdapter;
private int REQUEST_ENABLE_BT = 1;
private int REQUEST_ENABLE_LOCATION = 1;

private List<Beacon> beaconDevice;
private RecyclerView rv;
private BeaconAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();
    }

    //bluetooth manager
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothAdapter = bluetoothManager.getAdapter();

    if (ActivityCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
            MainActivity.this, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{ACCESS_FINE_LOCATION}, REQUEST_ENABLE_LOCATION);
        return;
    }

    final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE );

    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        enableGPS();

    //beacon library
    beaconManager = BeaconManager.getInstanceForApplication(this);
    // To detect proprietary beacons, you must add a line like below corresponding to your beacon
    // type.  Do a web search for "setBeaconLayout" to get the proper expression.
     beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind(this);

    //ui
    rv = findViewById(R.id.recyclerview);
    rv.setLayoutManager(new LinearLayoutManager(this));

}

private void enableGPS() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
            .setCancelable(false)
            .setPositiveButton("Enable",
                    new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int id){
                            Intent callGPSSettingIntent = new Intent(
                                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(callGPSSettingIntent);
                        }
                    });
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
                    dialog.cancel();
                }
            });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

@Override
protected void onResume() {
    super.onResume();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.removeAllRangeNotifiers();
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                beaconDevice = new ArrayList<>(beacons);
                adapter = new BeaconAdapter(getApplicationContext(),beaconDevice);
                rv.setAdapter(adapter);
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {
        Toast.makeText(MainActivity.this, "error: "+e, Toast.LENGTH_LONG).show();

    }
}

适配器

public class BeaconAdapter extends RecyclerView.Adapter<BeaconAdapter.BeaconViewHolder> {

private Context context;
private List<Beacon> beacons;

public BeaconAdapter(Context context, List<Beacon> beacons) {
    this.context = context;
    this.beacons = beacons;
}

@NonNull
@Override
public BeaconViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.beacon_item, viewGroup, false);
    return new BeaconViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull BeaconViewHolder holder, int position) {
    Beacon beacon = beacons.get(position);
    holder.uuid.setText(String.valueOf(beacon.getId1()));
    holder.major.setText(String.valueOf("major: "+beacon.getId2()));
    holder.minor.setText(String.valueOf("minor: "+beacon.getId3()));
    holder.distance.setText(String.valueOf("distance: "+beacon.getDistance()+" m"));
    holder.mac.setText(beacon.getBluetoothAddress());
    holder.rssi.setText("RSSI: " + beacon.getRssi());
}

@Override
public int getItemCount() {
    return beacons.size();
}

public class BeaconViewHolder extends RecyclerView.ViewHolder{

    private TextView uuid,major,minor,distance,rssi,mac;

    public BeaconViewHolder(@NonNull View itemView) {
        super(itemView);

        uuid = itemView.findViewById(R.id.uuid);
        distance = itemView.findViewById(R.id.distance);
        major = itemView.findViewById(R.id.major);
        minor = itemView.findViewById(R.id.minor);
        rssi = itemView.findViewById(R.id.rssi);
        mac = itemView.findViewById(R.id.mac);
    }
}

我认为我上面的代码很好。但是当我运行该应用程序时,它只显示了1张这样的设备 beacon 1 beacon 2

如何使扫描仪一次显示我的2个ibeacon设备?

0 个答案:

没有答案