Android 蓝牙开发(扫描设备、绑定、解绑)

本文介绍了如何在Android应用中进行蓝牙设备扫描、绑定和解绑的操作。首先展示了扫描设备的界面布局代码,接着创建了显示设备列表的item布局和适配器。然后通过BroadcastReceiver监听蓝牙设备的状态变化,并处理设备的发现、绑定状态改变等事件。文中还提到了状态栏颜色设置的工具类和动态权限申请。最后,提供了获取已绑定设备、创建和移除配对的代码实现。

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

android:id=“@+id/scan_devices”

android:layout_width=“match_parent”

android:layout_height=“50dp”

android:background=“?android:attr/selectableItemBackground”

android:gravity=“center”

android:text=“扫描蓝牙” />

在layout下创建列表展示的item的布局文件,名为item_device_list.xml

在这里插入图片描述

代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:orientation=“vertical”

android:id=“@+id/item_device”

android:background=“?android:attr/selectableItemBackground”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<LinearLayout

android:gravity=“center_vertical”

android:padding=“12dp”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”>

<ImageView

android:id=“@+id/iv_device_type”

android:src=“@mipmap/icon_bluetooth”

android:layout_width=“30dp”

android:layout_height=“30dp”/>

<TextView

android:id=“@+id/tv_name”

android:paddingLeft=“12dp”

android:textSize=“16sp”

android:text=“设备名称”

android:textColor=“#000”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<TextView

android:gravity=“right”

android:id=“@+id/tv_bond_state”

android:text=“绑定状态”

android:layout_width=“0dp”

android:layout_weight=“1”

android:layout_height=“wrap_content”/>

<View

android:background=“#EBEBEB”

android:layout_marginLeft=“54dp”

android:layout_width=“match_parent”

android:layout_height=“1dp”/>

三、编码


在此之前呢,记得放一个工具类,用于改变状态栏的文字和背景颜色的。创建一个util包,包下创建一个StatusBarUtil.java文件

在这里插入图片描述

工具类代码如下:

package com.llw.mybluetooth.util;

import android.annotation.TargetApi;

import android.app.Activity;

import android.graphics.Color;

import android.os.Build;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import java.lang.reflect.Field;

import java.lang.reflect.Method;

/**

  • 状态栏工具类

*/

public class StatusBarUtil {

/**

  • 修改状态栏为全透明

  • @param activity

*/

@TargetApi(19)

public static void transparencyBar(Activity activity) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

Window window = activity.getWindow();

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

window.setStatusBarColor(Color.TRANSPARENT);

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

Window window = activity.getWindow();

window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

}

}

/**

  • 状态栏亮色模式,设置状态栏黑色文字、图标,

  • 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android

  • @param activity

  • @return 1:MIUUI 2:Flyme 3:android6.0

*/

public static int StatusBarLightMode(Activity activity) {

int result = 0;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

if (MIUISetStatusBarLightMode(activity, true)) {

result = 1;

} else if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {

result = 2;

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

result = 3;

}

}

return result;

}

/**

  • 已知系统类型时,设置状态栏黑色文字、图标。

  • 适配4.4以上版本MIUIV、Flyme和6.0以上版本其他Android

  • @param activity

  • @param type 1:MIUUI 2:Flyme 3:android6.0

*/

public static void StatusBarLightMode(Activity activity, int type) {

if (type == 1) {

MIUISetStatusBarLightMode(activity, t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值