android蓝牙开发二-

1、获取系统已配对的蓝牙设备

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,"本机没有蓝牙设备");
        }
    }
}

 

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;
    }
}
————————————————
版权声明:本文为CSDN博主「596785154」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zcn596785154/article/details/78109938

 

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


    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@color/white"
    android:orientation="vertical">
            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" />
            android:layout_width="match_parent"
        android:layout_height="120dp">
                    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" />
   


————————————————
版权声明:本文为CSDN博主「596785154」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zcn596785154/article/details/78109938

你可能感兴趣的:(android)