android 蓝牙搜索
来源:互联网 发布:adobe cc mac 注册机 编辑:程序博客网 时间:2024/04/30 17:02
1.搜索蓝牙权限
<uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
2.MainActivity.java
package com.example.bluetooth;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import android.app.Activity;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private ListView tvBlueName;private Map<String, String> map;private List<Map<String,String>> list;private DeviceAdapter adapter;private BluetoothAdapter mBluetoothAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tvBlueName = (ListView) findViewById(R.id.textView1);mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//获取已经绑定的蓝牙设备信息Set<BluetoothDevice> devices = mBluetoothAdapter.getBondedDevices();list = new ArrayList<Map<String,String>>();if(devices.size() > 0){for(BluetoothDevice bluetoothDevice : devices){map = new HashMap<String, String>();map.put("name", bluetoothDevice.getName());map.put("address", bluetoothDevice.getAddress());list.add(map);}adapter = new DeviceAdapter();tvBlueName.setAdapter(adapter);}// 注册用以接收到已搜索到的蓝牙设备的receiver IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver, mFilter); // 注册搜索完时的receiver mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, mFilter); findViewById(R.id.button1).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {setProgressBarIndeterminateVisibility(true); setTitle("正在扫描...."); // 如果正在搜索,就先取消搜索 if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } // 开始搜索蓝牙设备,搜索到的蓝牙设备通过广播返回 mBluetoothAdapter.startDiscovery(); }});}private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(BluetoothDevice.ACTION_FOUND)){ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //获取未绑定的蓝牙设备 if(device.getBondState() != BluetoothDevice.BOND_BONDED){ map = new HashMap<String, String>(); map.put("name", device.getName()); map.put("address", device.getAddress()); list.add(map); } adapter.notifyDataSetChanged(); }else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) { setProgressBarIndeterminateVisibility(false); setTitle("搜索蓝牙设备"); } } }; class DeviceAdapter extends BaseAdapter{@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int arg0) {return list.get(arg0);}@Overridepublic long getItemId(int position) {return 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = getLayoutInflater().inflate(R.layout.item,null);TextView tv1 = (TextView) v.findViewById(R.id.name);TextView tv2 = (TextView) v.findViewById(R.id.address);tv1.setText(list.get(position).get("name"));tv2.setText(list.get(position).get("address"));return v;} }}
3.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="搜索" /> <ListView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="10dp" /></RelativeLayout>
0 0
- android 蓝牙搜索
- android 蓝牙 搜索匹配
- Android搜索附近蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- [Android蓝牙]搜索周围蓝牙设备
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android -- 蓝牙 bluetooth (三)搜索蓝牙
- android 蓝牙搜索功能实现
- Android 蓝牙Bluetooth搜索设备。
- Android 搜索不到蓝牙设备
- android 蓝牙bluetooth 4.2.2搜索蓝牙功能实现
- android 蓝牙4.0开发心得(一)蓝牙搜索
- Android 蓝牙通信开发(一)搜索蓝牙设备
- ubuntu12.04通过Ganglia利用NVML模块进行GPU监控
- 近阈值计算序曲
- TCP协议中的三次握手和四次挥手(图解)
- UVA - 10034 Freckles kruskal算法
- Automake系列( 四)安装可执行脚本
- android 蓝牙搜索
- SwipeRefreshLayout完美添加及完善上拉加载功能
- GPS坐标转换ddmm.mmmm(度分)-> dd.dddd(度)
- XCode 4 的调试定位技巧
- Java中文乱码原因及解决方法(转)
- android 高斯模糊实现以及性能比较
- lamba 表达式 in
- SQL Server 索引简述
- LigerDailog 一些Mark