1

我是使用 android studio 估计 sdk 的新手。我在家里有一个信标,我想对它进行工作,使用 sdk 获得一些流量。将小米与 android 4.4.4 api19 一起使用

我遇到了奇怪的问题,我的手机发现了可接近但实际的可接近列表是空的。谷歌搜索了一段时间,没有人遇到类似的问题。这可能是我现在无法弄清楚的非常简单的事情。你有什么想法吗?启用了蓝牙,只有一个信标靠近我,在黑客松蓝牙由于信标过载而崩溃。

我的代码

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
4

0 回答 0