当前位置: 首页 > 知识库问答 >
问题:

Android:蓝牙不会发现设备

鲁建茗
2023-03-14

我正在编写一个Android应用程序,它应该可以通过蓝牙发现设备。

我没有得到任何例外,但设备只是没有找到,即使我的windows pc找到他们(并可以找到自己)。

我肯定他们是好的,但我两种方法都试过了。当然我也分别试过了。

下面是我的ListActivity,它搜索设备:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mHandler = new Handler();

    setContentView(R.layout.bluetooth_list_view);
    listView = getListView();

    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
        finish();
    }

    final BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();
    mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    Log.d(TAG, "on Create start5");

    if (mBluetoothAdapter == null) {
        Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();
        finish();
        return;
    }

    mLeDeviceListAdapter = new LeDeviceListAdapter(this);
    setListAdapter(mLeDeviceListAdapter);
    listView.setAdapter(mLeDeviceListAdapter);

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

    registerReceiver(mReceiver, filter);
    mBluetoothAdapter.startDiscovery(); 
}    

private void scanLeDevice(final boolean enable) {
    Log.e(TAG, "scanLeDevice: " + enable);
    if (enable) {
        // Stops scanning after a pre-defined scan period.
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.e(TAG, "after Scan mLeDeviceListAdapter: " + mLeDeviceListAdapter.getCount());
                Log.e(TAG, "after Scan isEmpty(): " + mLeDeviceListAdapter.isEmpty());
                mScanning = false;
                mBluetoothLeScanner.stopScan(mScanCallback);
                invalidateOptionsMenu();
            }
        }, SCAN_PERIOD);

        mScanning = true;
        mBluetoothLeScanner.startScan(mScanCallback);

    } else {
        mScanning = false;
        mBluetoothLeScanner.stopScan(mScanCallback);
    }
    invalidateOptionsMenu();
}

日志显示扫描后MLEDeviceListAdapter:0和扫描后isEmpty():true。

ScanCallback mScanCallback = new ScanCallback() {
    @Override
    public void onScanResult(int callbackType, ScanResult result) {
        Log.e(TAG, "onScanResult: " + result);
        super.onScanResult(callbackType, result);
        mLeDeviceListAdapter.addDevice(result.getDevice());
        mLeDeviceListAdapter.notifyDataSetChanged();
    }
};

则从不调用该日志(onScanResult)。

对于not ble:private final BroadcastReceiver mReceiver=new BroadcastReceiver(){//TODONOT ble示例public void onReceive(Context Context,Intent Intent){String action=Intent.getAction();

        if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            Log.e(TAG, "Not BLE discovry starts");
            //discovery starts, we can show progress dialog or perform other tasks
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            Log.e(TAG, "Not BLE discovry finishes");
            //discovery finishes, dismis progress dialog
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            //bluetooth device found
            BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            Log.e(TAG, "Found device " + device.getName());
        }
    }
};

不是可可的迪斯科结束印刷,所以也不是可可的迪斯科开始。从未打印找到的设备。但我肯定这些装置是可行的。

我在清单中具有必要的权限:

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

我做错了什么?

编辑:我真的需要帮助。认为到目前为止我已经试过/检查过了:

  • 已启用蓝牙适配器。
  • 已授予蓝牙使用权限。
  • 已授予位置权限(access_coaster和ACCESS_FINE)。
  • 确保已启用gps提供程序(智能手机gps设置)。

共有1个答案

锺功
2023-03-14

可能是因为您没有在oncreate中请求ACCESS_COARSE_LOCATION

这个项目是您启动蓝牙扫描仪的一个很好的资源:

https://github.com/joelwass/android-ble-scan-example

 类似资料:
  • 我正在开发一个蓝牙4.0低能耗应用程序来快速扫描周围的广告专用设备(每秒10次或更多),并用RSSI获取他们的地址。根据Android蓝牙应用编程接口,发现是异步的,大约需要12秒。有什么办法可以设置这个时间吗?或者可以使用HCI或蓝牙堆栈直接与设备通信?

  • 我目前正在开发一个小应用程序,以开始使用蓝牙Android应用编程接口可以提供的服务。 编辑- 问题似乎是由特定的Nexus 5设备造成的。他们的蓝牙接收器好像不太好用。以下解决方案适用于其他设备 备注: > 我已经完成了几乎所有我感兴趣的功能(例如检查适配器是否存在,启用/禁用蓝牙,查询成对的部分,设置适配器可发现)。 问题: 实际上,当我启动. onDiscovery()方法时,没有找到设备,

  • 我正在创建一个蓝牙扫描仪应用程序,并试图找到可用的设备配对。我有一个蓝牙耳机,我正试图找到运行android 10应用程序。 权限在清单中设置 应用程序包含一个简单的按钮,点击它我开始发现蓝牙设备 用于发现的回调 如果我错过了什么,有人能帮我吗?

  • 我如何获得Android所有已连接蓝牙设备的列表,而不考虑配置文件? 或者,我看到您可以通过BluetoothManager获取特定配置文件的所有连接设备。获取连接的设备。 我想我可以通过ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED监听连接/断开来查看哪些设备连接...似乎容易出错。 但我想知道是否有更简单的方法来获取所有已连接蓝牙设备的列表。

  • 我正在编写一个应用程序来打开蓝牙,扫描附近可发现的蓝牙设备,包括配对和未配对,以便将它们添加到列表中。 这是我的代码: 代码看起来很好,但它没有找到或添加任何附近的设备到列表中,当我退出应用程序时,我得到以下异常日志: 有解决问题的办法吗? 要将配对设备添加到列表视图,我必须添加一个按钮并将其与scan()方法相关联。这很令人沮丧,当我点击“打开”按钮时,我想填充列表。此外,仍然需要找到未配对的设

  • 有没有办法在Android上ping蓝牙设备?Android中没有连接或配对蓝牙设备,但我事先知道设备的MAC地址和PIN。我试图实现的是ping一个MAC地址列表,看看是否有任何设备在范围内。