Android之BlueTooth关于蓝牙

BlueTooth:

蓝牙无线技术:利用“蓝牙”技术,能够有效地简化掌上电脑、笔记本电脑和移动电话手机等移动通信终端设备之间的通信,也能够成功地简化以上这些设备与Internet之间的通信,从而使这些现代通信设备与因特网之间的数据传输变得更加迅速高效,为无线通信拓宽道路。

知识点:

     一、创建BlueTooth蓝牙适配器:
         * 1.获取蓝牙适配器
         * 2.让蓝牙创建到可以使用的状态 ——enable()方法
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        /**
         *  一、创建BlueTooth蓝牙适配器:
         * 1.获取蓝牙适配器
         * 2.让蓝牙创建到可以使用的状态 ——enable()方法
         */
        defaultAdapter = BluetoothAdapter.getDefaultAdapter();
        boolean enable = defaultAdapter.enable();
    }
二、 BUT3 — 广播接收则的注册: 用于接收已经搜索到的蓝牙设备broadcastReceiver

* 1.注册:intent.getAction:
* 2.开启:远程蓝牙设备BluetoothDevice
* 3.搜索设备:ACTION_FOUND(发现远程设备)
* 4.判断是否搜索到:搜索到已经注册的蓝牙设备—— 进行序列化EXTRA_DEVICE(发送广播)
*                   如果搜索到的设备不是已经绑定的蓝牙设备BOND_BONDED(远程设备已经匹配)——获取其名称地址
* 5. 搜索完成

 各种判断蓝牙配对情况请看网址有方法概述:https://blog.csdn.net/lu1024188315/article/details/72864521

  BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                //获得已经搜索到的蓝牙设备:
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice bluetoothDevice = (BluetoothDevice)intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    if(bluetoothDevice.getBondState()!=BluetoothDevice.BOND_BONDED){
                        String name=bluetoothDevice.getName();
                        String address = bluetoothDevice.getAddress();
                        tv2.append("蓝牙名称:"+name+",蓝牙地址:"+address+"\n");
                    }
                }
                //搜索完成:
                else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){
                    Toast.makeText(context, "搜索完成", Toast.LENGTH_SHORT).show();
                }
            }
        };
三、动态注册
 //动态注册
        IntentFilter intentFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(broadcastReceiver, intentFilter);
        intentFilter=new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(broadcastReceiver, intentFilter);

四、 点击事件,进行判断、扫描蓝牙设备:

 @SuppressLint("MissingPermission")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but1:
                boolean enabled = defaultAdapter.isEnabled();
                Toast.makeText(this, "蓝牙是否打开: "+enabled, Toast.LENGTH_SHORT).show();
                break;
            case R.id.but2:
                //获取已经绑定的蓝牙设备:
                Set<BluetoothDevice> bondedDevices = defaultAdapter.getBondedDevices();
                if(bondedDevices.size()>0){
                    for (BluetoothDevice bondedDevice:bondedDevices) {
                        String name = bondedDevice.getName();
                        String address = bondedDevice.getAddress();
                        tv.append("蓝牙名称:"+name+",蓝牙地址:"+address+"\n");
                    }
                }
                break;
            //扫描:
            case R.id.but3:
                //点击搜索蓝牙,当蓝牙适配器每发现了一个新的蓝牙设备的时候就会发送一个acton为BluetoothDevice.ACTION_FOUND的广播,这个时候我们只需要在我们注册的
                //广播接收器中进行拦截获取数据就可以拿到新发现的蓝牙设备的数据,例如蓝牙的名称和地址
                if (defaultAdapter != null){
                    defaultAdapter.startDiscovery();
                }else {
                    Toast.makeText(this, "kong", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值