Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth GATT Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference

I am writing my first android app, and have come unstuck - I'm getting big issues with the reliability of the bluetooth GATT both read and write. 30 minutes ago I was able to write a byte from my phone to my peripheral, and see it received. Now I cannot. I'm begining to think this intermitent error is the one that's causing the problems:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
        at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:376)
        at com.znewsham.skiday.adapters.BluetoothLeService.broadcastUpdate(BluetoothLeService.java:152)
        at com.znewsham.skiday.adapters.BluetoothLeService.access$100(BluetoothLeService.java:50)
        at com.znewsham.skiday.adapters.BluetoothLeService$1.onServicesDiscovered(BluetoothLeService.java:118)
        at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:304)
        at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:217)
        at android.os.Binder.execTransact(Binder.java:454)

I got most of the code from the android developer examples, but modified it for my needs. If I remove the call to broadcastUpdate here, or wrap it in a try block the error will occur in a different location (e.g. onServicesDiscovered) the same error, but it only ever appears EXACTLY once.

This is where I bind the service:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == DEVICE_SCAN){
        address = data.getStringExtra("address");
        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        startService(gattServiceIntent);
        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    }
}

this is the service:

private final ServiceConnection mServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        boolean connected;
        do {
            bluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
            bluetoothLeService.attachBase(ViewSkiDayActivity.this);
            if (!bluetoothLeService.initialize()) {
                Log.e("", "Unable to initialize Bluetooth");
                finish();
            }
            // Automatically connects to the device upon successful start-up initialization.
            connected = bluetoothLeService.connect(address);
        }while(connected == false);
    }
    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        bluetoothLeService = null;
    }
};

Here is the callback handler where the error occurs:

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        String intentAction;
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            intentAction = ACTION_GATT_CONNECTED;
            mConnectionState = STATE_CONNECTED;
            try{
                broadcastUpdate(intentAction);
            }
            catch(Exception e){

            }
            Log.i(TAG, "Connected to GATT server.");
            // Attempts to discover services after successful connection.
            Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());

        } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
            intentAction = ACTION_GATT_DISCONNECTED;
            mConnectionState = STATE_DISCONNECTED;
            Log.i(TAG, "Disconnected from GATT server.");
            try{
                broadcastUpdate(intentAction);
            }
            catch(Exception e){

            }
        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
        }
    }

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        Log.w("BLARG", "write callback");
    }
};

broadcastUpdate:

private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

I'm pretty stumped, and I'm not even sure that fixing this issue will resolve my problems

like image 807
Zack Newsham Avatar asked Oct 19 '25 04:10

Zack Newsham


1 Answers

I don't know if this is going to help you, but I was having the exact same error message. For some context on my problem: I was calling 'sendBroadcast' outside of an Activity in a custom object of mine. I was able to solve by replacing this:

sendBroadcast(intent);

with this:

context.sendBroadcast(intent);

Needless to say I was keeping a reference to the context in my object, by passing it to the object in its constructor, when the activity instantiates said object for the first time:

new MyObject(this);

Hope it helps you.

like image 159
kurt Avatar answered Oct 20 '25 19:10

kurt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!