一、注册蓝牙操作的相关权限

1、注册权限需要在AndroidManifest.xml中注册:

android r如何关闭蓝牙的自动连接 安卓手机蓝牙自动关闭_android

2、进行蓝牙权限注册

在这里我注册了四个蓝牙相关的权限:

在这里解释一下这四个权限的作用:

android.permission.BLUETOOTH:允许程序连接到已配对的蓝牙设备,用于连接或传输数据等配对后的操作。

android.permission.BLUETOOTH_ADMIN:此权限是以注册“android.permission.BLUETOOTH”为前提的,作用是允许程序发现或配对蓝牙设备

由于Android 6.0之后采用了新的权限机制来保护用户隐私,因此需要另外添加android.permission.ACCESS_FINE_LOCATION和android.permission.ACCESS_COARSE_LOCATION权限

二、获取蓝牙设备器

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

使用getDefaultAdapter()返回一个BluetoothAdapter的实例。

三、开启蓝牙

protected voidopenBluetooth() {if (!bluetoothAdapter.isEnabled()) {//向用户发出请求,开启蓝牙设备

Intent intent = newIntent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

startActivityForResult(intent, 1);

}else{

Toast.makeText(this, "蓝牙已开启,请勿重复点击!", Toast.LENGTH_SHORT).show();

}

}

isEnable()判断蓝牙是否开启,若已开启返回true,反之则返回false。

ACTION_REQUEST_ENABLE是蓝牙相关广播的action值,用于请求用户选择是否打开蓝牙。

四、关闭蓝牙

protected voidcloseBluetooth() {if(bluetoothAdapter.isEnabled()) {

bluetoothAdapter.disable();

Toast.makeText(MainActivity.this, "已关闭蓝牙", Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(MainActivity.this, "蓝牙已关闭,请勿重复点击!", Toast.LENGTH_SHORT).show();

}

}

关闭蓝牙的操作和开启蓝牙的操作差不多,这里用到的disable()用来强制关闭蓝牙。

五、搜索蓝牙设备

1、定义两个数组用来存放蓝牙设备的信息:

public ArrayList arrayList = null;public ArrayList deviceName = null;

arrayList = new ArrayList<>();

deviceName= new ArrayList<>();

其中,arrayList用来存放蓝牙设备地址,deviceName用来存放蓝牙设备的名称和地址并且在列表中显示。

2、定义列表:此控件的使用需要用到为列表控件设置适配器,代码如下:

public static ArrayAdapter adapter = null;

ListView listView;

adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, deviceName);

listView=(ListView) findViewById(R.id.act_main_list);

listView.setAdapter(adapter);

若没有为列表设置适配器,列表在搜索时将无法显示出查找到的蓝牙设备。

3、注册广播

private voidregisterReceiver() {

IntentFilter filter= newIntentFilter();

filter.addAction(BluetoothDevice.ACTION_FOUND);

filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(bluetoothReceiver, filter);

}

4、创建广播接收器,用于接受发现蓝牙设备和查找完成的广播

private final BroadcastReceiver bluetoothReceiver = newBroadcastReceiver() {

@Overridepublic voidonReceive(Context context, Intent intent) {

String action=intent.getAction();if(BluetoothDevice.ACTION_FOUND.equals(action)) {

BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

deviceName.add("设备名:" + device.getName() + "\n" + "设备地址:" + device.getAddress() + "\n");//将搜索到的蓝牙名称和地址添加到列表。

arrayList.add(device.getAddress());//将搜索到的蓝牙地址添加到列表。

adapter.notifyDataSetChanged();//更新

}

}

};

5、搜索蓝牙

protected voidfindBluetooth() {

registerReceiver();

bluetoothAdapter.startDiscovery();

Toast.makeText(this, "搜索中...请稍后", Toast.LENGTH_SHORT).show();

}

这里使用startDiscovery()来搜索设备,且只能搜索到开启了可见性的蓝牙设备。

6、以上的开启蓝牙、关闭蓝牙和搜索蓝牙可以通过三个按钮点击事件来实现。

进入activity_main.xml中设置三个按钮控件

android r如何关闭蓝牙的自动连接 安卓手机蓝牙自动关闭_控件_02

设置按钮控件代码如下:

android:id="@+id/open"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="开启蓝牙"/>

android:id="@+id/close"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="关闭蓝牙"/>

android:id="@+idfind"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="搜索蓝牙"/>

然后再回到MainActivity中进行代码的编写:

以下代码为给三个按钮分别设置打开,关闭和搜索蓝牙三个事件。

public voidmyClick(View view){switch(view.getId()){caseR.id.open:
openBluetooth();break;caseR.id.close:
closeBluetooth();break;caseR.id.find:
findBluetooth();break;
}
}

7、连接蓝牙设备

注意:蓝牙设备的连接的整个过程需子线程中执行!

作为客户端连接:(每个设备作为一个客户端去连接一个服务端,向对方发起连接)

a、设置列表点击事件(点击后完成 b 步骤并且启动线程)

b、往线程类中传入传入BluetoothDevice的对象。此对象的值由存放远程设备的地址的集合类传入。

public staticContext context;public staticContext mge;public static BluetoothDevice device;

BluetoothAdapter adapter =BluetoothAdapter.getDefaultAdapter();

device= adapter.getRemoteDevice(arrayList.get(i));

clientThread= newClientThread(device, mge, context);

clientThread.start();

c、使用a中获取的BluetoothDevice的对象来获取BluetoothSocket的对象(在线程类中)

socket = (BluetoothSocket) device.getClass().getDeclaredMethod("createRfcommSocket", new Class[]{int.class}).invoke(device, 1);

d、建立连接(必须先取消扫描蓝牙设备再进行连接)

bluetoothAdapter.cancelDiscovery();

socket.connect();//连接

文章到这里,蓝牙设备的开启,关闭,搜索与连接完成啦!这是本人第一次写博客,若有不足之处请大家指正!

下篇文章我将写如何利用蓝牙发送数据与接收远端设备的数据,并将收到的数据进行处理实现实时改变色板的颜色。