Android 蓝牙开发——获取已配对蓝牙并显示

activity_blue_paired.xml布:里面有一个列表,用于显示所有已配对的设备


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/white"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        style="@style/text_18_ffffff"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#84b9ff"
        android:text="已配对蓝牙列表"
        android:visibility="visible" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="120dp">
        <ListView
            android:id="@+id/lv_blue_paired"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="#e6e7e9"
            android:dividerHeight="1dp"
            android:headerDividersEnabled="true"
            android:footerDividersEnabled="true"
            android:scrollbars="none"
            android:cacheColorHint="#00000000"
            android:listSelector="#00000000" />
    FrameLayout>

LinearLayout>

PairedBluetoothDialogAdapter.java:listView的Adapter类,显示listView列表的每一项数据,其中有两个TextView控件,用来显示蓝牙名和蓝牙地址。还有一个BluetoothDevice对象,这个对象不是用来显示,是用来在列表点击时,返回蓝牙设备

public class PairedBluetoothDialogAdapter extends BaseAdapter {
    public static final String TAG = "ListViewAdapter";
    private Context context;
    private List arrayList;
    public PairedBluetoothDialogAdapter(Context context, List arrayList){
        this.context = context;
        this.arrayList = arrayList;
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder  holder = null;
        LayoutInflater  mInflater = LayoutInflater.from(context);
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_paired_bluetooth, null);
            holder.tvName = (TextView)convertView.findViewById(R.id.item_name);
            holder.tvAddress = (TextView)convertView.findViewById(R.id.item_address);
            convertView.setTag(holder);
        }else {
            Log.d(TAG, "not_null " + position);
            holder = (ViewHolder)convertView.getTag();
        }
        holder.device = (BluetoothDevice) ((HashMap)arrayList.get(position)).get("blue_device");
        holder.tvName.setText((String)((HashMap)arrayList.get(position)).get("blue_name"));
        holder.tvAddress.setText((String)((HashMap)arrayList.get(position)).get("blue_address"));
        return convertView;
    }
    static class ViewHolder
    {
        public BluetoothDevice device;//不是用来显示,用来在item点击时返回连接对象
        public TextView tvName;
        public TextView tvAddress;
    }
}

item_paired_bluetooth.xml:Adapter的布局文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_height="48dp">
    <TextView
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:textSize="18sp"/>
    <TextView
        android:id="@+id/item_address"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:gravity="center_vertical"
        android:lines="1"
        android:ellipsize="end"
        android:layout_marginLeft="10dp"
        android:textSize="16sp"/>
LinearLayout>

java代码:显示蓝牙列表,并对点击事件做相应,可以在点击列表中获取到返回的蓝牙设备,并对其进行操作

public class BluePairedActivity extends Activity {
    public static final String TAG ="Chunna==BlueActivity";
    private List blueList;
    private HashMap blueHashMap;
    private ListView glvPaired;
    private BluetoothAdapter adapter;
    private PairedBluetoothDialogAdapter pairedAdapter;

    public static BluetoothSocket socket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue_paired);
        initBlueTooth();
        glvPaired = (ListView)findViewById(R.id.lv_blue_paired);
        pairedAdapter = new PairedBluetoothDialogAdapter(this,blueList);
        pairedAdapter.notifyDataSetChanged();

        glvPaired.setAdapter(pairedAdapter);
        glvPaired.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                BluetoothDevice gDevice = (BluetoothDevice)(((HashMap)pairedAdapter.getItem(position)).get("blue_device"));
                Log.d(TAG, "想要连接的远程主机:" + gDevice);
                Log.d(TAG, "想要连接的远程主机:" + gDevice.toString());

                //然后就可以连接或者做操作啦
            }
        });
    }

    private void initBlueTooth() {
        adapter = BluetoothAdapter.getDefaultAdapter();
        if (adapter != null) {
            if (!adapter.isEnabled()) {
                adapter.enable();
                //sleep one second ,avoid do not discovery
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Set devices = adapter.getBondedDevices();
            blueList = new ArrayList();
            Log.d(TAG,"获取已经配对devices"+devices.size());
            for (BluetoothDevice bluetoothDevice : devices)
            {
                Log.d(TAG, "已经配对的蓝牙设备:");
                Log.d(TAG, bluetoothDevice.getName());
                Log.d(TAG, bluetoothDevice.getAddress());
                blueHashMap = new HashMap();
                blueHashMap.put("blue_device",bluetoothDevice);
                blueHashMap.put("blue_name",bluetoothDevice.getName());
                blueHashMap.put("blue_address",bluetoothDevice.getAddress());
                blueList.add(blueHashMap);
            }
    }else{
            ToastUtil.getShortToastByString(this,"本机没有蓝牙设备");
        }
    }
}

你可能感兴趣的:(android-开发)