列出扫描的蓝牙低功耗设备时出错

时间:2016-01-28 09:58:45

标签: android

我正在开发一个Android应用程序,它在ListView中扫描并显示蓝牙低功耗设备。我的问题是我能够在ListView中只显示一个项目,即使有很多Ble设备。我使用的是ArrayList和ArrayAdapter非常必要.Below是我的代码的一部分,任何人都可以告诉我什么是我的错误以及如何我可以克服它。

public class ScanList extends Activity {
public  ListView scanDeviceList;
private  BluetoothAdapter mBluetoothAdapter;
private  Handler mHandler;
private  ArrayAdapter<BluetoothDevice> adapter1;
Context context;
Activity activity;
public  ArrayList<BluetoothDevice> devices;
public String mDeviceAddress,mDeviceName;
private static final int REQUEST_ENABLE_BT = 1;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scanned_device);
    scanDeviceList = (ListView) findViewById(R.id.scanDeviceList);
    mHandler = new Handler();
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

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

    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }


    scanLeDevice(true);
}

public  void scanLeDevice(final boolean enable) {
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);
        mScanning = true;
        mBluetoothAdapter.startLeScan(mLeScanCallback);
    } else {
        mScanning = false;
        mBluetoothAdapter.stopLeScan(mLeScanCallback);
    }
    invalidateOptionsMenu();
}

public  BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        devices = new ArrayList<BluetoothDevice>();
                        devices.add(device);
                        adapter1 = new ArrayAdapter<BluetoothDevice>(scanDeviceList.getContext(), android.R.layout.simple_list_item_1,devices);
                        scanDeviceList.setAdapter(adapter1);


                    }

                });

            }


        };
}

1 个答案:

答案 0 :(得分:0)

每次扫描时,onLeScan只返回1项(尽管许多becons广告)

devices = new ArrayList<BluetoothDevice>();

public  BluetoothAdapter.LeScanCallback mLeScanCallback =
        new BluetoothAdapter.LeScanCallback() {

            @Override
            public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        devices.add(device);
                    }
                });