簡體   English   中英

android上的ibeacon掃描結果從未顯示在智能手機上

[英]scanning result of ibeacon on android never shown on the smartphone

我正在嘗試使用Android智能手機檢測ibeacon。 我從一家提供android庫的公司購買了ibeacon設備,以使它們能夠正常工作(此庫很像AltBeacon的android beacon庫,例如我使用的代碼)。 這是MainActivity

public class MainActivity extends Activity implements IBeaconConsumer {

private static final String TAG = "BB-EXAPP";

// iBeacon bluetooth scanning parameters
private static final int FOREGROUND_SCAN_PERIOD = 1000;
private static final int FOREGROUND_BETWEEN_SCAN_PERIOD = 1000;
private static final int BACKGROUND_SCAN_PERIOD = 250;
private static final int BACKGROUND_BETWEEN_SCAN_PERIOD = 2000;

// iBeacon Library Stuff
private static final Region blueupRegion = new Region("BlueUp", "acfd065e-c3c0-11e3-9bbe-1a514932ac01", null, null);
private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this);
private Intent iBeaconService;
private boolean isMonitoring = false;
private boolean isRanging = false;

// Android BLE Stuff
private BluetoothAdapter mBluetoothAdapter;
private static final int REQUEST_ENABLE_BT = 1;


// UI Stuff
private List<IBeacon> beacons;
private ListView listView;
private IBeaconListAdapter listAdapter;


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

    // Initializes Bluetooth adapter
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    // Ensures Bluetooth is available on the device and it is enabled. If not,
    // displays a dialog requesting user permission to enable Bluetooth.
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }

    // Initializes iBeacon Service
    iBeaconService = new Intent(getApplicationContext(), IBeaconService.class);

    // Start the iBeacon Service
    Log.d(TAG, "Starting service: iBeaconService");
    startService(iBeaconService);

    // Set desired scan periods
    iBeaconManager.setForegroundScanPeriod(FOREGROUND_SCAN_PERIOD);
    iBeaconManager.setForegroundBetweenScanPeriod(FOREGROUND_BETWEEN_SCAN_PERIOD);
    iBeaconManager.setBackgroundScanPeriod(BACKGROUND_SCAN_PERIOD);
    iBeaconManager.setBackgroundBetweenScanPeriod(BACKGROUND_BETWEEN_SCAN_PERIOD);

    // Bind the iBeacon Service
    iBeaconManager.bind(this);

    //
    // UI Initialization
    //

    // Create Empty IBeacons List
    beacons = new ArrayList<IBeacon>();

    // Create List Adapter
    listAdapter = new IBeaconListAdapter(this, beacons);

    // Get ListView
    listView = (ListView)findViewById(R.id.listView);

    // Set ListAdapter
    listView.setAdapter(listAdapter);
}

@Override
public void onIBeaconServiceConnect() {
    Log.d(TAG, "onIBeaconServiceConnect");

    // Set Monitor Notifier
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {

        @Override
        public void didExitRegion(Region region) {
            Log.d(TAG, "didExitRegion: region = " + region.toString());
        }

        @Override
        public void didEnterRegion(Region region) {

            Log.d(TAG, "didEnterRegion: region = " + region.toString());

            // Set Range Notifier
            iBeaconManager.setRangeNotifier(new RangeNotifier() {

                @Override
                public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
                    // Update UI iBeacons List
                    beacons = new ArrayList<IBeacon>(iBeacons);
                    listAdapter.notifyDataSetChanged();

                    // Log found iBeacons
                    Log.d(TAG, "didRangeBeaconsInRegion: region = " + region.toString());
                    if (!iBeacons.isEmpty()) {
                        int j = 0;
                        for (IBeacon beacon : iBeacons) {
                            Log.d(TAG, "  [" + j + "] (Major = " + beacon.getMajor() + ", Minor = " + beacon.getMinor() + ", RSSI = " + beacon.getRssi() + ", Accuracy = " + beacon.getAccuracy() + ")");
                            j++;
                        }
                    }
                }

            });

            // Start Ranging
            try {
                iBeaconManager.startRangingBeaconsInRegion(blueupRegion);
                isRanging = true;
                Log.d(TAG, "startRangingBeaconsInRegion: region = " + blueupRegion.toString());
            } catch (RemoteException e) {
                Log.d(TAG, "startRangingBeaconsInRegion [RemoteException]");
                e.printStackTrace();
            }

        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            Log.d(TAG, "didDetermineStateForRegion: state = " + state + ", region = " + region.toString());
        }

    });


    // Start Monitoring
    try {
        iBeaconManager.startMonitoringBeaconsInRegion(blueupRegion);
        isMonitoring = true;
        Log.d(TAG, "startMonitoringBeaconsInRegion: region = " + blueupRegion.toString());
    } catch (RemoteException e) {
        Log.d(TAG, "startMonitoringBeaconsInRegion [RemoteException]");
        e.printStackTrace();
    }
}

@Override
public void onResume() {
    Log.d(TAG, "onResume");
    super.onResume();
    if (iBeaconManager.isBound(this)) {
        iBeaconManager.setBackgroundMode(this, false);
        Log.d(TAG, "iBeaconManager.setBackgroundMode = false");
    }
}

