蓝牙连接BluetoothAdapter

本文详细介绍了Android中蓝牙权限的申请,如何使用BluetoothAdapter和BluetoothManager进行蓝牙操作,包括初始化、开启、搜索设备和配对,以及针对不同Android版本的适配策略。重点讲解了BLE(蓝牙4.0及以上)的优势和使用注意事项。
摘要由CSDN通过智能技术生成

目录

1. 权限

2. BluetoothAdapter(与BluetoothManager)

3. 蓝牙的使用

(1) 初始化蓝牙适配器

(2) 开启蓝牙

(3) 搜索/取消搜索蓝牙设备

(4) 与指定的蓝牙设备配对

4. 案例代码一览


Android中使用的蓝牙分为两种,蓝牙2.0BLE(蓝牙4.0及以上版本)

蓝牙2.0有效距离为小于等于10米,传输速度最大375KB/s,连接建立时间6s。

BLE有效距离为小于等于100米,传输速度最大3MB/s,连接建立时间2s。

1. 权限

使用蓝牙需申请蓝牙权限蓝牙管理权限;建议限制仅在支持BLE(蓝牙4.0)的设备运行;如果Android6.0蓝牙搜索不到设备需要补充定位权限

<!--蓝牙,无需动态申请-->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<!--蓝牙管理,无需动态申请-->
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<!--开启可被搜索到的蓝牙时需要该权限,需要动态申请-->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!--开启不可被搜索到的蓝牙时需要该权限,需要动态申请-->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!--仅在支持BLE(蓝牙4.0)的设备上运行-->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

<!--如果Android6.0蓝牙搜索不到设备需要补充定位权限,需要动态申请-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

2. BluetoothAdapter(与BluetoothManager)

Android提供的蓝牙模块管理工具为BluetoothAdapter(蓝牙适配器)蓝牙2.0使用BluetoothAdapter的静态方法(getDefaultAdapter方法)获取蓝牙适配器BLE(蓝牙4.0)可使用BluetoothManager(蓝牙管理器)的getAdapter方法获取蓝牙适配器无论使用哪种方法获得的BluetoothAdapter均为一个。

BluetoothAdapter实际上干的是管理器的工作,以下是BluetoothAdapter的常用方法:

  • getDefaultAdapter:获取默认的蓝牙适配器。该方法为静态方法。
  • getState:获取蓝牙的开关状态。STATE_ON表示已开启,STATE_TURNING_ON表示正在开启,STATE_OFF表示已关闭,STATE_TURNING_OFF表示正在关闭。
  • enable:启用蓝牙功能。使用本方法启动蓝牙无法被其他设备检测到。
  • disable:禁用蓝牙功能。
  • isEnabled:判断蓝牙功能是否启用。返回true表示已启用,返回false表示未启用。
  • getBondedDevices:获取已配对(已绑定)的设备集合。
  • getRemoteDevice:根据设备地址获取远程的设备对象。
  • startDiscovery:开始搜索周围的蓝牙设备。
  • cancelDiscovery:取消搜索周围的蓝牙设备。
  • isDiscovering:判断是否正在搜索周围的蓝牙设备。

由于BluetoothAdapter实际干了管理器的活,因此Android从4.3开始引入了正牌的管理器 BluetoothManager,调用BluetoothManager对象的getAdapter方法也可获得蓝牙适配器。但Android 4.3对蓝牙的增强补充,不只是添加BluetoothManager,更是为了支持最新的BLE(即蓝牙低能耗“Bluetooth Low Energy”)BLE对应的是蓝牙4.0及以上版本。因为BLE采取非常快速的连接方式,所以平时处于“非连接”状态,此时链路两端仅是知晓对方,只有在必要时才开启链路,完成传输后会尽快关闭链路。BLE技术与之前版本的蓝牙标准相比, 主要有三个方面的改进:更省电、连接速度更快、传输距离更远。

3. 蓝牙的使用

检测蓝牙设备并配对的功能可得分成4个步骤:初始化(获取蓝牙适配器并判断该设备是否有蓝牙功能)、开启蓝牙、搜索蓝牙设备、与指定设备配对。下面分别进行详细说明。

(1) 初始化蓝牙适配器

如果App会用到BLE的特性,则需增加对 Android版本的判断,对于4.3及以上版本要从BluetoothManager中获取蓝牙适配器。如果仅仅是普通的蓝牙连接,则调用getDefaultAdapter获取蓝牙适配器就行了。

  1. 获取蓝牙适配器。
  2. 判断设备是否有蓝牙功能。
//获取蓝牙适配器
BluetoothAdapter bluetoothAdapter=null;
//Android4.3以上支持BLE技术(即蓝牙4.0版本及以上)
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR2){
    //如需要使用BLE特性,需使用蓝牙管理器获取蓝牙适配器
    BluetoothManager bluetoothManager= (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    bluetoothAdapter=bluetoothManager.getAdapter();
}
else {
    bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
}

//判断是否拥有蓝牙功能
if(bluetoothAdapter!=null){
    //该设备有蓝牙功能
}
else {
    //该设备无蓝牙功能
}

(2) 开启蓝牙

  1. 使用getState方法获取蓝牙适配器状态判断蓝牙是否已开启或正在开启。
  2. 使用startActivityForResult方法弹出是否允许扫描蓝牙界面。
  3. 重写onActivityResult方法获取申请结果。
private int mRequestCode=123456;//请求码

//获取蓝牙状态
int bluetoothState=bluetoothAdapter.getState();
//蓝牙状态不为打开状态且不为正在打开状态
if(bluetoothState!=BluetoothAdapter.STATE_ON&&bluetoothState!=BluetoothAdapter.STATE_TURNING_ON){
    //弹出是否允许扫描蓝牙界面(本action开启的蓝牙允许被搜索到)
    //需要BLUETOOTH_ADVERTISE权限
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    //弹出打开蓝牙界面(本action开启的蓝牙不会被搜索到)
    //需要BLUETOOTH_CONNECT权限
    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(intent,mRequestCode);
}
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //由蓝牙弹出窗返回
    if(requestCode==mRequestCode){
        if(resultCode==RESULT_OK){
                //蓝牙开启
        }
        else if(resultCode==RESULT_CANCELED) {
            //不允许蓝牙开启
            //弹出是否允许扫描蓝牙界面
            Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(intent,mRequestCode);
        }
    }
}

(3) 搜索/取消搜索蓝牙设备

  1. 使用isDiscovery方法判断蓝牙是否在搜索中。
  2. 使用startDiscovery方法开启蓝牙搜索。
  3. 使用cancelDiscovery方法取消蓝牙搜索。
  4. 蓝牙设备搜索通过广播异步返回结果,创建广播接收者并注册。
  5. 发现蓝牙设备后获取intent中包含的蓝牙设备(BluetoothDevice)对象。

BluetoothDevice类常用方法如下:

  • getName:获取设备的名称。
  • getAddress:获取设备的MAC地址。
  • getBondState:获取设备的配对状态。BOND_NONE表示未绑定,BOND_BONDING表示正在绑定,BOND_BONDED表示已绑定。
  • createBond:建立该设备的配对信息。该方法为隐藏方法,需要通过反射调用。
  • removeBond:移除该设备的配对信息。该方法为隐藏方法,需要通过反射调用。
//蓝牙适配器未在搜索中
if(!bluetoothAdapter.isDiscovering()){
    //开始蓝牙搜索
    bluetoothAdapter.startDiscovery();
}

//蓝牙适配器搜索中
if(bluetoothAdapter.isDiscovering()){
    //取消蓝牙搜索
    bluetoothAdapter.cancelDiscovery();
}
//自定义广播接收者
public class MyReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        //获取活动
        String action=intent.getAction();
        //发现蓝牙设备
        if(action.equals(BluetoothDevice.ACTION_FOUND)){
            //获取蓝牙设备   Parcelable-打包的
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        }
    }
}
private MyReceiver myReceiver=null;
protected void onStart() {
    super.onStart();
    //创建过滤器
    IntentFilter intentFilter=new IntentFilter();
    //添加发现蓝牙设备活动
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    //创建广播接收者对象
    myReceiver=new MyReceiver();
    //注册广播接收者
    registerReceiver(myReceiver,intentFilter);
}

protected void onStop() {
    super.onStop();
    //注销广播接收者
    unregisterReceiver(myReceiver);
}

(4) 与指定的蓝牙设备配对

在发现蓝牙设备后,可获取到蓝牙设备(BluetoothDevice)对象。从上面的方法说明可以看出,搜索获得新设备后,即可调用设备对象的createBond方法建立配对。但配对成功与否的结果同样不是立即返回的,因为系统会弹出配对确认框供用户选择。只有用户在两部手机都选择了“配对”按钮,才算是双方正式搭配好了。由于配对请求需要在界面上手工确认,因此配对结果只能通过异步机制返回,此处的结果返回仍然采取广播形式,即系统会发出广播BluetoothDevice.ACTION_BOND_STATE_CHANGED通知App。故而前面第三步的广播接收器需增加过滤配对状态的变更动作,接收器内部也要补充更新蓝牙设备的配对状态了。

//自定义广播接收者
public class MyReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        //获取活动
        String action=intent.getAction();
        //发现蓝牙设备
        if(action.equals(BluetoothDevice.ACTION_FOUND)){
            //获取蓝牙设备   Parcelable-打包的
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        }
        //绑定蓝牙状态改变
        else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
            //获取蓝牙设备   Parcelable-打包的
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //获取绑定状态
            int bondState=bluetoothDevice.getBondState();
            //判断绑定状态
            if(bondState==BluetoothDevice.BOND_BONDED){
                //已绑定
            }
            else if(bondState==BluetoothDevice.BOND_BONDING){
                //正在绑定
            }
            else if (bondState==BluetoothDevice.BOND_NONE) {
                //未绑定
            }
        }
    }
}
private MyReceiver myReceiver=null;
protected void onStart() {
    super.onStart();
    //创建过滤器
    IntentFilter intentFilter=new IntentFilter();
    //添加发现蓝牙设备活动
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    //添加绑定状态改变活动
    intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    //创建广播接收者对象
    myReceiver=new MyReceiver();
    //注册广播接收者
    registerReceiver(myReceiver,intentFilter);
}

