Не удалось вызвать метод fetchUuidsWithSdp. java .lang.NullPointerException: нулевой получатель - PullRequest
0 голосов
/ 02 мая 2020

Я пытаюсь получить имя Bluetooth не из кеша, и по этой причине я использую этот метод:

public static void startServiceDiscovery(BluetoothDevice device, BroadcastReceiver mScanReceiver) {
    // Need to use reflection prior to API 15
    Class cl = null;
    try {
        cl = Class.forName("android.bluetooth.BluetoothDevice");
    } catch( ClassNotFoundException exc ) {
      //  Log.e(CTAG, "android.bluetooth.BluetoothDevice not found." );
    }
    if (null != cl) {
        Class[] param = {};
        Method method = null;
        try {
            method = cl.getMethod("fetchUuidsWithSdp", param);
        } catch( NoSuchMethodException exc ) {
          //  Log.e(CTAG, "fetchUuidsWithSdp not found." );
        }
        if (null != method) {
            Object[] args = {};
            try {
                method.invoke(device, args);
                Log.d(TAG, "startServiceDiscovery: "+method.toString());
            } catch (Exception exc) {
                Log.e("fuck", "Failed to invoke fetchUuidsWithSdp method."+exc );
            }
        }
    }
}

Вот мой приемник:

private final BroadcastReceiver mScanReceiver = new BroadcastReceiver() {
    @RequiresPermission(allOf = {Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH})
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        ...

        if (BluetoothDevice.ACTION_NAME_CHANGED.equals(action)){
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_NAME);
            startServiceDiscovery(device,mScanReceiver);
        }
    }
};

И я ' Я регистрирую такие трансляции

filter = new IntentFilter(BluetoothDevice.ACTION_NAME_CHANGED);
    mConfig.context.registerReceiver(mScanReceiver, filter);

Мой вопрос: почему я получаю эту ошибку, и что не так с моим кодом, пожалуйста, помогите!

...