一、结果展示
点击“打开手机蓝牙”:
点击“允许检测”:
已配对成功:
点击关闭蓝牙:
二、核心代码
1、AndroidManifest.xml文件
在AndroidManifest.xml文件中添加如下代码来申请权限
第一行为发现其他设备
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
2、MainActivity.java文件
package com.example.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
//蓝牙适配器
private BluetoothAdapter mBluetoothAdapter;
//用来存放搜到的蓝牙
private Set<BluetoothDevice> mDevices;
private ListView mListView;
private ArrayList mList;
private ArrayAdapter mAdapter;
private TextView mConnectedView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//自定义方法初始化UI控件
initView();
initData();
}
private void initData() {
//实例化蓝牙适配器
mBluetoothAdapter =BluetoothAdapter.getDefaultAdapter();
}
private void initView() {
// mListView = (ListView) findViewById(R.id.lv_bluetooth_name);
// mConnectedView 指的是已被连接的(可用的蓝牙)
mConnectedView = (TextView) findViewById(R.id.tv_bluetooth_connected);
}
public void onClick(View view) {
if (view != null) {
switch (view.getId()){
/**
* 选择打开开启蓝牙。但是当选择它,蓝牙将不会被打开。
* 事实上它会询问许可,以启用蓝牙。
*/
case R.id.bt_bluetooth_on:
/**
* 判断BluetoothAdapter 是否已经在准备状态,没有的话,就打开
*/
if (!mBluetoothAdapter.isEnabled()) {
//调用下列蓝牙ACTION_REQUEST_ENABLE的意图
Intent turnOn =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn,0);
Toast.makeText(MainActivity.this,
"turn on", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,
"Already on", Toast.LENGTH_LONG).show();
}
break;
/**
* 开启许可,允许其他蓝牙设备120秒内可以搜索到该设备
* 选择设置可见按钮来打开视图。
* 下面的屏幕会出现要求许可才能打开发现120秒
*/
case R.id.bt_bluetooth_visible:
if (mBluetoothAdapter.isEnabled()) {
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible,0);
}else{
Toast.makeText(MainActivity.this, "请先开启蓝牙", Toast.LENGTH_SHORT).show();
}
break;
/**
* 选择列表中的设备选项。它会列出倒在列表视图中的配对设备。
* 就我而言,只有一个配对设备。它如下所示。
*/
case R.id.bt_bluetooth_list:
//可以通过调用 getBondedDevices()方法来获取配对设备列表
mDevices = mBluetoothAdapter.getBondedDevices();
//在这初始化ListView是为了方便刷新,显示数据
mListView = (ListView) findViewById(R.id.lv_bluetooth_name);
mList = new ArrayList();
if (mBluetoothAdapter.isEnabled()) {
for (BluetoothDevice bd :mDevices){
mList.add(bd.getName());
}
if (mList.size() != 0){
mConnectedView.setVisibility(View.VISIBLE);
}
Toast.makeText(MainActivity.this,
"Showing Paired Devices", Toast.LENGTH_SHORT).show();
mAdapter = new ArrayAdapter(
this,android.R.layout.simple_list_item_1,mList);
mListView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
}else{
Toast.makeText(MainActivity.this, "请先开启蓝牙", Toast.LENGTH_SHORT).show();
}
break;
/**
* 选择关闭按钮来关闭蓝牙。
* 当关掉蓝牙指示成功切换关闭蓝牙会出现以下消息
*/
case R.id.bt_bluetooth_off:
//判断蓝牙是否关闭
if (mBluetoothAdapter.isEnabled()){
//未关闭
mBluetoothAdapter.disable();
mList.clear();
mConnectedView.setVisibility(View.INVISIBLE);
mAdapter.notifyDataSetChanged();
}
Toast.makeText(getApplicationContext(),"蓝牙已关闭" ,
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
3、activity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scv_bluetooth_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_bluetooth_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/bt_bluetooth_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="打开手机蓝牙"/>
<Button
android:id="@+id/bt_bluetooth_visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="允许检测性"/>
<Button
android:id="@+id/bt_bluetooth_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="打开已配对列表"/>
<Button
android:id="@+id/bt_bluetooth_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="关闭蓝牙"/>
<TextView
android:visibility="invisible"
android:id="@+id/tv_bluetooth_connected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已配对设备"/>
<ListView
android:id="@+id/lv_bluetooth_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:visibility="invisible"
android:id="@+id/ll_bluetooth_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_bluetooth_usable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="可用设备"/>
<TextView
android:id="@+id/tv_bluetooth_touch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="触摸可配对"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
源码已上传至GitHub