栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android蓝牙开发(1):发现设备失败,startDiscovery()返回结果为false

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android蓝牙开发(1):发现设备失败,startDiscovery()返回结果为false

原因:需要配置相关权限,并且位置权限需要进行动态申请

Android官方蓝牙指南

https://developer.android.google.cn/guide/topics/connectivity/bluetooth#SettingUp

配置权限:AndroidManifest.xml
    
    
    
    
    
    
    
在代码中动态申请位置权限:MainActivity.java

ps:关于动态申请权限的成功/失败处理自行百度

        //判断是否有访问位置的权限,没有权限,直接申请位置权限
        if ((checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                || (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }
广播类:BluetoothBroadcastReceiver.java
public class BluetoothBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "BluetoothBroadcastRecei";

    @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);
            Log.e(TAG, "设备名字:" + device.getName() + "n设备地址:" + device.getAddress());
        }
    }
}
检测及开启蓝牙功能:MainActivity.java
@Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //判断是否有访问位置的权限,没有权限,直接申请位置权限
        if ((checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
                || (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 200);
        }

        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        receiver = new BluetoothBroadcastReceiver();
        registerReceiver(receiver, filter);
        
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        
        if(bluetoothAdapter!=null){
            // 如果设备存在蓝牙功能
            if(bluetoothAdapter.isEnabled()){
                // 如果蓝牙功能已开启
                if(bluetoothAdapter.isDiscovering()){
                    // 如果正在执行“发现设备”操作
                    bluetoothAdapter.cancelDiscovery();
                }
                else {
                    // 开始“发现设备”!
                    bluetoothAdapter.startDiscovery();
                }
            }
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();

        // Don't forget to unregister the ACTION_FOUND receiver.
        unregisterReceiver(receiver);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584150.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号