Android Open Source - BLEService Device List Adapter

From Project

Back to project page BLEService.


The source code is released under:

Copyright (c) 2014, Ratio LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project BLEService 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 org.bluetooth.bledemo;
/*w  w w  .  j  av a 2s  . c  o  m*/
import java.util.ArrayList;

import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class DeviceListAdapter extends BaseAdapter {
  private ArrayList<BluetoothDevice> mDevices;
  private ArrayList<byte[]> mRecords;
  private ArrayList<Integer> mRSSIs;
  private LayoutInflater mInflater;
  public DeviceListAdapter(Activity par) {
    mDevices  = new ArrayList<BluetoothDevice>();
    mRecords = new ArrayList<byte[]>();
    mRSSIs = new ArrayList<Integer>();
    mInflater = par.getLayoutInflater();
  public void addDevice(BluetoothDevice device, int rssi, byte[] scanRecord) {
    if(mDevices.contains(device) == false) {
  public BluetoothDevice getDevice(int index) {
    return mDevices.get(index);
  public int getRssi(int index) {
    return mRSSIs.get(index);
  public void clearList() {
  public int getCount() {
    return mDevices.size();

  public Object getItem(int position) {
    return getDevice(position);

  public long getItemId(int position) {
    return position;

  public View getView(int position, View convertView, ViewGroup parent) {
    // get already available view or create new if necessary
    FieldReferences fields;
        if (convertView == null) {
          convertView = mInflater.inflate(R.layout.activity_scanning_item, null);
          fields = new FieldReferences();
          fields.deviceAddress = (TextView)convertView.findViewById(;
          fields.deviceName    = (TextView)convertView.findViewById(;
          fields.deviceRssi    = (TextView)convertView.findViewById(;
        } else {
            fields = (FieldReferences) convertView.getTag();
        // set proper values into the view
        BluetoothDevice device = mDevices.get(position);
        int rssi = mRSSIs.get(position);
        String rssiString = (rssi == 0) ? "N/A" : rssi + " db";
        String name = device.getName();
        String address = device.getAddress();
        if(name == null || name.length() <= 0) name = "Unknown Device";

    return convertView;
  private class FieldReferences {
    TextView deviceName;
    TextView deviceAddress;
    TextView deviceRssi;

Java Source Code List