Search in sources :

Example 1 with BleDeviceAdapter

use of com.afunx.ble.blelitelib.adapter.BleDeviceAdapter in project BleLiteLib4android by afunx.

the class ScanActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan);
    mHandler = new Handler();
    mListView = (ListView) findViewById(R.id.lv_devices);
    mBleDeviceAdapter = new BleDeviceAdapter(this);
    mListView.setAdapter(mBleDeviceAdapter);
    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            BleDevice bleDevice = (BleDevice) mBleDeviceAdapter.getItem(position);
            // ServiceListActivity.startActivity(ScanActivity.this, bleDevice.getBluetoothDevice());
            TestActivity.startActivity(ScanActivity.this, bleDevice.getBluetoothDevice());
        }
    });
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            doRefresh();
        }
    });
    mLeScanCallback = new BluetoothAdapter.LeScanCallback() {

        private long timestamp = 0;

        private final long interval = 200;

        @Override
        public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
            BleDevice bleDevice = new BleDevice();
            bleDevice.setBluetoothDevice(device);
            bleDevice.setRssi(rssi);
            bleDevice.setScanRecord(scanRecord);
            if (mBleDeviceAdapter.addOrUpdateDevice(bleDevice)) {
                timestamp = System.currentTimeMillis();
                mBleDeviceAdapter.notifyDataSetChanged();
            } else {
                if (System.currentTimeMillis() - timestamp > interval) {
                    timestamp = System.currentTimeMillis();
                    mBleDeviceAdapter.notifyDataSetChanged();
                }
            }
        }
    };
    BleUtils.enableBluetooth();
    BleProxy.getInstance().init(getApplicationContext());
    requestBleAuthority();
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) Handler(android.os.Handler) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) BleDeviceAdapter(com.afunx.ble.blelitelib.adapter.BleDeviceAdapter) BleDevice(com.afunx.ble.blelitelib.bean.BleDevice) AdapterView(android.widget.AdapterView) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Aggregations

BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothDevice (android.bluetooth.BluetoothDevice)1 Handler (android.os.Handler)1 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 BleDeviceAdapter (com.afunx.ble.blelitelib.adapter.BleDeviceAdapter)1 BleDevice (com.afunx.ble.blelitelib.bean.BleDevice)1