美文网首页
Android蓝牙设置

Android蓝牙设置

作者: 霁逸lei | 来源:发表于2025-03-25 14:07 被阅读0次

1.获取蓝牙数据

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.startDiscovery();

2.广播监听蓝牙设备数据

IntentFilter intent = new IntentFilter();
intent.addAction(BluetoothDevice.ACTION_FOUND);//搜索发现设备
intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//设备配对
intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
intent.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intent.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
getActivity().registerReceiver(searchDevices, intent);

/**
 * 蓝牙接收广播
 */
private BroadcastReceiver searchDevices = new BroadcastReceiver() {
    //接收
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            if (device == null){
                return;
            }
            String content = TextUtils.isEmpty(device.getName()) ? device.getAddress() : device.getName() + "\n" + device.getAddress();
            if (!devicesList.contains(content)) {
                devicesList.add(content);
                pairedDevicesArray.notifyDataSetChanged();
            }
        } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            bluetoothDevice = device;
            binding.tvConnect.setText(device.getName());
            binding.cardConnect.setVisibility(View.VISIBLE);
            Log.d("Bluetooth", "设备已连接: ");
        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            bluetoothDevice = null;
            binding.cardConnect.setVisibility(View.GONE);
            Log.d("Bluetooth", "设备已断开连接: ");
        }
    }
};

3.蓝牙配对和取消配对

    //设备配对
    private void pairDevice(BluetoothDevice device) {
        try {
            Method method = device.getClass().getMethod("createBond");
            method.invoke(device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //取消配对
    private boolean unpairDevice(BluetoothDevice device) {
        try {
            // 获取 BluetoothDevice 类中的 removeBond 方法
            Method removeBondMethod = BluetoothDevice.class.getMethod("removeBond");
            // 调用 removeBond 方法
            removeBondMethod.invoke(device);
            Log.d("Bluetooth", "已取消配对设备: " + device.getName());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

4.查询配对和连接设备的信息

private void queryPairedDevices() {
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            Log.d("Bluetooth", "已绑定设备: " + device.getName() + " - " + device.getAddress());
        }
    }
}

private BluetoothProfile a2dpProfile;// 用于音频设备(如耳机、音箱)
private BluetoothProfile headsetProfile;// 用于耳机
private void queryConnectedDevices() {
    mBluetoothAdapter.getProfileProxy(getActivity(), new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            if (profile == BluetoothProfile.A2DP) {
                a2dpProfile = proxy;
                showConnectedDevices(a2dpProfile);
            } else if (profile == BluetoothProfile.HEADSET) {
                headsetProfile = proxy;
                showConnectedDevices(headsetProfile);
            }
        }

        @Override
        public void onServiceDisconnected(int profile) {
            if (profile == BluetoothProfile.A2DP) {
                a2dpProfile = null;
            } else if (profile == BluetoothProfile.HEADSET) {
                headsetProfile = null;
            }
        }
    }, BluetoothProfile.A2DP); // 或者使用 BluetoothProfile.HEADSET
}

5.权限

    <!--蓝牙-->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

相关文章

网友评论

      本文标题:Android蓝牙设置

      本文链接:https://www.haomeiwen.com/subject/rnfrvjtx.html