onLeScan不会被调用的android [英] onLeScan never gets called android

查看:3196
本文介绍了onLeScan不会被调用的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iPad,在地方其设备名称的发送蓝牙LE信息的应用程序。我试图捕捉我的Andr​​oid code信息和更新以某种方式的用户界面。基本上,我需要不断扫描设备和使用根据我的协议来更新我的Andr​​oid UI中的设备名称。我知道这是不是做的事情以正确的方式,我也打算重构这两个应用正确使用蓝牙LE,但我需要这一声做,我没有时间,现在。

在我的code下面我知道scanLeDevices获取调用由于一些日志我把,但我在onLeScan打印语句永远不会获取调用。有人能告诉我为什么吗?我一直停留在这个很长一段时间...

 公共类MainActivity延伸活动{
    私有静态最后弦乐TAG =...;
    清单<串GT;时listItems =新的ArrayList<串GT;();
    ArrayAdapter<串GT;适配器;    BluetoothManager mBluetoothManager;
    私人BluetoothAdapter mBluetoothAdapter;
    TextView的MAINVIEW;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        的setContentView(R.layout.activity_main);
        setProgressBarIndeterminate(真);        mBluetoothManager =(BluetoothManager)getSystemService(BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        MAINVIEW =(的TextView)findViewById(R.id.main_view);        scanLeDevice(真);
    }    @覆盖
    保护无效onResume(){
        super.onResume();        如果(mBluetoothAdapter == NULL ||!mBluetoothAdapter.isEnabled()){
            //蓝牙被禁用
            意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
            完();
            返回;
        }        如果(!getPackageManager()。hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)){
            Toast.makeText(这一点,不LE支持。Toast.LENGTH_SHORT).show();
            完();
            返回;
        }        scanLeDevice(真);
    }    私有静态最后长SCAN_TIME = 1000;
    布尔mScanning = FALSE;
    私人无效scanLeDevice(最终布尔启用){
        Log.i(NULL,内部scanLeDevice);
        如果(启用){
            //一个pre定义的扫描时间后停止扫描。
            mHandler.postDelayed(新的Runnable(){
                @覆盖
                公共无效的run(){
                    mScanning = FALSE;
                    Log.i(NULL,叫stopLeScan);
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            },SCAN_TIME);            mScanning = TRUE;
            Log.i(NULL,叫startLeScan);
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        }其他{
            mScanning = FALSE;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }    私人BluetoothAdapter.LeScanCallback mLeScanCallback =
            新BluetoothAdapter.LeScanCallback(){
        @覆盖
        公共无效onLeScan(最终BluetoothDevice类设备,INT RSSI,
                字节[] scanRecord){
            runOnUiThread(新的Runnable(){
               @覆盖
               公共无效的run(){
                   Log.i(NULL,INSIDE ONLESCAN);
                   // DO我的工作
               }
           });
       }
    };
}


解决方案

我不能肯定地说什么是错的,但这里有一些想法:


  1. 测试startLeScan的返回值()。如果startLeScan()返回false,扫描没有启动,可能是由于权限问题。见的http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions

  2. 请SCAN_TIME更长。例如SCAN_TIME = 10000如果SCAN_TIME比你烽火台的广播间隔越短,你的程序可能无法检测到它。

  3. 删除)从的onCreate调用scanLeDevice(()。正因为如此,这个程序启动时,的onCreate()调用scanLeDevice(),然后onResume()被几乎立即调用,()再次呼吁scanLeDevice。我不知道如果你调用startLeScan(当扫描已运行)会发生什么,但我认为这将是很好的避免这样做。

  4. 重新启动您的Andr​​oid设备。我发现这BLE发展,当我的BLE的应用程序停止工作。
  5. 中是有益的
  6. 测试另一个BLE检测应用可看到您的BLE设备。

I have an app on an iPad that sends out Bluetooth LE information in place of its device name. I'm trying to capture that information in my Android code and update the UI in some way. Basically I need to scan for devices continuously and use the "device name" according to my protocol to update my Android UI. I know this isn't the right way to do things and I do plan to refactor both apps to correctly use Bluetooth LE, but I need this done soon and I don't have the time right now.

In my code below I know that scanLeDevices is getting called due to some Logs I put in, but the print statement I have in onLeScan is never getting called. Can anybody please tell me why? I've been stuck on this for a long time...

public class MainActivity extends Activity {
    private static final String TAG = "...";
    List<String> listItems=new ArrayList<String>();
    ArrayAdapter<String> adapter;

    BluetoothManager mBluetoothManager;
    private BluetoothAdapter mBluetoothAdapter;
    TextView mainView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setProgressBarIndeterminate(true);

        mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
        mBluetoothAdapter = mBluetoothManager.getAdapter();
        mainView = (TextView) findViewById(R.id.main_view);

        scanLeDevice(true);     
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            //Bluetooth is disabled
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(enableBtIntent);
            finish();
            return;
        }

        if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            Toast.makeText(this, "No LE Support.", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        scanLeDevice(true);
    }

    private static final long SCAN_TIME = 1000;
    boolean mScanning = false;
    private void scanLeDevice(final boolean enable) {
        Log.i(null, "Inside scanLeDevice");
        if (enable) {
            // Stops scanning after a pre-defined scan period.
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    Log.i(null, "Calling stopLeScan");
                    mBluetoothAdapter.stopLeScan(mLeScanCallback);
                }
            }, SCAN_TIME);

            mScanning = true;
            Log.i(null, "Calling startLeScan");
            mBluetoothAdapter.startLeScan(mLeScanCallback);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(mLeScanCallback);
        }
    }

    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, int rssi,
                byte[] scanRecord) {
            runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Log.i(null, "INSIDE ONLESCAN");
                   //DO MY WORK
               }
           });
       }
    };
}

解决方案

I can't say for certain what's wrong, but here are a few ideas:

  1. test the return value of startLeScan(). If startLeScan() returns false, the scan didn't start, likely due to permissions problems. See http://developer.android.com/guide/topics/connectivity/bluetooth-le.html#permissions
  2. make SCAN_TIME longer. For example SCAN_TIME = 10000. If SCAN_TIME is shorter than the broadcast interval of your Beacon, your program might not detect it.
  3. remove the call to scanLeDevice() from onCreate(). As it is, when this program starts, onCreate() calls scanLeDevice(), then onResume() is called almost immediately, and calls scanLeDevice() again. I don't know what happens if you call startLeScan() when a scan is already running, but I think it would be good to avoid doing that.
  4. Reboot your Android device. I've found this to be helpful during BLE development, when my BLE app stops working.
  5. Test that another BLE detection app can see your BLE device.

这篇关于onLeScan不会被调用的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