@Override
public void onStop() {
    Log.d(TAG, "onStop");
    if (iBeaconManager.isBound(this)) {
        iBeaconManager.setBackgroundMode(this, true);
        Log.d(TAG, "iBeaconManager.setBackgroundMode = true");
    }
    super.onStop();
}

@Override
public void onDestroy() {
    Log.d(TAG, "onDestroy");

    if (isRanging) {
        try {
            iBeaconManager.stopRangingBeaconsInRegion(blueupRegion);
            Log.d(TAG, "stopRangingBeaconsInRegion: region = " + blueupRegion.toString());
        } catch (RemoteException e) {
            Log.d(TAG, "stopRangingBeaconsInRegion [RemoteException]");
            e.printStackTrace();
        }
    }

    if (isMonitoring) {
        try {
            iBeaconManager.stopMonitoringBeaconsInRegion(blueupRegion);
            Log.d(TAG, "stopMonitoringBeaconsInRegion: region = " + blueupRegion.toString());
        } catch (RemoteException e) {
            Log.d(TAG, "stopMonitoringBeaconsInRegion [RemoteException]");
            e.printStackTrace();
        }
    }

    if (iBeaconManager.isBound(this)) {
        Log.d(TAG, "Unbinding iBeaconManager");
        iBeaconManager.unBind(this);
    }

    Log.d(TAG, "Stopping service: iBeaconService");
    stopService(iBeaconService);

    super.onDestroy();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

還有第二項活動可以列出該清單:

public class IBeaconListAdapter extends BaseAdapter {

private Activity activity;
private List<IBeacon> beacons;
private static LayoutInflater inflater = null;

public IBeaconListAdapter(Activity _activity, List<IBeacon> _beacons) {
    this.activity = _activity;
    this.beacons = _beacons;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
    return beacons.get(position);
}

@Override
public long getItemId(int position) {
    /*IBeacon beacon = beacons.get(position);
    if (beacon != null) {
        return beacon.hashCode();
    }*/
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;

    if (convertView == null) {
        view = inflater.inflate(R.layout.beacon_list_row, null);
    }

    TextView majorTextView = (TextView)view.findViewById(R.id.majorValue);
    TextView minorTextView = (TextView)view.findViewById(R.id.minorValue);
    TextView rssiTextView = (TextView)view.findViewById(R.id.rssiValue);
    TextView accuracyTextView = (TextView)view.findViewById(R.id.accuracyValue);

    IBeacon beacon = beacons.get(position);

    if (beacon != null) {
        DecimalFormat df = new DecimalFormat("#.0");
        majorTextView.setText(beacon.getMajor());
        minorTextView.setText(beacon.getMinor());
        rssiTextView.setText(beacon.getRssi() + " dBm");
        accuracyTextView.setText(df.format(beacon.getAccuracy()) + " m");
    }

    return view;
}

}

當我在電話上運行該應用程序時,從LogCat中可以看到它檢測到設備,而在電話上我只能看到空白頁! 我從LogCat中注意到,一旦調用onIBeaconServiceConnect,該應用程序將跳轉到startMonitoringBeaconsInRegion,並且從不調用didEnterRegion方法,該方法包括didRangeBeaconsInRegion和用於填充列表的代碼。 我找不到類似我的問題的其他答案,而且我真的不知道我的錯誤在哪里。

一些想法:

  1. 確保您的信標確實正在傳輸,並具有您認為的信標。 嘗試下載“ 定位”之類的現成應用程序,並確保它可以看到它們。

  2. 您的代碼設置了一個正在尋找信標的區域,該信標的UUID為acfd065e-c3c0-11e3-9bbe-1a514932ac01 您確定這是正確的嗎? 嘗試將區域定義替換為Region("BlueUp", null, null, null); 因此無論標識符如何,它都能檢測到任何區域。

  3. 如果您打算進行重大開發,強烈建議您升級到Android Beacon Library 2.0 該庫所基於的舊0.x庫版本已不再受支持或維護,並且當出現此類問題時,將很難找到幫助。

這些是LogCat中的重要行。

D/BB-EXAPP(7756): onIBeaconServiceConnect
D/BB-EXAPP(7756): startMonitoringBeaconsInRegion: region = proximityUuid: acfd065e-c3c0-11e3-9bbe-1a514932ac01 major: null minor:null
D/AbsListView(7756): unregisterIRListener() is called 
I/IBeaconService(7756): start monitoring received
D/BluetoothAdapter(7756): startLeScan(): null
D/BluetoothAdapter(7756): onClientRegistered() - status=0 clientIf=4
I/IBeaconService(7756): Adjusted scanStopTime to be Sat Jan 17 12:09:58 CET 2015
D/AbsListView(7756): unregisterIRListener() is called 
D/BluetoothAdapter(7756): onScanResult() - Device=DA:0D:22:4B:40:17 RSSI=-59
D/Callback(7756): attempting callback via intent: ComponentInfo{com.android.appbeacon/com.blueup.libbeacon.IBeaconIntentProcessor}
D/BluetoothAdapter(7756): onScanResult() - Device=DA:0D:22:4B:40:17 RSSI=-64
I/IBeaconService(7756): iBeacon detected multiple times in scan cycle :acfd065e-c3c0-11e3-9bbe-1a514932ac01 0 0 accuracy: 0.7351236323265571 proximity: 2
D/BluetoothAdapter(7756): stopLeScan() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM