蓝牙

蓝牙

一、设置权限
<uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
二、布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/open_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开蓝牙" />

    <Button
        android:id="@+id/close_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="关闭蓝牙" />

    <Button
        android:id="@+id/getName_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取本机蓝牙名称" />

    <Button
        android:id="@+id/nearby_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="扫描附近蓝牙" />

    <Button
        android:id="@+id/show_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="展示已配对设备" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/mynearbyReceiver_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </androidx.recyclerview.widget.RecyclerView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myshowReceiver_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

三、判断权限

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
   
            requestPermissions(new String[]{
   
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 100);

        }

四、打开,关闭蓝牙

    //打开蓝牙
        openId.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View v) {
   
                Intent intent = new Intent();
                //启动蓝牙
                intent.setAction(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                intent.setAction(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivity(intent);
            }
        });
        //关闭蓝牙
        closeId.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View v) {
   
                //获得本机蓝牙适配器
                BluetoothAdapter defaultAdapter = BluetoothAdapter.getDefaultAdapter();
                //关闭本机蓝牙设备
                defaultAdapter.disable();
            }
        });

五、获取本机蓝牙名字

   //获取本机名字
        getNameId.setOnClickListener(new View.OnClickListener() {
   
            <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值