无法调用fetchUuidsWithSdp方法.java.lang.NullPointerException:空接收者

时间:2020-05-02 00:42:12

标签: java android bluetooth broadcastreceiver

我正在尝试从缓存中获取蓝牙名称,因此我使用此方法:

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);

我的问题是为什么我会收到此错误,并且我的代码出了什么问题,请帮忙!

0 个答案:

没有答案