Search in sources :

Example 1 with BleDevice

use of com.afunx.ble.blelitelib.bean.BleDevice 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)

Example 2 with BleDevice

use of com.afunx.ble.blelitelib.bean.BleDevice in project BleLiteLib4android by afunx.

the class BleDeviceAdapter method addOrUpdateDeviceInternal.

/**
 * add or update ble device
 *
 * @param device the device to be added or updated
 * @return whether the device is added or updated
 */
private boolean addOrUpdateDeviceInternal(BleDevice device) {
    BleDevice deviceInList = getBleDevice(device);
    if (deviceInList == null) {
        // add
        mBleDeviceList.add(device);
        return true;
    } else {
        // update
        deviceInList.setRssi(device.getRssi());
        deviceInList.setBluetoothDevice(device.getBluetoothDevice());
        deviceInList.setScanRecord(device.getScanRecord());
        return false;
    }
}
Also used : BleDevice(com.afunx.ble.blelitelib.bean.BleDevice)

Example 3 with BleDevice

use of com.afunx.ble.blelitelib.bean.BleDevice in project BleLiteLib4android by afunx.

the class BleDeviceAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get view holder
    ViewHolder viewHolder = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.ble_device_item, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.tvDevName = (TextView) convertView.findViewById(R.id.tv_dev_name);
        viewHolder.tvDevAddr = (TextView) convertView.findViewById(R.id.tv_dev_addr);
        viewHolder.tvDevSig = (TextView) convertView.findViewById(R.id.tv_dev_sig);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // set view content
    final BleDevice device = mBleDeviceList.get(position);
    final Resources resources = mContext.getResources();
    // device name
    String devName = device.getBluetoothDevice().getName();
    if (TextUtils.isEmpty(devName)) {
        devName = resources.getString(R.string.device_name_unknown_device);
    }
    viewHolder.tvDevName.setText(devName);
    // device address
    String titleAddr = resources.getString(R.string.device_address);
    String devAddr = device.getBluetoothDevice().getAddress();
    viewHolder.tvDevAddr.setText(titleAddr + ": " + devAddr);
    // device signal
    String titleSig = resources.getString(R.string.device_signal);
    int devSig = device.getRssi();
    viewHolder.tvDevSig.setText(titleSig + ": " + devSig);
    return convertView;
}
Also used : BleDevice(com.afunx.ble.blelitelib.bean.BleDevice) Resources(android.content.res.Resources)

Aggregations

BleDevice (com.afunx.ble.blelitelib.bean.BleDevice)3 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 BluetoothDevice (android.bluetooth.BluetoothDevice)1 Resources (android.content.res.Resources)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