Android 蓝牙开发基础操作

本文详细介绍了在Android平台上进行蓝牙开发的基本步骤,包括权限设置、蓝牙设备的发现与配对、蓝牙状态监听等内容。通过实例展示了如何实现蓝牙设备的扫描、连接及配对等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 蓝牙开发权限
<!-- 允许程序进行发现和配对新的蓝牙设备-->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!--  允许程序连接配对过的蓝牙设备-->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <!-- 在 Android 6.0 及以上,还需要打开位置权限。如果应用没有位置权限,蓝牙扫描功能不能使用(其它蓝牙操作例如连接蓝牙设备和写入数据不受影响)-->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  • 初始化操作
BluetoothAdapter  mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

开启关闭蓝牙

第一种开启蓝牙的方法

 bluetoothAdapter.enable();//这个方法没有提示窗口,直接开启蓝牙

第二种开启蓝牙的方法

    Intent request = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(request,100);

这种开启方法有交互界面,用户可以选择是否开启蓝牙

关闭蓝牙

 bluetoothAdapter.disable();
  • 扫描蓝牙设备
 if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {

            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            mBluetoothAdapter.startDiscovery();
        }
  • 注册蓝牙接收者处理蓝牙各种状态监听
  /**
     * 注册蓝牙广播接收者
     */
    private void registBluetoothBroadcastReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        context.registerReceiver(mBroadcastReceiver, filter);
    }
  • 广播接受者

BluetoothDevice.ACTION_FOUND:每当扫描到蓝牙设备时,就会走这里

 case BluetoothDevice.ACTION_FOUND:
                    //<editor-fold>
                    device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int deviceClassType = device.getBluetoothClass().getDeviceClass();
                    //找到指定的蓝牙设备
                    if ((deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET
                            || deviceClassType == BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES)
                            && device.getName().equals(PubUtils.deviceName)) {
                        Log.i(TAG, "Found device:" + device.getName());
                        mBluetoothDevice = device;
                        //start bond,开始配对
                        startMarth();
                    } else {
                        Toast.makeText(context.getApplicationContext(), "找不到设备,请确保设备开启被搜寻模式", Toast.LENGTH_LONG).show();
                    }
                    if (dialog_match != null && dialog_match.isShowing()) {
                        dialog_match.dismiss();
                    }
                    //</editor-fold>
                    break;

BluetoothAdapter.ACTION_STATE_CHANGED:蓝牙状态:蓝牙开启关闭时会走这里

 case BluetoothAdapter.ACTION_STATE_CHANGED:
                    //<editor-fold>
                    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                    switch (state) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Log.i(TAG, "BluetoothAdapter is turning on.");
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Log.i(TAG, "BluetoothAdapter is on.");
                            //蓝牙已打开,开始搜索并连接service
                            startDiscovery();
                            getBluetoothA2DP();
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Log.i(TAG, "BluetoothAdapter is turning off.");
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Log.i(TAG, "BluetoothAdapter is off.");
                            break;
                    }

BluetoothDevice.ACTION_BOND_STATE_CHANGED:设备配对时各个状态

 case BluetoothDevice.ACTION_BOND_STATE_CHANGED://配对状态
                    //<editor-fold>
                    int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE);
                    device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    switch (bondState) {
                        case BluetoothDevice.BOND_BONDED:  //配对成功
                            Log.i(TAG, "Device:" + device.getName() + " bonded.");
                            mBluetoothAdapter.cancelDiscovery();  //取消搜索
                            PubUtils.bluetoothDevices.setMatchStatus("已配对");
                            greendaoUtil.updateEntity(PubUtils.bluetoothDevices);
                            List<BluetoothDevices> arrays = greendaoUtil.queryAllBluetoothDevices();
                            adapter.setDate(arrays);
                            connect();  //连接蓝牙设备
                            break;
                        //正在配对
                        case BluetoothDevice.BOND_BONDING:
                            Log.i(TAG, "Device:" + device.getName() + " bonding.");
                            break;
                        //配对不成功
                        case BluetoothDevice.BOND_NONE:
                            Log.i(TAG, "Device:" + device.getName() + " not bonded.");
                            //不知道是蓝牙耳机的关系还是什么原因,经常配对不成功
                            //配对不成功的话,重新尝试配对
                            startMarth();
                            break;
                        default:
                            break;

                    }

  • 蓝牙配对
 /**
     * 开始配对
     */
    private void startMarth() {
        Log.i(TAG, "createBond");
        mBluetoothDevice.createBond();
    }
  • 查看已配对的蓝牙设备
  Set<BluetoothDevice> localSet = bluetoothAdapter.getBondedDevices();

查看手机有没有连接蓝牙耳机

/**
     * 查看手机有没有已经连接的蓝牙
     * @return
     */
    public boolean blootoothConnected() {
        if (BluetoothProfile.STATE_CONNECTED == mBluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)){
            return true;
        }else{
            return false;
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值