蓝牙的简单学习——开/关—搜索

  • 1、显式开启蓝牙设备

    1. Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    2. startActivityForResult(intent,1);
    2、隐式开启/关闭蓝牙设备

    1. BluetoothAdapter mAdapter=BluetoothAdapter.getDefaultAdapter();
    2. //开启蓝牙设备
    3. mAdapter.enable();
    4. //关闭蓝牙设备
    5. mAdapter.disable();
    3、获取已配对的蓝牙设备

    01. Set<BluetoothDevice> mDevices=mAdapter.getBondedDevices();
    02. //获取已配对的设备
    03. if(mDevices.size()>0)
    04. {
    05. for(BluetoothDevice mDevice : mDevices)
    06. {
    07. mView.append(mDevice.getName()+":"+mDevice.getAddress()+"\n");
    08. }
    09. }
    4、搜索蓝牙设备

    01. private final BroadcastReceiver receiver=new BroadcastReceiver()
    02. {
    03. @Override
    04. public void onReceive(Context mContext, Intent intent)
    05. {
    06. if(BluetoothDevice.ACTION_FOUND.equals(intent.getAction()))
    07. {
    08. BluetoothDevice mDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    09. if(mDevice.getBondState()!=BluetoothDevice.BOND_BONDED)
    10. {
    11. mView.append(mDevice.getName()+":"+mDevice.getAddress()+"\n");
    12. }
    13. }elseif(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction()))
    14. {
    15. MainActivity.this.setProgressBarIndeterminateVisibility(false);
    16. }
    17. }
    18. };
    19.  
    20.  
    21. //蓝牙动作监听广播
    22. IntentFilter mFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
    23. this.registerReceiver(receiver, mFilter);
    24. //蓝牙动作监听结束广播
    25. mFilter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    26. this.registerReceiver(receiver, mFilter);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值