android的学习:蓝牙通讯(本地连接)

一、代入

Bluetooth是一种无线标准技术,可实现固定设备、移动设备和楼宇个人域网之间的短距离数据交换(使用2.4—2.485GHz的ISM波段的UHF无线电波);是目前使用最广泛的无线通讯协议,近距离无线通讯的标准。主要针对短距离设备通讯(10米)

推荐(参考)资源
https://blog.csdn.net/u013323018/article/details/83001455
https://blog.csdn.net/lucky_liuxiang/article/details/80231802

二、蓝牙工作流程图

在这里插入图片描述

三、BluetoothAdapter

BluetoothAdapter蓝牙适配器对象,通过它我们可以蓝牙设备进行er类简单点来说就是代表本设备(手机、电脑等)的基本开发了,主要有如下功能:

  1. 打开蓝牙设备
  2. 扫描蓝牙设备
  3. 设置/获取蓝牙状态信息,例如:蓝牙状态值、蓝牙Name、蓝牙Mac地址等;

BluetoothAdapter STATE 状态值,即开关状态

  • int STATE_OFF 蓝牙已经关闭
  • int STATE_ON 蓝牙已经打开
  • int STATE_TURNING_OFF 蓝牙处于关闭过程中,关闭ing
  • int STATE_TURNING_ON 蓝牙处于打开过程中,打开ing

BluetoothAdapter SCAN_MOD状态值,即扫描状态

  • int SCAN_MODE_CONNECTABLE 表明该蓝牙可以扫描其他蓝牙设备
  • int SCAN_MODE_CONNECTABLE_DISCOVERABLE 表明该蓝牙设备同时可以扫码其他蓝牙设备,并且可以被其他蓝牙设备扫描到
  • int SCAN_MODE_NONE 该蓝牙不能扫描以及被扫描

五、获取本地蓝牙的实例

1、布局文件

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝牙配置"
        android:id="@+id/txt_title"
        android:textSize="20sp"
        android:textColor="#212121"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_open"
        android:text="打开蓝牙"
        android:textSize="20sp"
        android:textColor="#212121"
        android:background="#eee"/>

</LinearLayout>

2、MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button btn_openBT;
    BluetoothAdapter myAdapter;
    public static final int requestCode =0x101;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取本地蓝牙适配器
        myAdapter=BluetoothAdapter.getDefaultAdapter();
        
        //判断蓝牙是否存在
        if (myAdapter==null){
            Toast.makeText(this, "该设备不支持蓝牙功能", Toast.LENGTH_SHORT).show();
            return;
        }
        //接下来就判断蓝牙是否开启
        btn_openBT=findViewById(R.id.btn_open);
        btn_openBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (myAdapter.isEnabled()){
                    Toast.makeText(MainActivity.this, "蓝牙已经处于打开的状态", Toast.LENGTH_SHORT).show();
                }else {
                    /*关闭状态的话,第一种方法是强行打开
                    myAdapter.enable();*/
                    //第二种方法,用Android里的API来打开
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(intent,requestCode);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==0x101){
                if (requestCode==RESULT_CANCELED){
                    Toast.makeText(this, "蓝牙已经打开", Toast.LENGTH_SHORT).show();
                }
        }
    }
}

3、添加权限

    <uses-permission android:name="android.permission.BLUETOOTH"></uses-permission>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"></uses-permission>

4、运行结果
在这里插入图片描述
如果对您有帮助,麻烦您点个赞哦~谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值