1、在AndroidM文件中,添加以下权限:
(资料图)
<
uses
-
permission android
:
name
=
""
/
>
<
uses
-
permission android
:
name
=
""
/
>
1、在代码中使用BluetoothAdapter类获取本地蓝牙适配器,并使用startDiscovery方法开始搜索设备:
BluetoothAdapter bluetoothAdapter
=
BluetoothAdapter
.
getDefaultAdapter
;">)
;
bluetoothAdapter
.
startDiscovery
;">)
;
1、在BroadcastReceiver类中,监听ACTION_FOUND和ACTION_DISCOVERY_FINISHED广播,获取搜索到的设备信息:
public
class
MyBroadcastReceiver
extends BroadcastReceiver
{
@Override
public void onReceive
;">,
Intent intent
)
{
String action
=
intent
.
getAction
;">)
;
if
;">.
ACTION_FOUND
.
equals
;">)
)
{
BluetoothDevice device
=
intent
.
getParcelableExtra
;">.
EXTRA_DEVICE
)
;
String name
=
device
.
getName
;">)
;
String address
=
device
.
getAddress
;">)
;
//
if
;">!=
null
&
&
name
.
equals
;">"Redmi Buds 4 Pro"
)
)
{
//
弹出适配弹窗
}
else
if
;">!=
null
&
&
address
.
startsWith
;">"50:7D:5F"
)
)
{
//
弹出适配弹窗
}
1、在弹出适配弹窗时,可以使用AlertDialog类创建对话框,提示用户进行适配操作:
AlertDialog
.
Builder builder
=
new AlertDialog
.
Builder
;">)
;
builder
.
setTitle
;">"适配红米Buds 4 Pro"
)
;
builder
.
setMessage
;">"是否适配该设备?"
)
;
builder
.
setPositiveButton
;">"适配"
,
new DialogInterface
.
OnClickListener
;">)
{
@Override
public void onClick
;">,
int
which
)
{
//
执行适配操作
}
}
)
;
builder
.
setNegativeButton
;">"取消"
,
null
)
;
builder
.
show
;">)
;
1、如果用户选择适配,可以使用BluetoothDevice类的createBond方法进行配对操作:
device
.
createBond
;">)
;
X 关闭
-
达沃斯论坛顺利举行,官方指定赞助用车的领克09EM-P闪耀世界
2023-07-11
-
河北所有万达广场(河北省万达广场名单一览
2023-07-11
-
温网:阿尔卡拉斯晋级八强
2023-07-11
-
扭力扳手使用方法和注意事项(图解) 扭力扳手怎么用
2023-07-11
-
【网格化管理】科技助力应急防汛,他们这样筑牢城市安全堤坝
2023-07-11