Android蓝牙无法发现附近的设备

Android蓝牙无法发现附近的设备,android,bluetooth,android-bluetooth,Android,Bluetooth,Android Bluetooth,我正在制作一个需要连接蓝牙设备的Android应用程序。我遵循了android开发人员的文档,但bluetoothReceiver中的onReceive()函数似乎从未被调用过。就像没有找到设备一样 权限 <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

我正在制作一个需要连接蓝牙设备的Android应用程序。我遵循了android开发人员的文档,但bluetoothReceiver中的onReceive()函数似乎从未被调用过。就像没有找到设备一样

权限

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
启用蓝牙()-用于启动蓝牙

private void enableBluetooth() {
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    //Bluetooth not supported on device
    if(bluetoothAdapter == null){
        Toast.makeText(getApplicationContext(), "Device Not Supporting Bluetooth", Toast.LENGTH_SHORT).show();
    }

    //Bluetooth not enabled
    if(!bluetoothAdapter.isEnabled()){
        //Open setting to enable bluetooth
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth, REQUEST_ENABLE_BT);
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQUEST_ENABLE_BT){
        if(resultCode == RESULT_OK){
            Toast.makeText(getApplicationContext(), "Bluetooth Connected" , Toast.LENGTH_SHORT).show();
            // Register the BroadcastReceiver

            discoverBluetooth();
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(bluetoothReceiver, filter); // Don't forget to unregister during onDestroy
            boolean isStarted = bluetoothAdapter.startDiscovery();
            Toast.makeText(getApplicationContext(), "Bluetooth Discovery status:" + (isStarted? " Started" : " Not Started"), Toast.LENGTH_SHORT).show();


        }else{
            Toast.makeText(getApplicationContext(), "Bluetooth Not Connected" , Toast.LENGTH_SHORT).show();
        }
    }
}
discoverBluetooth()-添加到发现的蓝牙设备列表中,并打印到logcat

  private void discoverBluetooth() {
         bluetoothReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.d("Device action: ", action);
                if(BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    String discoveredDeviceName = device.getName() + " " + device.getAddress();
                    bluetoothList.add(discoveredDeviceName);

                    Log.d("Device: ", discoveredDeviceName);
                }
            }
        };

    }
onActivityResult()-如果用户启动蓝牙,则调用startDiscovery()并调用上面的函数discoverBluetooth()

onStop()


我认为您应该在AndroidManifest.xml中添加与蓝牙相关的权限

<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"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>


你可以在Github上参考这个项目,这是一个在android上开发BLE的谷歌示例项目:我能发现其他手机吗?你的蓝牙设备需要是可发现的。是的,当蓝牙开启时,您可以找到附近的手机。我在这里写了我关于蓝牙发现的发现。
@Override
protected void onDestroy() {
    unregisterReceiver(bluetoothReceiver);
    super.onDestroy();

}
<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"/>
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"/>