判断当前手机蓝牙是否开启

众所周知蓝牙是我们手机都有的一个功能
可以利用代码来实现管理蓝牙的状态

private BluetoothAdapter adapter;
    private BroadcastReceiver receiver = new 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)) {
                String name=   device.getName();
            } else if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
                //可以在监听到蓝牙状态是进行操作
                switch (device.getBondState()){
                    case BluetoothDevice.BOND_BONDED:
                        //已经配对成功
                        break;
                    case BluetoothDevice.BOND_BONDING:
                        //正在连接
                        break;
                    case BluetoothDevice.BOND_NONE:
                        //取消连接
                        break;
                    default:
                        break;
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //蓝牙连接设备增加
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        //蓝牙连接状态发生改变
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        //注册
        registerReceiver(receiver, filter);
        //获取蓝牙适配器
        adapter  = BluetoothAdapter.getDefaultAdapter();
        boolean isenable = adapter.isEnabled();
        System.out.println(isenable + "<-=-=-=-isenable");
        //获取蓝牙是否可用
        Toast.makeText(MainActivity.this, isenable + "<-=-=-=-isenable", Toast.LENGTH_SHORT).show();
        if (!adapter.isEnabled()) {
            //开启蓝牙
            adapter.enable();
        } else {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            //设置你自己的设备可以被其他设备搜索的时间;最大3600
            intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(intent);
        }
        //蓝牙设备的地址
        String address = adapter.getAddress();
        String name = adapter.getName();
        Log.d("蓝牙", "name=" + name + "  address=" + address);
        //获取通过蓝牙已经连接的设备
        Set<BluetoothDevice> list = adapter.getBondedDevices();
        if (list != null) {
            for (BluetoothDevice device : list) {
                //设备名称
                String dname = device.getName();
            }
        }
        //搜索其他设备
        new Thread() {
            @Override
            public void run() {
                super.run();
                //正在搜索状态
                if (adapter.isDiscovering()) {
                    adapter.cancelDiscovery();
                }
                //开始搜索其他设备;
                adapter.startDiscovery();
            }
        }.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值