Android Open Source - BleSensorTag Ble Devices Adapter

From Project

Back to project page BleSensorTag.


The source code is released under:

MIT License

If you think the Android project BleSensorTag listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package sample.ble.sensortag.adapters;
/*w  w w. ja  va  2s  . co m*/
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import sample.ble.sensortag.R;

/** Adapter for holding devices found through scanning.
 *  Created by steven on 9/5/13.
public class BleDevicesAdapter extends BaseAdapter {
    private final LayoutInflater inflater;

    private final ArrayList<BluetoothDevice> leDevices;
    private final HashMap<BluetoothDevice, Integer> rssiMap = new HashMap<BluetoothDevice, Integer>();

    public BleDevicesAdapter(Context context) {
        leDevices = new ArrayList<BluetoothDevice>();
        inflater = LayoutInflater.from(context);

    public void addDevice(BluetoothDevice device, int rssi) {
        if (!leDevices.contains(device)) {
        rssiMap.put(device, rssi);

    public BluetoothDevice getDevice(int position) {
        return leDevices.get(position);

    public void clear() {

    public int getCount() {
        return leDevices.size();

    public Object getItem(int i) {
        return leDevices.get(i);

    public long getItemId(int i) {
        return i;

    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = inflater.inflate(R.layout.li_device, null);
            viewHolder = new ViewHolder();
            viewHolder.deviceAddress = (TextView) view.findViewById(;
            viewHolder.deviceName = (TextView) view.findViewById(;
            viewHolder.deviceRssi = (TextView) view.findViewById(;
        } else {
            viewHolder = (ViewHolder) view.getTag();

        BluetoothDevice device = leDevices.get(i);
        final String deviceName = device.getName();
        if (deviceName != null && deviceName.length() > 0)
        viewHolder.deviceRssi.setText(""+rssiMap.get(device)+" dBm");

        return view;

    private static class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView deviceRssi;

Java Source Code List