Android蓝牙应用程序可以';无法发现其他设备

Android蓝牙应用程序可以';无法发现其他设备,android,bluetooth,Android,Bluetooth,我是android的初学者,在中尝试了一个演示代码Bluetoothchat。它在我的手机(Nexus5,android 6.0.1)上运行不好。完整的软件包在github上。我没有在演示中更改任何内容。Android studio 2.1.2 它没有发现其他蓝牙设备(ipad和黑莓)。设备的可见性已打开。如果我在nexus 5上使用默认的Bluetooth查找功能,这两个设备就会显示在列表中 我还尝试了其他一些关于蓝牙发现应用程序的演示。我的手机也没用。我知道如何找到一个设备的基本想法,但找不

我是android的初学者,在中尝试了一个演示代码Bluetoothchat。它在我的手机(Nexus5,android 6.0.1)上运行不好。完整的软件包在github上。我没有在演示中更改任何内容。Android studio 2.1.2

它没有发现其他蓝牙设备(ipad和黑莓)。设备的可见性已打开。如果我在nexus 5上使用默认的Bluetooth查找功能,这两个设备就会显示在列表中

我还尝试了其他一些关于蓝牙发现应用程序的演示。我的手机也没用。我知道如何找到一个设备的基本想法,但找不到为什么它不工作

Bluetoothchat中的相关代码如下:

基本设置

    mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
    // Find and set up the ListView for newly discovered devices
    ListView newDevicesListView = (ListView) findViewById(R.id.new_devices);
    newDevicesListView.setAdapter(mNewDevicesArrayAdapter);
    newDevicesListView.setOnItemClickListener(mDeviceClickListener);

    // Register for broadcasts when a device is discovered
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

    // Register for broadcasts when discovery has finished
    filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    this.registerReceiver(mReceiver, filter);

    // Get the local Bluetooth adapter
    mBtAdapter = BluetoothAdapter.getDefaultAdapter();
发现部分

/**
 * Start device discover with the BluetoothAdapter
 */
private void doDiscovery() {
    Log.d(TAG, "doDiscovery()");

    // Indicate scanning in the title
    setProgressBarIndeterminateVisibility(true);
    setTitle(R.string.scanning);

    // Turn on sub-title for new devices
    findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);

    // If we're already discovering, stop it
    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }

    // Request discover from BluetoothAdapter
    mBtAdapter.startDiscovery();
}
广播接收机

/**
 * The BroadcastReceiver that listens for discovered devices and changes the title when
 * discovery is finished
 */
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // If it's already paired, skip it, because it's been listed already
            if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
                mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
            // When discovery is finished, change the Activity title
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            setProgressBarIndeterminateVisibility(false);
            setTitle(R.string.select_device);
            if (mNewDevicesArrayAdapter.getCount() == 0) {
                String noDevices = getResources().getText(R.string.none_found).toString();
                mNewDevicesArrayAdapter.add(noDevices);
            }
        }
    }
};
破坏部分

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

    // Make sure we're not doing discovery anymore
    if (mBtAdapter != null) {
        mBtAdapter.cancelDiscovery();
    }

    // Unregister broadcast listeners
    this.unregisterReceiver(mReceiver);
}

有人能帮我吗?提前谢谢

确保已将这些权限添加到android清单中

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
并指定在用户接受/拒绝请求时应执行的操作:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PERMISSION_BLUETOOTH: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this, "Permission granted.", Toast.LENGTH_SHORT).show();

                // Permission granted.
            } else {
                Toast.makeText(this, "Permission must be granted to use the application.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

我可以确认这也发生在我的Nexus 6P上。我添加了一个日志条目,以查看广播接收器触发了哪些操作,并且只遇到了“DISCOVERY_FINISHED”操作。有关此问题的答案,请参阅
    // Handling permissions.
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_COARSE_LOCATION)) {

            // Not to annoy user.
            Toast.makeText(this, "Permission must be granted to use the app.", Toast.LENGTH_SHORT).show();
        } else {

            // Request permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    REQUEST_PERMISSION_BLUETOOTH);
        }
    } else {
        // Permission has already been granted.
        Toast.makeText(this, "Permission already granted.", Toast.LENGTH_SHORT).show();
    }
@Override
public void onRequestPermissionsResult(int requestCode,
                                       @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_PERMISSION_BLUETOOTH: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                Toast.makeText(this, "Permission granted.", Toast.LENGTH_SHORT).show();

                // Permission granted.
            } else {
                Toast.makeText(this, "Permission must be granted to use the application.", Toast.LENGTH_SHORT).show();
            }
        }
    }
}