在AndroidManifest.xml中添加权限
<!--开启蓝牙权限-->
<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" />
布局
list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:layout_margin="4dp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:layout_margin="4dp"
android:text="TextView" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启蓝牙"
android:textSize="24sp"></Button>
<Button
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索设备"
android:textSize="24sp"></Button>
<Button
android:id="@+id/linked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已连接"
android:textSize="24sp"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp">
</ListView>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启蓝牙"
android:textSize="24sp"></Button>
<Button
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="搜索设备"
android:textSize="24sp"></Button>
<Button
android:id="@+id/linked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="已连接"
android:textSize="24sp"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp">
</ListView>
</LinearLayout>
</LinearLayout>
设置蓝牙
开启蓝牙
listView = findViewById(R.id.listView);
link= findViewById(R.id.link);
link.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT) {
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "成功打开蓝牙", Toast.LENGTH_SHORT).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "打开蓝牙失败", Toast.LENGTH_SHORT).show();
}
}
}
搜索设备
search=findViewById(R.id.search);
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getPermission();
if (bluetoothAdapter != null && bluetoothAdapter.isEnabled()) {
bluetoothAdapter.startDiscovery();
}
}
});
private void getPermission() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
int permissionCheck = 0;
permissionCheck = this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
permissionCheck += this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//未获得权限
this.requestPermissions( // 请求授权
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
ACCESS_LOCATION);// 自定义常量,任意整型
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean flag = true;
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
flag = false;
}
}
switch (requestCode) {
case ACCESS_LOCATION:
if (flag) {
Log.d("test", "onRequestPermissionsResult: 用户允许权限");
} else {
Log.d("test", "onRequestPermissionsResult: 拒绝搜索设备权限");
}
break;
}
}
查询已配对的设备
linked=findViewById(R.id.linked);
linked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
listItem=new HashMap<String,Object>(); //必须在循环体里新建
listItem.put("name", deviceName);
listItem.put("mac",deviceHardwareAddress);
listData.add(listItem); //添加一条记录
}
listAdapter = new SimpleAdapter(view.getContext(),
listData,
R.layout.list, //自行创建的列表项布局
new String[]{"name","mac"},
new int[]{R.id.textView,R.id.textView2});
listView.setAdapter(listAdapter);
}
}
});
实现效果