Android Studio API 29中的蓝牙扫描不起作用。
创始人
2024-08-16 14:00:16
0

要解决Android Studio API 29中蓝牙扫描不起作用的问题,可以尝试以下解决方法:

  1. 确保已添加相应的权限到AndroidManifest.xml文件中:



  1. 在代码中请求蓝牙和位置权限:
// 检查并请求蓝牙和位置权限
private void requestPermissions() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                REQUEST_PERMISSION_LOCATION);
    } else {
        // 已有权限,开始扫描
        startBluetoothScan();
    }
}

// 处理权限请求结果
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_PERMISSION_LOCATION) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // 权限被授予,开始扫描
            startBluetoothScan();
        } else {
            // 权限被拒绝,显示错误或提示信息
        }
    }
}
  1. 确保已启用蓝牙:
// 检查并启用蓝牙
private void enableBluetooth() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        // 设备不支持蓝牙
        // 显示错误或提示信息
        return;
    }
    
    if (!bluetoothAdapter.isEnabled()) {
        Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH);
    } else {
        // 蓝牙已启用,开始扫描
        startBluetoothScan();
    }
}

// 处理蓝牙启用请求结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
        if (resultCode == RESULT_OK) {
            // 蓝牙已启用,开始扫描
            startBluetoothScan();
        } else {
            // 蓝牙启用请求被拒绝,显示错误或提示信息
        }
    }
}
  1. 实现蓝牙扫描:
private void startBluetoothScan() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        // 设备不支持蓝牙
        // 显示错误或提示信息
        return;
    }
    
    // 开始蓝牙扫描
    bluetoothAdapter.startDiscovery();
    
    // 注册蓝牙扫描广播接收器
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(bluetoothReceiver, filter);
}

// 蓝牙扫描广播接收器
private final BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // 发现新设备
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // 处理设备
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            // 扫描结束
            // 处理扫描结果
        }
    }
};

// 停止蓝牙扫描
private void stopBluetoothScan() {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        // 设备不支持蓝牙
        // 显示错误或提示信息
        return;
    }
    
    if (bluetoothAdapter.isDiscovering()) {
        bluetoothAdapter.cancelDiscovery();
    }
}

// 在Activity生命周期方法中注册和注销广播接收器
@Override
protected void onResume() {
    super.onResume();
    registerReceiver(bluetoothReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    registerReceiver(bluetoothReceiver, new

相关内容

热门资讯

总结透视!HH平台挂,wepo... wepoker透视器免费 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理...
总结透视!hhpoker辅助码... 总结透视!hhpoker辅助码怎么用,wepoker有插件吗,系统教程(有挂教学)-哔哩哔哩;无需打...
办法透视!hhpoker德州机... hhpoker德州机器人开挂教程视频分享装挂详细步骤在当今的网络游戏中,hhpoker德州机器人作为...
经验透视!德州之星扫描器,we... 经验透视!德州之星扫描器,wepoker有辅助工具吗,我来教教你(有挂神器)-哔哩哔哩;无需打开直接...
教程书透视!wepoker黑侠... 您好:这款wepoker黑侠破解游戏是可以开挂的,确实是有挂的,很多玩家在这款wepoker黑侠破解...
阶段透视!wepoker可以免... 阶段透视!wepoker可以免费玩吗,wepoker好友局透视,玩家教你(今日头条)-哔哩哔哩 >>...
秘籍透视!werplan免费挂... werplan免费挂下载是一款专注玩家量身打造的游戏记牌类型软件,在werplan免费挂下载这款游戏...
步骤透视!德普之星透视免费,w... 步骤透视!德普之星透视免费,wepoker怎么提高运气,安装教程(有挂方针)-哔哩哔哩;无需打开直接...
要领透视!大菠萝免费辅助,拱趴... 要领透视!大菠萝免费辅助,拱趴大菠萝开挂方法,详细教程(有挂解惑)-哔哩哔哩您好:拱趴大菠萝开挂方法...
积累透视!wepoker有用吗... 积累透视!wepoker有用吗,we-poker辅助软件教程,透明教程(有挂透明挂)-哔哩哔哩;无需...