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();
}
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;
}
}
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;
}
Aggregations