I am trying to make an intent call from Bluetooth Low Energy Callback function onCharacteristicRead(). Please suggest me where i am wrong .
I am not sure if it is related to the context which we give to the intent class or some thing else.
It is showing me below error :
"Unable to start activity ComponentInfo::java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"
Below is the code sample :
package com.example.pushkara.msable;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.ConsoleHandler;
@TargetApi(18)
public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter mBluetoothAdapter;
    private int REQUEST_ENABLE_BT = 1;
    private Handler mHandler;
    private static final long SCAN_PERIOD = 5000;
    private BluetoothLeScanner mLEScanner;
    private ScanSettings settings;
    private List<ScanFilter> filters;
    private BluetoothGatt mGatt;
    private  MsaScanCallback mScanCallback;
    private Button mCheckInButton;
    private Button mCheckOutButton;
    private Button mNewButton;
    private Boolean buttonvalue = false;
    public static TextView mSecurityCode;
    public static TextView rssiNdistance;
    public final static UUID UUID_MSA_APP =  UUID.fromString("13333333-3333-3333-3333-333333333337");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCheckInButton = (Button)findViewById(R.id.checkIn);
        mCheckOutButton = (Button)findViewById(R.id.checkOut);
        mSecurityCode= (TextView)findViewById(R.id.securityCode);
        rssiNdistance= (TextView)findViewById(R.id.rssiNdistance);
        mCheckOutButton.setOnClickListener(new MsaCheckInCheckout(false));
        mCheckInButton.setOnClickListener(new MsaCheckInCheckout(true));
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("This app needs location access");
                    builder.setMessage("Please grant location access so this app can detect msa server.");
                    builder.setPositiveButton(android.R.string.ok, null);
                    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @TargetApi(Build.VERSION_CODES.M)
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
                        }
                    });
                    builder.show();
                }
            }
            mHandler = new Handler();
            if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
                Toast.makeText(this, "BLE Not Supported",
                        Toast.LENGTH_SHORT).show();
                finish();
            }
            final BluetoothManager bluetoothManager =
                    (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();
        }
        catch(Exception ex){
            Log.i("msable"," [@@MA]::::Exception 1" +ex.toString());
            mSecurityCode.setText("[@@MA]Exc1" + ex.toString());
        }
    }
    private final ConnectDevice connectToDeviceCallback = new ConnectDevice() {
        @Override
        public void OnConnectToDevice(BluetoothDevice device){
               connectToDevice(device);
        }
    };
    public class MsaCheckInCheckout implements View.OnClickListener {
        private boolean mCheckIn;
        public MsaCheckInCheckout(boolean isCheckIn){
            mCheckIn =isCheckIn;
        }
        @Override
        public void onClick (View v) {
            try {
                // your code here;
                Log.i("CLICK", mCheckIn + "");
                if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
                } else {
                    if (Build.VERSION.SDK_INT >= 21) {
                        if (mGatt != null) {
                            mGatt.close();
                            mGatt = null;
                        }
                        mScanCallback = new MsaScanCallback(connectToDeviceCallback);
                        mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
                        settings = new ScanSettings.Builder()
                                .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
                                .build();
                        ScanFilter filter = new ScanFilter.Builder().setServiceUuid(new ParcelUuid(UUID_MSA_APP)).build();
                        filters = new ArrayList<>();
                        filters.add(filter);
                    }
                    scanLeDevice(true);
                }
            }
            catch(Exception ex){
                Log.i("msable","[@@MA] ::::Exception  2" +ex.toString());
                mSecurityCode.setText("[@@MA]Ex2" + ex.toString());
            }
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        try {
            switch (requestCode) {
                case 1: {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        Log.d("1", "coarse location permission granted");
                    } else {
                        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Functionality limited");
                        builder.setMessage("Since location access has not been granted, this app will not be able to discover msa server");
                        builder.setPositiveButton(android.R.string.ok, null);
                        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
                            @Override
                            public void onDismiss(DialogInterface dialog) {
                            }
                        });
                        builder.show();
                    }
                }
            }
        }
        catch(Exception ex){
            Log.i("msable","[@@MA] ::::Exception  3" +ex.toString());
            mSecurityCode.setText("[@@MA]Ex3" + ex.toString());
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
            scanLeDevice(false);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mGatt == null) {
            return;
        }
        mGatt.close();
        mGatt = null;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            if (requestCode == REQUEST_ENABLE_BT) {
                if (resultCode == Activity.RESULT_CANCELED) {
                    //Bluetooth not enabled.
                    finish();
                    return;
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
        catch(Exception ex){
            Log.i("msable","[@@MA] ::::Exception  4" +ex.toString());
            mSecurityCode.setText("[@@MA]Ex4" + ex.toString());
        }
    }
    private void scanLeDevice(final boolean enable) {
        try {
            Log.i("BLE", "**** scanLeDevice");
            if(mBluetoothAdapter != null || mLEScanner !=null) {
                Log.i("BLE", "**** Object not null proceed to stop scan");
                if (enable) {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (Build.VERSION.SDK_INT < 21) {
                                mBluetoothAdapter.stopLeScan(mLeScanCallback);
                                Log.i("BLE", "mBluetoothAdapter STOP SCAN");
                            } else {
                                Log.i("BLE", "mLEScanner STOP SCAN");
                                if(mLEScanner !=null) {
                                    Log.i("BLE", "mLEScanner not null proceed for stop ");
                                }
                                else
                                {
                                    Log.i("BLE", "mLEScanner is null !!!!!");
                                }
                                mLEScanner.stopScan(mScanCallback);
                            }
                        }
                    }, SCAN_PERIOD);
                    if (Build.VERSION.SDK_INT < 21) {
                        mBluetoothAdapter.startLeScan(mLeScanCallback);
                        Log.i("BLE", "mBluetoothAdapter START SCAN");
                    } else {
                        Log.i("START SCAN", "START SCAN");
                        mLEScanner.startScan(filters, settings, mScanCallback);
                    }
                } else {
                    if (Build.VERSION.SDK_INT < 21) {
                        mBluetoothAdapter.stopLeScan(mLeScanCallback);
                    } else {
                        mLEScanner.stopScan(mScanCallback);
                    }
                }
            }
            else
            {
                Log.i("BLE","**** Object is null");
            }
        }
        catch(Exception ex){
            Log.i("msable","[@@MA] ::::Exception  5" +ex.toString());
            mSecurityCode.setText("[@@MA]Ex5" + ex.toString());
        }
    }
    double getDistance(int rssi) {
         return Math.pow(10d, ((double)  -52 - rssi) / (10 * 2));
    }
    private BluetoothAdapter.LeScanCallback mLeScanCallback =
            new BluetoothAdapter.LeScanCallback() {
                @Override
                public void onLeScan(final BluetoothDevice device, final int rssi,
                                     byte[] scanRecord) {
                    try {
                            Log.i("BLE","**** onLeScan LeScanCallback");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.i("onLeScan", device.toString());
                                double distance = getDistance(rssi);
                                rssiNdistance.setText("rssi:"+rssi+" distance:"+distance);
                                if (distance <= 1) {
                                    connectToDevice(device);
                                }
                                else{
                                    mSecurityCode.setText("Out of Range!!!");
                                }
                            }
                        });
                    }
                    catch(Exception ex){
                        Log.i("msable","[@@MA] ::::Exception  6" +ex.toString());
                        mSecurityCode.setText("[@@MA]Ex6" + ex.toString());
                    }
                }
            };
    public void connectToDevice(BluetoothDevice device) {
        Log.i("BLE","**** connectToDevice");
        if (mGatt == null) {
            mGatt = device.connectGatt(this, false, gattCallback);
            scanLeDevice(false);// will stop after first device detection
        }else{
            Log.i("gatt status","ATT");
        }
    }
    private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            try {
                Log.i("BLE","**** gattCallback onConnectionStateChange Status: "+status);
                Log.i("onConnectionStateChange", "Status: " + status);
                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTED:
                        Log.i("gattCallback", "STATE_CONNECTED");
                        gatt.discoverServices();
                        break;
                    case BluetoothProfile.STATE_DISCONNECTED:
                        Log.e("gattCallback", "STATE_DISCONNECTED");
                        break;
                    default:
                        Log.e("gattCallback", "STATE_OTHER");
                }
            }
            catch(Exception ex){
                Log.i("msable","[@@MA] ::::Exception  7" +ex.toString());
                mSecurityCode.setText("[@@MA]Ex7" + ex.toString());
            }
        }
        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            try {
                Log.i("BLE","**** onServicesDiscovered Status: "+status);
                List<BluetoothGattService> services = gatt.getServices();
                for (BluetoothGattService bg : services) {
                    Log.i(bg.toString(), bg.getUuid().toString());
                    if (UUID_MSA_APP.equals(bg.getUuid())) {
                        Log.i("MSA APP", "found");
                        BluetoothGattCharacteristic msacode = bg.getCharacteristics().get(0);
                        //gatt.setCharacteristicNotification(msacode, true);
                        gatt.readCharacteristic(msacode);
                        Log.i("BLE", "**** BluetoothGattCharacteristic msacode: " + msacode.toString());
                    }
                    else{
                        Log.i("MSA APP", "UUID_MSA_APP not Matching found");
                    }
                }
            }
            catch(Exception ex){
                Log.i("msable","[@@MA] ::::Exception  8" +ex.toString());
                mSecurityCode.setText("[@@MA]Ex8" + ex.toString());
            }
        }
        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic
                                                 characteristic, int status) {
            try {
                Log.i("BLE","**** onCharacteristicRead Status: "+status);
                final byte[] data = characteristic.getValue();
                if (data != null && data.length > 0) {
                    final StringBuilder stringBuilder = new StringBuilder(data.length);
                    for (byte byteChar : data)
                        stringBuilder.append(String.format("%02X ", byteChar));
                    Log.i("onCharacteristicRead", new String(data));
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mSecurityCode.setText(new String(data));
                            Intent intent = new Intent(MainActivity.this, newActivity.class);
                            intent.putExtra("UniqueCode", new String(data));
                            startActivity(intent);
                        }
                    });
                }
                gatt.disconnect();
            }
            catch(Exception ex){
                Log.i("msable","[@@MA] ::::Exception  9" +ex.toString());
                mSecurityCode.setText("[@@MA]Ex9" + ex.toString());
            }
        }
    };
}
 
    