空的nearables列出estimote-sdk

时间:2015-11-11 09:23:55

标签: android bluetooth estimote beacon

我是estimote sdk的新手,使用android studio。我在家里有一个灯塔,我想继续工作,用sdk获得一些流量。 使用小米与android 4.4.4 api19

我遇到了奇怪的问题,我的手机发现无法忍受,但实际的可用物品清单是空的。用了一段时间用Google搜索,没有人有类似的问题。它可能是一件非常简单的事情,我现在无法弄清楚。你知道吗?启用了蓝牙功能,只有一个信标靠近我,黑客上蓝牙因信标过载而被压碎。

我的代码

package com.example.tom.test;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Nearable;
import com.estimote.sdk.Region;

import java.util.List;


public class MainActivity extends ActionBarActivity {

    private BeaconManager beaconManager;
    private String scanId;
    private Nearable currentNearable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beaconManager = new BeaconManager(getApplicationContext());
        setContentView(R.layout.activity_main);
        beaconManager.setNearableListener(new BeaconManager.NearableListener() {
            @Override
            public void onNearablesDiscovered(List<Nearable> listOfNearables) {
                Log.d(null, "nearable discovered");
                Log.d(null, "size of list is " + String.valueOf(listOfNearables.size()));
                int maxPower = 0;
                if (!listOfNearables.isEmpty()) {
                    Log.d(null, "in");
                    maxPower = listOfNearables.get(0).power.powerInDbm;
                    currentNearable = listOfNearables.get(0);
                    for (Nearable nearable : listOfNearables) {
                        if (maxPower < nearable.power.powerInDbm) {
                            maxPower = nearable.power.powerInDbm;
                            currentNearable = nearable;
                        }
                    }
                    displayCurrentNearableInfo();
            }
        }
        });
    }

    @Override
    protected void onStart() {
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                scanId = beaconManager.startNearableDiscovery();
            }
        });
        super.onStart();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        beaconManager.disconnect();
        super.onDestroy();
    }
    @Override protected void onStop() {
        beaconManager.stopNearableDiscovery(scanId);
        super.onStop();
    }

    private void displayCurrentNearableInfo() {
        if(currentNearable != null){
        StringBuilder builder = new StringBuilder()
                .append("temperature: ").append(currentNearable.temperature).append("\n")
                .append("contents: ").append(currentNearable.describeContents())
                .append("power: " ).append(currentNearable.power.powerInDbm);
        TextView infoText = (TextView) findViewById(R.id.textView);
        infoText.setText(builder.toString());
        }
    }
}

控制台

11-11 10:18:19.750    5940-6027/com.example.tom.test D/BluetoothAdapter﹕ startLeScan(): null
11-11 10:18:19.750    5940-5952/com.example.tom.test D/BluetoothAdapter﹕ onClientRegistered() - status=0 clientIf=5
11-11 10:18:20.780    5940-6027/com.example.tom.test D/BluetoothAdapter﹕ stopLeScan()
11-11 10:18:20.820    5940-5940/com.example.tom.test D/﹕ nearable discovered
11-11 10:18:20.820    5940-5940/com.example.tom.test E/﹕ size of list is 0

0 个答案:

没有答案