Android 蓝牙开发——获取已配对蓝牙并显示
2017-09-27 10:16
357 查看
activity_blue_paired.xml布:里面有一个列表,用于显示所有已配对的设备
>
PairedBluetoothDialogAdapter.java:listView的Adapter类,显示listView列表的每一项数据,其中有两个TextView控件,用来显示蓝牙名和蓝牙地址。还有一个BluetoothDevice对象,这个对象不是用来显示,是用来在列表点击时,返回蓝牙设备
<?xml version="1.0" encoding="utf-8"?> <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>4000
>
PairedBluetoothDialogAdapter.java:listView的Adapter类,显示listView列表的每一项数据,其中有两个TextView控件,用来显示蓝牙名和蓝牙地址。还有一个BluetoothDevice对象,这个对象不是用来显示,是用来在列表点击时,返回蓝牙设备
public class PairedBluetoothDialogAdapter extends BaseAdapter { public static final String TAG = "ListViewAdapter"; private Context context; private List<HashMap> arrayList; public PairedBluetoothDialogAdapter(Context context, List<HashMap> 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的布局文件<?xml version="1.0" encoding="utf-8"?> <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<HashMap> 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<BluetoothDevice> devices = adapter.getBondedDevices(); blueList = new ArrayList<HashMap>(); 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之蓝牙开发续(自动配对)
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- Android 音乐播放器的开发教程(五)本地音乐的获取及显示 ----- 小达
- Android蓝牙开发——查询手机上已配对的蓝牙设备
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- android开发之蓝牙配对连接的方法
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备(转)
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- android开发之蓝牙主动配对连接手机
- Android手机蓝牙的开发——(一)蓝牙的开启关闭配对等基本操作
- android开发之蓝牙主动配对连接手机
- android开发之蓝牙初步 扫描已配对蓝牙、更改蓝牙可见性、搜索外部蓝牙设备
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- android开发之蓝牙配对连接的方法
- android 打开蓝牙设备 显示已经配对的蓝牙设备 ,并将已配对的蓝牙设备显示在textview中
- Android 蓝牙开发(二)开启蓝牙,获取状态,发现设备。
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- Android开发 通过URL获取网页源代码(滚动显示)
- 【移动开发】Android相机、相册获取图片显示并保存到SD卡
- Android低功耗蓝牙应用开发获取的服务UUID