简体   繁体   English

E / BluetoothAdapter:蓝牙活页夹为空

[英]E/BluetoothAdapter: Bluetooth binder is null

hello I have a problem with the OnePlus6, this device is detected as having no bluetooth 您好,我在OnePlus6上遇到问题,该设备被检测为没有蓝牙

here is the method that activates bluetooth: 这是激活蓝牙的方法:

protected void onResume(){
    super.onResume();
    boolean fAdapterEnabled = BluetoothAdapter.getDefaultAdapter().isEnabled();
    try {
        if (!fAdapterEnabled) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivity(intent);
        }
    }catch (Exception e){
        Log.d("bgx_dbg", "Exception caught while calling isEnabled.");
        Toast.makeText(this,"Exception caught", Toast.LENGTH_LONG).show();
    }
}

I want to clarify this code works perfectly with another smartphone 我想澄清一下此代码可与其他智能手机完美配合

here is the mistake I have: 这是我的错误:

2019-01-25 14:16:57.571 28761-28761/? 2019-01-25 14:16:57.571 28761-28761 /? E/BluetoothAdapter: Bluetooth binder is null 2019-01-25 14:16:57.590 28761-28761/? E / BluetoothAdapter:蓝牙活页夹为空2019-01-25 14:16:57.590 28761-28761 /? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.clj.blesample, PID: 28761 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.bluetooth.BluetoothAdapter.isEnabled()' on a null object reference E / AndroidRuntime:致命例外:主进程:com.clj.blesample,PID:28761 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean android.bluetooth.BluetoothAdapter.isEnabled()'

MANIFEST: 表现:

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
<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.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

From the code given above this seems to be a bluetooth adapter issue which can happen when you either don't give the application the correct permissions in the manifest shown below or the bluetooth is broken. 从上面给出的代码来看,这似乎是蓝牙适配器问题,如果您没有在下面显示的清单中给应用程序赋予正确的权限,或者蓝牙损坏了,可能会发生。 I have added code below which you should use to take into consideration of faulty bluetooth, also make a utility class which you can call this from. 我在下面添加了代码,您应该使用该代码来考虑出现故障的蓝牙,并创建一个实用程序类,您可以从中进行调用。

Note: its the bluetooth adapter null not isEnabled 注意:其蓝牙适配器的null不是isEnabled

Permissions: 权限:

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

Code: 码:

public static boolean isBluetoothEnabled() {
    final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    return (bluetoothAdapter != null && bluetoothAdapter.isEnabled());
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM