所有的信标不使用的Android库altBeacon所示 [英] All beacons are not shown in Android using altBeacon library
问题描述
我使用 AltBEacon的Android库以开发Android的 iBeacon显示应用设备。我在扫描但是,只有 2出4信标发现标(有时1/4)。我增加 mBeaconManager.setForegroundScanPeriod(5000升);
来5秒,但仍然相同的结果。我不知道,如果 CustomAdapter
我用绑定细节观点是错误或问题是做移动设备(我用的Galaxy Note II - 4.4.2) ? 任何人都可以找到我的错误?
另一个问题是,从设备的距离计算到信标返回不正确(当我约0.5米
程返回betweem 3 -6M
)
我在做什么错了?任何帮助appriciated
的注:我已经检查UUID,主要,灯塔的未成年人任何错误。我已经增加了这些扫描time.None工作。的
这里是开始监视和测距的片段:
公共类FragmentScanBeacons扩展片段实现BeaconConsumer { 公共静态区域mRegion =新的区域(服务器,Identifier.parse(这是我的UUID),NULL,NULL);
私人BeaconManager mBeaconManager; 私人BeaconBaseAdapter beaconBaseAdapter; 私人的ListView beaconsListLv; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mBeaconManager = BeaconManager.getInstanceForApplication(getActivity());
// BEACON PARSER
。mBeaconManager.getBeaconParsers()增加(新BeaconParser()setBeaconLayout(M:2-3 = 0215,I:4-19,I:20-21,I:22-23页:24-24)) ;
// mBeaconManager.debug = TRUE;
beaconBaseAdapter =新BeaconBaseAdapter(getActivity());
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){ 查看查看= inflater.inflate(R.layout.fragment_beacons,集装箱,FALSE); // UI
beaconsListLv =(ListView控件)view.findViewById(R.id.beaconsListView); //设置适配器
beaconsListLv.setAdapter(beaconBaseAdapter); //检查蓝牙和扫描灯塔
verifyBluetooth(); //开始监测和测距
mBeaconManager.bind(本); 返回视图。
} @覆盖
公共无效onResume(){
super.onResume();
如果(mBeaconManager.isBound(本)){
mBeaconManager.setBackgroundMode(假);
}
} @覆盖
公共无效的onPause(){
super.onPause();
如果(mBeaconManager.isBound(本)){
mBeaconManager.setBackgroundMode(真);
}
} @覆盖
公共无效的onDestroy(){
super.onDestroy();
mBeaconManager.unbind(本);
} @覆盖
公共无效onBeaconServiceConnect(){
尝试{
//扫描持续时间SCAN_PERIOD
mBeaconManager.setForegroundScanPeriod(千升);
// mBeaconManager.setBackgroundScanPeriod(0升);
//等待每一次SCAN_PERIOD_INBETWEEN
mBeaconManager.setForegroundBetweenScanPeriod(0升);
//更新默认的时间用新的
mBeaconManager.updateScanPeriods();
}赶上(RemoteException的E){
e.printStackTrace();
} //设置监测
mBeaconManager.setMonitorNotifier(新MonitorNotifier(){
@覆盖
公共无效didEnterRegion(地区区域){
Log.d(TEST,输入的航标区);
//开始Raning只要你发现一盏明灯
尝试{
mBeaconManager.startRangingBeaconsInRegion(mRegion);
}赶上(RemoteException的E){
e.printStackTrace();
}
} @覆盖
公共无效didExitRegion(地区区域){
Log.d(TEST,烽火EXITED区);
} @覆盖
公共无效didDetermineStateForRegion(INT国家,地区的区域){
Log.d(TEST,从看到/没有看到灯塔切换到国家+状态);
}
}); //设置测距
mBeaconManager.setRangeNotifier(新RangeNotifier(){
@覆盖
公共无效didRangeBeaconsInRegion(最终收集和LT;烽火台>标,地区区){
如果(信标=空&放大器;!&放大器; beacons.size()大于0){
getActivity()。runOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
beaconBaseAdapter.initAll(信标);
}
});
}
}
}); 尝试{
//开始监测
mBeaconManager.startMonitoringBeaconsInRegion(mRegion);
}赶上(RemoteException的E){
e.printStackTrace();
} } @覆盖
公共语境getApplicationContext(){
返回getActivity()getApplicationContext()。
} @覆盖
公共无效unbindService(ServiceConnection serviceConnection){
。getActivity()unbindService(serviceConnection);
} @覆盖
公共布尔bindService(意向意图,ServiceConnection serviceConnection,INT模式){
返回getActivity()bindService(意向,serviceConnection,模式)。
}
}
这里是自定义适配器:
公共类BeaconBaseAdapter延伸BaseAdapter { 私人语境myContext; 私人LayoutInflater吹气;
公共静态的ArrayList<&警号GT;信标; 公共BeaconBaseAdapter(上下文的背景下){
this.myContext =背景;
this.inflater = LayoutInflater.from(上下文);
this.beacons =新的ArrayList<&警号GT;();
} 公共无效initAll(收集和LT;烽火台> newBeacons){
this.beacons.clear();
this.beacons.addAll(newBeacons);
notifyDataSetChanged();
} @覆盖
公众诠释的getCount(){
返回beacons.size();
} @覆盖
公共灯塔的getItem(INT位置){
返回beacons.get(位置);
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
如果(convertView == NULL){
convertView = inflater.inflate(R.layout.beacon_list_row,NULL);
convertView.setTag(新ViewHolder(convertView));
} 绑定(的getItem(位置),位置,convertView);
返回convertView;
}
私人无效绑定(灯塔航标,INT位置,查看视图){
ViewHolder支架=(ViewHolder)view.getTag(); holder.manufacturerTextView.setText(制造商:+ beacon.getManufacturer());
holder.idOneTextView.setText(UUID:+ beacon.getId1());
holder.idTwoTextView.setText(专业:+ beacon.getId2());
holder.idThreeTextView.setText(小+ beacon.getId3());
holder.txPowerTextView.setText(TX-功率:+ beacon.getTxPower());
holder.rssiTextView.setText(RSSI:+ beacon.getRssi());
holder.distanceTextView.setText(的String.format(距离:(%.2f M),beacon.getDistance()));
holder.nameTextView.setText(蓝牙名称:+ beacon.getBluetoothName());
holder.addressTextView.setText(蓝牙ADRS:+ beacon.getBluetoothAddress()); } 静态类ViewHolder {
最终的TextView nameTextView;
最终的TextView manufacturerTextView;
最终的TextView idOneTextView;
最终的TextView idTwoTextView;
最终的TextView idThreeTextView;
最终的TextView txPowerTextView;
最终的TextView rssiTextView;
最终的TextView distanceTextView;
最终的TextView addressTextView; ViewHolder(查看视图){
nameTextView =(TextView中)view.findViewWithTag(名称);
manufacturerTextView =(TextView的)view.findViewWithTag(制造商);
idOneTextView =(TextView中)view.findViewWithTag(id_one);
idTwoTextView =(TextView中)view.findViewWithTag(id_two);
idThreeTextView =(TextView中)view.findViewWithTag(id_three);
txPowerTextView =(TextView中)view.findViewWithTag(TX_POWER);
rssiTextView =(TextView的)view.findViewWithTag(RSSI);
distanceTextView =(TextView的)view.findViewWithTag(距离);
addressTextView =(TextView中)view.findViewWithTag(地址);
}
}
}
好吧,我终于解决了这个问题。我按照信标 MAC地址
订购。因此,而不是:
公共无效initAll(收集和LT;烽火台> newBeacons){
this.beacons.clear();
this.beacons.addAll(newBeacons);
}
我所做的:
公共无效initAll(收集和LT;烽火台> newBeacons){
this.beacons.clear();
this.beacons.addAll(newBeacons);
//分类
Collections.sort(this.beacons,新的比较<&警号GT;(){
@覆盖
公众诠释比较(灯塔B1,B2灯塔){
串MAC1 = b1.getBluetoothAddress();
串MAC2 = b2.getBluetoothAddress(); 返回mac1.compareTo(MAC2);
}
});
}
I am using AltBEacon Android library for developing an iBeacon app for Android Devices. I am scanning for beacons however only 2 out of 4 beacons are found(sometimes 1/4). I increasemBeaconManager.setForegroundScanPeriod(5000l);
to 5sec but still same result. I am not sure if the CustomAdapter
i use for binding details to view is wrong or the issue is do to the mobile device (I am using Galaxy Note II - 4.4.2)? Can anyone locate my mistake?
Another issue is that the distance calculation from device to the beacon is returning not correct (when i am approx 0.5m
away it returns betweem 3-6m
)
What i am doing wrong? Any help is appriciated
Note: I have checked UUID, Major, Minors of Beacon for any mistake. I have increased the scanning time.None of these worked.
Here is the Fragment that starts monitoring and ranging:
public class FragmentScanBeacons extends Fragment implements BeaconConsumer{
public static Region mRegion = new Region("Server", Identifier.parse("Here is my UUID"), null, null);
private BeaconManager mBeaconManager;
private BeaconBaseAdapter beaconBaseAdapter;
private ListView beaconsListLv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBeaconManager = BeaconManager.getInstanceForApplication(getActivity());
//BEACON PARSER
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
// mBeaconManager.debug = true;
beaconBaseAdapter = new BeaconBaseAdapter(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_beacons, container, false);
//UI
beaconsListLv = (ListView) view.findViewById(R.id.beaconsListView);
//Set Adapter
beaconsListLv.setAdapter(beaconBaseAdapter);
//Check for bluetooth and Scan for Beacon
verifyBluetooth();
//Start Monitoring and Ranging
mBeaconManager.bind(this);
return view;
}
@Override
public void onResume() {
super.onResume();
if(mBeaconManager.isBound(this)){
mBeaconManager.setBackgroundMode(false);
}
}
@Override
public void onPause() {
super.onPause();
if(mBeaconManager.isBound(this)){
mBeaconManager.setBackgroundMode(true);
}
}
@Override
public void onDestroy() {
super.onDestroy();
mBeaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
try {
//Scan lasts for SCAN_PERIOD time
mBeaconManager.setForegroundScanPeriod(1000l);
// mBeaconManager.setBackgroundScanPeriod(0l);
//Wait every SCAN_PERIOD_INBETWEEN time
mBeaconManager.setForegroundBetweenScanPeriod(0l);
//Update default time with the new one
mBeaconManager.updateScanPeriods();
}catch (RemoteException e){
e.printStackTrace();
}
//Set Monitoring
mBeaconManager.setMonitorNotifier(new MonitorNotifier() {
@Override
public void didEnterRegion(Region region) {
Log.d("TEST", "ENTERED beacon region");
//Start Raning as soon as you detect a beacon
try {
mBeaconManager.startRangingBeaconsInRegion(mRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didExitRegion(Region region) {
Log.d("TEST", "EXITED beacon region");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d("TEST", "SWITCHED from seeing/not seeing beacon to state " + state);
}
});
//Set Ranging
mBeaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
if (beacons != null && beacons.size() > 0) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
beaconBaseAdapter.initAll(beacons);
}
});
}
}
});
try {
//Start Monitoring
mBeaconManager.startMonitoringBeaconsInRegion(mRegion);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@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 mode) {
return getActivity().bindService(intent, serviceConnection, mode);
}
}
and here is the custom adapter:
public class BeaconBaseAdapter extends BaseAdapter {
private Context myContext;
private LayoutInflater inflater;
public static ArrayList<Beacon> beacons;
public BeaconBaseAdapter(Context context) {
this.myContext = context;
this.inflater = LayoutInflater.from(context);
this.beacons = new ArrayList<Beacon>();
}
public void initAll(Collection<Beacon> newBeacons) {
this.beacons.clear();
this.beacons.addAll(newBeacons);
notifyDataSetChanged();
}
@Override
public int getCount() {
return beacons.size();
}
@Override
public Beacon getItem(int position) {
return beacons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.beacon_list_row, null);
convertView.setTag(new ViewHolder(convertView));
}
bind(getItem(position), position, convertView);
return convertView;
}
private void bind(Beacon beacon, int position, View view) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.manufacturerTextView.setText("Manufacturer: " + beacon.getManufacturer());
holder.idOneTextView.setText("UUID: " + beacon.getId1());
holder.idTwoTextView.setText("Major: " + beacon.getId2());
holder.idThreeTextView.setText("Minor: " + beacon.getId3());
holder.txPowerTextView.setText("TX-Power: " + beacon.getTxPower());
holder.rssiTextView.setText("RSSI: " + beacon.getRssi());
holder.distanceTextView.setText(String.format("DISTANCE: (%.2f m)", beacon.getDistance()));
holder.nameTextView.setText("Bluetooth Name: " + beacon.getBluetoothName());
holder.addressTextView.setText("Bluetooth Adrs: " + beacon.getBluetoothAddress());
}
static class ViewHolder {
final TextView nameTextView;
final TextView manufacturerTextView;
final TextView idOneTextView;
final TextView idTwoTextView;
final TextView idThreeTextView;
final TextView txPowerTextView;
final TextView rssiTextView;
final TextView distanceTextView;
final TextView addressTextView;
ViewHolder(View view) {
nameTextView = (TextView) view.findViewWithTag("name");
manufacturerTextView = (TextView) view.findViewWithTag("manufacturer");
idOneTextView = (TextView) view.findViewWithTag("id_one");
idTwoTextView = (TextView) view.findViewWithTag("id_two");
idThreeTextView = (TextView) view.findViewWithTag("id_three");
txPowerTextView = (TextView) view.findViewWithTag("tx_power");
rssiTextView = (TextView) view.findViewWithTag("rssi");
distanceTextView = (TextView) view.findViewWithTag("distance");
addressTextView = (TextView) view.findViewWithTag("address");
}
}
}
Ok i finally solved it. I ordered according to the beacon Mac Address
. So instead of:
public void initAll(Collection<Beacon> newBeacons) {
this.beacons.clear();
this.beacons.addAll(newBeacons);
}
i did:
public void initAll(Collection<Beacon> newBeacons) {
this.beacons.clear();
this.beacons.addAll(newBeacons);
//Sort
Collections.sort(this.beacons, new Comparator<Beacon>() {
@Override
public int compare(Beacon b1, Beacon b2) {
String mac1 = b1.getBluetoothAddress();
String mac2 = b2.getBluetoothAddress();
return mac1.compareTo(mac2);
}
});
}
这篇关于所有的信标不使用的Android库altBeacon所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!