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

深碍√TFBOYSˉ_ 2022-06-08 13:29 915阅读 0赞

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <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" />
  4. <FrameLayout android:layout_width="match_parent" android:layout_height="120dp">
  5. <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" />
  6. </FrameLayout>
  7. </LinearLayout>

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

  1. public class PairedBluetoothDialogAdapter extends BaseAdapter {
  2. public static final String TAG = "ListViewAdapter";
  3. private Context context;
  4. private List<HashMap> arrayList;
  5. public PairedBluetoothDialogAdapter(Context context, List<HashMap> arrayList){
  6. this.context = context;
  7. this.arrayList = arrayList;
  8. }
  9. @Override
  10. public int getCount() {
  11. return arrayList.size();
  12. }
  13. @Override
  14. public Object getItem(int position) {
  15. return arrayList.get(position);
  16. }
  17. @Override
  18. public long getItemId(int position) {
  19. return position;
  20. }
  21. @Override
  22. public View getView(int position, View convertView, ViewGroup parent) {
  23. ViewHolder holder = null;
  24. LayoutInflater mInflater = LayoutInflater.from(context);
  25. if(convertView == null)
  26. {
  27. holder = new ViewHolder();
  28. convertView = mInflater.inflate(R.layout.item_paired_bluetooth, null);
  29. holder.tvName = (TextView)convertView.findViewById(R.id.item_name);
  30. holder.tvAddress = (TextView)convertView.findViewById(R.id.item_address);
  31. convertView.setTag(holder);
  32. }else {
  33. Log.d(TAG, "not_null " + position);
  34. holder = (ViewHolder)convertView.getTag();
  35. }
  36. holder.device = (BluetoothDevice) ((HashMap)arrayList.get(position)).get("blue_device");
  37. holder.tvName.setText((String)((HashMap)arrayList.get(position)).get("blue_name"));
  38. holder.tvAddress.setText((String)((HashMap)arrayList.get(position)).get("blue_address"));
  39. return convertView;
  40. }
  41. static class ViewHolder
  42. {
  43. public BluetoothDevice device;//不是用来显示,用来在item点击时返回连接对象
  44. public TextView tvName;
  45. public TextView tvAddress;
  46. }
  47. }

item_paired_bluetooth.xml:Adapter的布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <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">
  3. <TextView android:id="@+id/item_name" android:layout_width="wrap_content" android:layout_height="48dp" android:gravity="center_vertical" android:textSize="18sp"/>
  4. <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"/>
  5. </LinearLayout>

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

  1. public class BluePairedActivity extends Activity {
  2. public static final String TAG ="Chunna==BlueActivity";
  3. private List<HashMap> blueList;
  4. private HashMap blueHashMap;
  5. private ListView glvPaired;
  6. private BluetoothAdapter adapter;
  7. private PairedBluetoothDialogAdapter pairedAdapter;
  8. public static BluetoothSocket socket;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_blue_paired);
  13. initBlueTooth();
  14. glvPaired = (ListView)findViewById(R.id.lv_blue_paired);
  15. pairedAdapter = new PairedBluetoothDialogAdapter(this,blueList);
  16. pairedAdapter.notifyDataSetChanged();
  17. glvPaired.setAdapter(pairedAdapter);
  18. glvPaired.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  19. @Override
  20. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  21. BluetoothDevice gDevice = (BluetoothDevice)(((HashMap)pairedAdapter.getItem(position)).get("blue_device"));
  22. Log.d(TAG, "想要连接的远程主机:" + gDevice);
  23. Log.d(TAG, "想要连接的远程主机:" + gDevice.toString());
  24. //然后就可以连接或者做操作啦
  25. }
  26. });
  27. }
  28. private void initBlueTooth() {
  29. adapter = BluetoothAdapter.getDefaultAdapter();
  30. if (adapter != null) {
  31. if (!adapter.isEnabled()) {
  32. adapter.enable();
  33. //sleep one second ,avoid do not discovery
  34. try {
  35. Thread.sleep(1000);
  36. } catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. Set<BluetoothDevice> devices = adapter.getBondedDevices();
  41. blueList = new ArrayList<HashMap>();
  42. Log.d(TAG,"获取已经配对devices"+devices.size());
  43. for (BluetoothDevice bluetoothDevice : devices)
  44. {
  45. Log.d(TAG, "已经配对的蓝牙设备:");
  46. Log.d(TAG, bluetoothDevice.getName());
  47. Log.d(TAG, bluetoothDevice.getAddress());
  48. blueHashMap = new HashMap();
  49. blueHashMap.put("blue_device",bluetoothDevice);
  50. blueHashMap.put("blue_name",bluetoothDevice.getName());
  51. blueHashMap.put("blue_address",bluetoothDevice.getAddress());
  52. blueList.add(blueHashMap);
  53. }
  54. }else{
  55. ToastUtil.getShortToastByString(this,"本机没有蓝牙设备");
  56. }
  57. }
  58. }

发表评论

表情:
评论列表 (有 0 条评论,915人围观)

还没有评论,来说两句吧...

相关阅读