简单的蓝牙功能打开蓝牙 关闭蓝牙 搜索蓝牙 配对蓝牙
清单文件需要加权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
BluetoothManager 蓝牙管理类,管理BluetoothAdapter。主要负责管理蓝牙的本地连接。
BluetoothAdapter 蓝牙适配器类:代表本蓝牙设备
BluetoothDevice 蓝牙设备,配对后的远程蓝牙设备
BluetoothServiceSocket 蓝牙服务器
BluetoothSocket 蓝牙客户端
蓝牙传输功能的UUID:00001101-0000-1000-8000-00805F9B34FB
蓝牙常用Action
BluetoothDevice.ACTION_FOUND 可发现蓝牙设备
BluetoothAdapter.ACTION_DISCOVERY_FINISHED 蓝牙设备的搜索结果
BluetoothDevice.ACTION_BOND_STATE_CHANGED 蓝牙设备的绑定状态
BluetoothAdapter.ACTION_REQUEST_ENABLE //启动蓝牙
BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE//开启设备可被发现
BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION//指定搜索时间
BluetoothDevice.EXTRA_DEVICE获得蓝牙设
package com.example.day10bluetool;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.day10bluetool.Lianjie.ConnecctionManager;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView listView;
private MainAdpter mainAdpter;
private List<BlueToothBean> list = new ArrayList<>();
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private BluetoothReceiver bluetoothReceiver;
BluetoothDevice bluetoothDevice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initDate();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
Method method=BluetoothDevice.class.getMethod("createBond");
method.invoke(list.get(position).bluetoo