Android:如何断开已连接的蓝牙设备?
创始人
2024-10-13 15:31:41
0

在Android中,可以使用BluetoothAdapter类的cancelDiscovery()方法来取消蓝牙设备的发现,并使用BluetoothSocket类的close()方法来断开已连接的蓝牙设备。

以下是一个简单的示例代码,展示如何断开已连接的蓝牙设备:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.IOException;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "BluetoothDisconnect";

    private BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice connectedDevice;
    private BluetoothSocket bluetoothSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化蓝牙适配器
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // 注册广播接收器以接收蓝牙连接状态的改变
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(bluetoothReceiver, filter);
    }

    // 连接蓝牙设备
    private void connectDevice(BluetoothDevice device) {
        connectedDevice = device;

        try {
            // 获取设备的UUID
            UUID uuid = device.getUuids()[0].getUuid();

            // 创建蓝牙套接字
            bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);

            // 连接蓝牙设备
            bluetoothSocket.connect();
        } catch (IOException e) {
            Log.e(TAG, "无法连接到蓝牙设备", e);
        }
    }

    // 断开蓝牙设备连接
    private void disconnectDevice() {
        try {
            // 关闭蓝牙套接字
            bluetoothSocket.close();
        } catch (IOException e) {
            Log.e(TAG, "无法关闭蓝牙套接字", e);
        }
    }

    // 广播接收器,用于接收蓝牙连接状态的改变
    private BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            // 检查是否断开了蓝牙连接
            if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                BluetoothDevice disconnectedDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // 检查是否是已连接的蓝牙设备
                if (connectedDevice != null && disconnectedDevice.getAddress().equals(connectedDevice.getAddress())) {
                    Log.d(TAG, "已断开蓝牙设备连接");
                    connectedDevice = null;
                    bluetoothSocket = null;
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();

        // 取消蓝牙发现
        if (bluetoothAdapter.isDiscovering()) {
            bluetoothAdapter.cancelDiscovery();
        }

        // 断开蓝牙设备连接
        if (connectedDevice != null) {
            disconnectDevice();
        }

        // 注销广播接收器
        unregisterReceiver(bluetoothReceiver);
    }
}

上述代码中,connectDevice()方法用于连接蓝牙设备,disconnectDevice()方法用于断开蓝牙设备连接。在Activity的onDestroy()方法中,取消蓝牙发现并断开蓝牙设备的连接。还定义了一个广播接收器来接收蓝牙连接状态的改变,并在断开连接时进行相应的处理。

相关内容

热门资讯

据统计!pokemomo辅助软... 据统计!pokemomo辅助软件,八张透视辅助,演示教程(有挂细节)1、全新机制【八张透视辅助ai辅...
明白辅助挂!红龙poker作弊... 明白辅助挂!红龙poker作弊指令,奇迹脚本辅助,大纲教程(有挂方针)1、游戏颠覆性的策略玩法,独创...
目前!德州圈脚本,德普之星辅助... 目前!德州圈脚本,德普之星辅助器,积累教程(真的有挂)1、操作简单,无需德普之星辅助器手机版透视脚本...
相较于以往!智星菠萝有挂吗,来... 相较于以往!智星菠萝有挂吗,来来拼十辅助免费辅助,方针教程(存在有挂)1、首先打开来来拼十辅助免费辅...
据通报!德扑之心免费透视,广东... 据通报!德扑之心免费透视,广东雀神挂机怎么样,步骤教程(有挂方略)1、该软件可以轻松地帮助玩家将广东...
有玩家发现!aapoker真的... 有玩家发现!aapoker真的假的,闲聚辅助器,绝活儿教程(讲解有挂)1)闲聚辅助器免费钻石:进一步...
方法辅助挂!德州局脚本,博雅红... 方法辅助挂!德州局脚本,博雅红河西元红河挂,方式教程(有挂讲解)1、博雅红河西元红河挂免费辅助多个强...
有玩家发现!扑克之星辅助,jj... 有玩家发现!扑克之星辅助,jj斗地主外卦,讲义教程(有挂分析)1、进入到jj斗地主外卦是否有挂之后,...
黑科技辅助挂!hhpoker辅... 黑科技辅助挂!hhpoker辅助,陕麻圈辅助开挂软件,妙招教程(有挂详情);1、下载好陕麻圈辅助开挂...
为切实保障!epoker透视底... 为切实保障!epoker透视底牌,哈局八张辅助,总结教程(有挂方略)1、该软件可以轻松地帮助玩家将哈...