protected void onStop() {
    super.onStop();
    //注销广播接收者
    unregisterReceiver(myReceiver);
}

4. 案例代码一览

public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter bluetoothAdapter=null;
    private Button button=null;
    @SuppressLint("MissingInflatedId")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        button=findViewById(R.id.button);

        //获取蓝牙适配器
        //Android4.3以上支持BLE技术(即蓝牙4.0版本及以上)
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR2){
            //如需要使用BLE特性,需使用蓝牙管理器获取蓝牙适配器
            BluetoothManager bluetoothManager= (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
            bluetoothAdapter=bluetoothManager.getAdapter();
        }
        else {
            bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        }

        //判断是否拥有蓝牙功能
        if(bluetoothAdapter!=null){
            //该设备有蓝牙功能
            openBluetooth();
            //为按钮设置扫描监听器
            button.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("MissingPermission")
                public void onClick(View view) {
                    //蓝牙适配器未在搜索中
                    if(!bluetoothAdapter.isDiscovering()){
                        //开始蓝牙搜索
                        bluetoothAdapter.startDiscovery();
                    }
                    //定时停止蓝牙搜索
                    new Thread(new Runnable() {
                        public void run() {
                            try {
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            //蓝牙适配器搜索中
                            if(bluetoothAdapter.isDiscovering()){
                                //取消蓝牙搜索
                                bluetoothAdapter.cancelDiscovery();
                            }
                        }
                    }).start();
                }
            });
        }
        else {
            //该设备无蓝牙功能
        }

    }

    private int mRequestCode=123456;
    /**
     * 打开蓝牙
     */
    @SuppressLint("MissingPermission")
    private void openBluetooth(){
        //获取蓝牙状态
        int bluetoothState=bluetoothAdapter.getState();
        //蓝牙状态不为打开状态且不为正在打开状态
        if(bluetoothState!=BluetoothAdapter.STATE_ON&&bluetoothState!=BluetoothAdapter.STATE_TURNING_ON){
            //弹出是否允许扫描蓝牙界面
            Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            startActivityForResult(intent,mRequestCode);
        }
    }

    @SuppressLint("MissingPermission")
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //由蓝牙弹出窗返回
        if(requestCode==mRequestCode){
            if(resultCode==RESULT_OK){
                //蓝牙开启
            }
            else if(resultCode==RESULT_CANCELED) {
                //不允许蓝牙开启
                //弹出是否允许扫描蓝牙界面
                Intent intent=new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
                startActivityForResult(intent,mRequestCode);
            }
        }
    }

    private MyReceiver myReceiver=null;
    protected void onStart() {
        super.onStart();
        //创建过滤器
        IntentFilter intentFilter=new IntentFilter();
        //添加发现蓝牙设备活动
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        //添加绑定状态改变活动
        intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        //创建广播接收者对象
        myReceiver=new MyReceiver();
        //注册广播接收者
        registerReceiver(myReceiver,intentFilter);
    }

    protected void onStop() {
        super.onStop();
        //注销广播接收者
        unregisterReceiver(myReceiver);
    }
public class MyReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        //获取活动
        String action=intent.getAction();
        //发现蓝牙设备
        if(action.equals(BluetoothDevice.ACTION_FOUND)){
            //获取蓝牙设备   Parcelable-打包的
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        }
        //绑定蓝牙状态改变
        else if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
            //获取蓝牙设备
            BluetoothDevice bluetoothDevice=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //获取绑定状态
            int bondState=bluetoothDevice.getBondState();
            //判断绑定状态
            if(bondState==BluetoothDevice.BOND_BONDED){
                //已绑定
            }
            else if(bondState==BluetoothDevice.BOND_BONDING){
                //正在绑定
            }
            else if (bondState==BluetoothDevice.BOND_NONE) {
                //未绑定
            }
        }
    }
}

tag:蓝牙,bluetooth,Bluetooth,BLE,BluetoothDevice

A:Android蓝牙连接需要以下步骤: 1. 获取蓝牙适配器 首先要获取蓝牙适配器BluetoothAdapter,可以使用以下代码: BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 2. 打开蓝牙 如果蓝牙没有被打开,需要请求用户授权打开蓝牙。可以使用以下代码: if(!bluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); } 3. 搜索蓝牙设备 要搜索蓝牙设备,我们需要注册一个广播接收器,接收系统发送的蓝牙广播。可以使用以下代码: private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // 这里可以获取到搜索到的蓝牙设备 } } }; IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(bluetoothReceiver, filter); bluetoothAdapter.startDiscovery(); 4. 连接蓝牙设备 要连接蓝牙设备,我们需要获取设备的地址,然后通过BluetoothDevice对象建立蓝牙连接。可以使用以下代码: BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuid); socket.connect(); 5. 发送和接收数据 连接成功后,可以通过socket获取输入输出流来发送和接收数据。可以使用以下代码: InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在下嗷呜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值