蓝牙入门代码
来源:互联网 发布:php compact 编辑:程序博客网 时间:2024/06/06 04:09
1.MyAdapter代码
/** * Created by wangcaiwen on 2017/8/15. */public class MyAdapter extends BaseAdapter { private ArrayList<BluetoothDevice> mList; private Context context; public MyAdapter(ArrayList<BluetoothDevice> mList, Context context) { this.mList = mList; this.context = context; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int i) { return mList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder=null;if(view==null) { view = View.inflate(context, R.layout.item, null); holder=new ViewHolder(); holder.tv_address=view.findViewById(R.id.tv_address); holder.tv_name=view.findViewById(R.id.tv_name); holder.tv_state=view.findViewById(R.id.tv_state); view.setTag(holder);}else{ holder = (ViewHolder) view.getTag();}//设置值 BluetoothDevice device= (BluetoothDevice) getItem(i); holder.tv_address.setText(device.getAddress()); holder.tv_name.setText(device.getName()); int state = device.getBondState(); switch (state){ case BluetoothDevice.BOND_BONDED: holder.tv_state.setText("已配对"); break; case BluetoothDevice.BOND_NONE: holder.tv_state.setText("未配对"); break; case BluetoothDevice.BOND_BONDING: holder.tv_state.setText("正在配对"); break; } return view; } class ViewHolder{ public TextView tv_address; public TextView tv_name; public TextView tv_state; }}
2.activity代码
public class MainActivity extends AppCompatActivity { private ListView lv; private ArrayList<BluetoothDevice> mList; private BluetoothAdapter mBlueAdapter; private MyAdapter ma; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); //注册广播接收器,用于接受蓝牙相关的广播 registerBlueToothReceiver(); } /** * 注册蓝牙广播 */ private void registerBlueToothReceiver() { } /** * 初始化数据 */ private void initData() { mList = new ArrayList<>(); mBlueAdapter = BluetoothAdapter.getDefaultAdapter(); mBlueAdapter.enable(); } /** * 初始化控件 */ private void initView() { lv = (ListView) findViewById(R.id.lv); } /** * 扫描设备 * @param v */ public void scan(View v){ searchBondedDevice(); searchUnBondedDevice(); setAdapter(); } /** * 设置适配器,刷新数据 */ private void setAdapter() { if(ma==null){ ma = new MyAdapter(mList,MainActivity.this); lv.setAdapter(ma); }else{ ma.notifyDataSetChanged(); } } /** * 搜索未配对的设备 */ private void searchUnBondedDevice() { if(mBlueAdapter.isDiscovering()){ mBlueAdapter.cancelDiscovery(); } mBlueAdapter.startDiscovery(); } /** * 搜索已配对的设备 */ private void searchBondedDevice() { Set<BluetoothDevice> bondedDevices = mBlueAdapter.getBondedDevices(); for (BluetoothDevice bd : bondedDevices) { if(!mList.contains(bd)){ mList.add(bd); } } } /** * 广播接收器 */ class MyReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); //蓝牙设备 BluetoothDevice device= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //发现蓝牙设备的逻辑 if(action.equals(BluetoothDevice.ACTION_FOUND)){ //是否包含此设备 if(!mList.contains(device)){ mList.add(device); } //刷新数据 setAdapter(); //蓝牙状态监听 }else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){ //得到蓝牙状态 int bondState = device.getBondState(); switch(bondState){ case BluetoothDevice.BOND_BONDED: mList.remove(device); mList.add(0,device); setAdapter(); break; case BluetoothDevice.BOND_BONDING: Toast.makeText(MainActivity.this, "正在配对...", Toast.LENGTH_SHORT).show(); break; case BluetoothDevice.BOND_NONE: Toast.makeText(MainActivity.this, "未配对", Toast.LENGTH_SHORT).show(); break; } } } }}
阅读全文
0 0
- 蓝牙入门代码
- iOS蓝牙开发入门06--demo的外设代码释义
- J2ME蓝牙实战入门
- J2ME蓝牙实战入门
- J2ME蓝牙实战入门
- 蓝牙开发入门
- android -- 蓝牙 bluetooth 入门
- Android蓝牙编程入门
- Android 蓝牙开发入门
- Android蓝牙编程入门
- iOS蓝牙4.0入门
- iOS蓝牙4.0入门
- 蓝牙低功耗入门
- android蓝牙开发入门
- 蓝牙入门到精通
- Android蓝牙开发入门
- Android传统蓝牙入门
- iOS 蓝牙入门CoreBluetooth
- SpringMVC整合Redis2.9.0
- Java中eclipse再安装问题
- 机器学习笔记第二课
- Oracle Flashback技术
- 3-5 三角形判断
- 蓝牙入门代码
- 理解最大似然估计
- 网络接口配置——bonding
- Ajax的优缺点 -- 算是面试题吧
- JSP知识点概括
- 剑指offer(四)重建二叉树
- 【贪心】51Nod1241[特殊的排序]题解
- Unity中利用Invoke执行延时方法和利用InvokeRepeating执行重复方法
- Qt网络编程--HTTP服务器(五)