provision failed and giving null pointer exception in android using ESP provisioning Library

11 Views Asked by At

Hello in the below code I am doing scanning for the devices .if the device found connecting via ble after 40sec resetting the device and changing the mode to data mode.But I am facing provision failed and reset is not happening and giving null pointer exception.

Can any one help me how to resolve this issue.

A.java:

String prefix = "PESPROV_" + bleName;
                    final BluetoothManager bluetoothManager =
                            (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
                    mBluetoothAdapter = bluetoothManager.getAdapter();
                    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
                        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                        context.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                    }
                    BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
                    scanDevice(context, "PESPROV_", bleName);
                    scanCallback = new ScanCallback() {
                        @Override
                        public void onScanResult(int callbackType, ScanResult result) {
                            String deviceName = result.getScanRecord().getDeviceName();
                            try {
                                if (result.getDevice() != null && !TextUtils.isEmpty(deviceName)) {
                                    // Found BLE device
                                    Log.d(TAG, "========== Device Found : " + deviceName);
                                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
                                        // TODO: Consider calling
                                        //    ActivityCompat#requestPermissions
                                        // here to request the missing permissions, and then overriding
                                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                        //                                          int[] grantResults)
                                        // to handle the case where the user grants the permission. See the documentation
                                        // for ActivityCompat#requestPermissions for more details.
                                        return;
                                    }
                                    // scanner.stopScan(scanCallback);
                                    if (TextUtils.isEmpty(prefix)) {
                                        pesManager.onPeripheralFound(result.getDevice(), result);
                                        scanner.stopScan(scanCallback);
                                    } else if (deviceName.equals("PES_" + bleName)) {
                                        scanner.stopScan(scanCallback);
                                        pesManager.initPesDeviceForSendNonce(context, bleName);
                                        String nonceStr = ""; //jObj.optString(PesConstants.NONCE);
                                        String provUrl = ""; //jObj.optString(PesConstants.PROVISIONING_URL);
                                        pesManager.sendPesNonce(provUrl, nonceStr);
                                        Timber.tag("PES").i("PesNative-Inside offlinePerformPesConnectivity method | Completed Executing sendNonce Api Call");
                                        checkAPIExecutedTime(loggingData);
                                    } else if (deviceName.startsWith(prefix)) {
                                        pesManager.onPeripheralFound(result.getDevice(), result);
                                        if (result.getScanRecord().getServiceUuids() != null && !result.getScanRecord().getServiceUuids().isEmpty()) {
                                            serviceUuid = result.getScanRecord().getServiceUuids().get(0).toString();
                                        }
                                        pesManager.connectBleDevices(result.getDevice(), serviceUuid);
                                        pesManager.getEspDevice().setProofOfPossession(pop);
                                        pesBleDevice.bleDevice = pesManager.getBleDevice(result.getDevice().getAddress());
                                        pesBleDevice.connectToBleGattServer(pesBleDevice.bleDevice, context);
                                        new Handler(Looper.getMainLooper()).postDelayed(() -> {
                                            pesBleDevice.discoverServiced();
                                        }, 1000);
                                        try {
                                            wifiAccesspointName = Constant.SSID;
                                            wifiAccesspointPassword = Constant.PASS_PHASE;
                                            try {
                                                new Handler(Looper.getMainLooper()).postDelayed(() -> {
                                                    pesManager.provision(wifiAccesspointName, wifiAccesspointPassword);
                                                }, 70000);
                                                String nonceStr = ""; //jObj.optString(PesConstants.NONCE);
                                                String provUrl = ""; //jObj.optString(PesConstants.PROVISIONING_URL);
                                                pesManager.sendPesNonce(provUrl, nonceStr);
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();

                                        }
                                    }
                                }
                                else {
                                    PesManager.getInstance(webView).sendReconnectBLEStatusSentSendNonce(2);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onBatchScanResults(List<ScanResult> results) {
                            super.onBatchScanResults(results);
                        }

                        @Override
                        public void onScanFailed(int errorCode) {
                            super.onScanFailed(errorCode);
                        }
                    };
                    scanner.startScan(scanCallback);

B.java:

 private void scanDevice(Activity context, String prefix, String deviceIdentifier) {
            Log.d("PesNative", "Scanning for BLE Devices Nearby");
            Timber.tag("PES").i("PesNative-Inside scanDevice method | Scanning for BLE Devices Nearby");
    
            if(TextUtils.isEmpty(prefix))
            {
                prefix = "PESPROV_";// Before Provisioning
            }
            pesManager.scanBleDevices(context, prefix + deviceIdentifier);
        }
c.java:
public void scanBleDevices(Activity activity, String deviceNamePrefix) {

        getProvisionManager(activity).createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_1);
        initialiseBleAdapter(activity);

        if (!hasPermissions(activity) || isScanning) {
            return;
        }

        isScanning = true;
        deviceList.clear();
        bluetoothDevices.clear();
        context = activity;

        if (checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        getProvisionManager(activity).searchBleEspDevices(deviceNamePrefix, this);
    }

D.java:

public void provision(String ssidValue,String passphraseValue) {
        Log.e("PesManager", "Password : " + passphraseValue);
        Log.e("PesManager", "ssidValue : " + ssidValue);

        Log.d(TAG, "Wifi provisioning started...");
        Timber.tag("PES").i("PesNative-Inside provision method | Wi-fi provisioning started...");
        try{
            if (provisionManager != null &&  provisionManager.getEspDevice() != null) {
                provisionManager.getEspDevice().provision(ssidValue, passphraseValue, this);
            }
        } catch (Exception e){ //Null pointer exception
            e.printStackTrace();
        }

    }
0

There are 0 best solutions below