简体   繁体   English

android:蓝牙不会发现设备

[英]android: Bluetooth won't discover devices

I'm writing an Android-App, which is supposed to discover devices via Bluetooth. 我正在编写一个Android应用程序,应该可以通过蓝牙发现设备。

I don't get any exceptions, but the devices are just not found, even though my windows pc finds them (and can be found itself). 我没有任何例外,但是即使我的Windows PC找到了这些设备(也可以自己找到),却找不到这些设备。

I'm certain they are BLE, but I tried both ways. 我确定它们是BLE,但是我尝试了两种方法。 And of course I tried them separately. 当然,我分开尝试了。

Here is my ListActivity , which searches for the devices: 这是我的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();
}

The logs say after Scan mLeDeviceListAdapter: 0 and after Scan isEmpty(): true . 日志在Scan mLeDeviceListAdapter:0 之后Scan 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();
    }
};

That log ( onScanResult ) is never called. 永远不会调用该日志( onScanResult )。

And for not BLE: private final BroadcastReceiver mReceiver = new BroadcastReceiver() { //TODO not ble example public void onReceive(Context context, Intent intent) { String action = intent.getAction(); 对于非BLE:私有最终的BroadcastReceiver mReceiver = new BroadcastReceiver(){//待办事项示例public void onReceive(Context context,Intent intent){字符串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());
        }
    }
};

Not BLE discovry finishes is printed and so is Not BLE discovry starts . Not BLE Discovry表面处理已打印,因此Not BLE Discovry开始 Found device is never printed. 找到的设备从不打印。 But I'm positiv that the devices are BLE. 但是我认为这些设备是BLE。

I have the necessary permissions in my manifest: 我在清单中拥有必要的权限:

<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" />

What am I doing wrong? 我究竟做错了什么?

Edit: i really need help. 编辑:我真的需要帮助。 Thinks I tried/checked so far: 认为我到目前为止尝试过/检查过:

  • Enabled Bluetooth adapter. 已启用蓝牙适配器。
  • Bluetooth usage permissions granted. 授予蓝牙使用权限。
  • Location permission (ACCESS_COARSE and ACCESS_FINE) granted. 已授予位置权限(ACCESS_COARSE和ACCESS_FINE)。
  • Made sure gps provider is enabled (Smartphone GPS settings). 确保启用了gps提供程序(智能手机GPS设置)。

Maybe because you didn't request ACCESS_COARSE_LOCATION in onCreate 可能是因为您没有在onCreate请求ACCESS_COARSE_LOCATION

This project is a good resource for you to start your Bluetooth scanner: 该项目是启动蓝牙扫描仪的良好资源:

https://github.com/joelwass/Android-BLE-Scan-Example https://github.com/joelwass/Android-BLE-Scan-Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到蓝牙设备Xamarin Android - Can't discover bluetooth devices Xamarin Android Android蓝牙发现和显示设备 - Android Bluetooth Discover and Show Devices Android发现片段中的蓝牙设备 - Android Discover Bluetooth Devices in Fragment 是否有系统动作来发现和配对Android中的蓝牙设备? - Is there a system action to discover and pair Bluetooth devices in Android? 需要以编程方式打开蓝牙并在Android中发现设备 - Need to programmatically open bluetooth and discover devices in Android 如何在 Unity Android 插件中发现蓝牙设备? - How to discover Bluetooth devices in a Unity Android Plugin? Android 6.0蓝牙程序无法发现任何可用的蓝牙设备 - Android 6.0 Bluetooth Program Cannot Discover Any Available Bluetooth Devices 我的华为 P30 lite 在我的 android 工作室应用程序中没有发现其他蓝牙设备 - My Huawei P30 lite doesn't discover other bluetooth devices in my android studio app 如何在android中以编程方式发现蓝牙设备并在Listview中显示 - How to discover Bluetooth devices Programatically in android and display in Listview iPhone蓝牙SDK-发现Android / Windows Phone 7设备和后台发现 - iPhone Bluetooth SDK - discover Android / Windows phone 7 devices and background discovery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM