扫描周围可用蓝牙设备、以及设置蓝牙设备的可见性

    此篇博文主要讲的是蓝牙:它包括蓝牙的可见性设置、以及扫描周围的蓝牙设备

    manifests里面所加的权限:

    <!--扫描已配对的蓝牙设备权限-->
        <uses-permission android:name="android.permission.BLUETOOTH" />
        <!--修改蓝牙设备的可见性的权限-->
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

    主程序,代码里面已经有详细的注解,这里就不多加叙述了。

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private Button mButton;
        private Button mButtonDiscover;
        private Button mButtonScan;
        private BluetoothAdapter bluetoothAdapter;
        private BluetoothReceiver bluetoothReceiver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //得到BluetoothAdapter的对象
            bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();
            //创建一个IntentFilter对象,将其action指定为BluetoothDevice.ACTION_FOUND
            IntentFilter intentFilter=new IntentFilter(BluetoothDevice.ACTION_FOUND);
            bluetoothReceiver=new BluetoothReceiver();
            //注册广播接收器
            registerReceiver(bluetoothReceiver,intentFilter);
            mButton= (Button) findViewById(R.id.button_bluetooth);
            mButton.setOnClickListener(this);
            mButtonDiscover= (Button) findViewById(R.id.discover);
            mButtonDiscover.setOnClickListener(this);
            mButtonScan= (Button) findViewById(R.id.scan_bluetooth);
            mButtonScan.setOnClickListener(this);
    
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.button_bluetooth:
                    getBlueToothAddress();
                    break;
                case R.id.discover:
                    //设置监听器用来修改蓝牙设备的可见性,设置一个intent对象,将其action值设置为BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE
                    Intent discoverintent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                    //将一个键值对存放在intent对象当中,主要用来指定可见状态的持续时间
                    discoverintent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,120);
                    startActivity(discoverintent);
                    break;
                case R.id.scan_bluetooth:
                    bluetoothAdapter.startDiscovery();
                    break;
            }
    
        }
        public class BluetoothReceiver extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
              String action=intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)){
                    //可以从收到的intent对象中,将代表远程蓝牙的适配器取出
                    BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    Log.d("bluetooth",device.getAddress());
                }
    
            }
        }
    /*
    用来判断是否有蓝牙设备,如果有蓝牙设备,扫描并得到已经配对的蓝牙设备的Mac地址
     */
        private void getBlueToothAddress() {
            //判断BluetoothAdapter对象是否为空,如果为空,则表示本机没有蓝牙设备
            if (bluetoothAdapter!=null){
                //调用isEnabled()方法,判断当前蓝牙设备是否可用
                if (!bluetoothAdapter.isEnabled()){
                    //如果蓝牙设备不可用,创建一个Intent对象,该对象用于启动一个Activity,提示用户开启蓝牙设备
                    Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivity(intent);
                }
                //得到所有已配对的蓝牙适配器的对象
                Set<BluetoothDevice> devices=bluetoothAdapter.getBondedDevices();
                if (devices.size()>0){
                    for (Iterator iterator=devices.iterator();iterator.hasNext();){
                        BluetoothDevice bluetoothDevice= (BluetoothDevice) iterator.next();
                        //得到远程蓝牙设备的地址
                        Log.d("bluetooth", bluetoothDevice.getAddress());
                    }
                }
            }else {
                Log.d("bluetooth","没有蓝牙设备");
            }
        }
    }

    布局文件:

    <LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">
       <Button
        android:id="@+id/button_bluetooth"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描周围蓝牙设备"/>
        <Button
            android:id="@+id/discover"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="设置可见性"/>
        <Button
            android:id="@+id/scan_bluetooth"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="开始扫描"/>
    
    </LinearLayout>
    • 0
      点赞
    • 2
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

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

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

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值