简体   繁体   English

蓝牙GATT尝试在空对象引用上调用虚方法'void android.content.Context.sendBroadcast(android.content.Intent)'

[英]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. 我正在编写我的第一个Android应用程序,并且已经解开了 - 我对蓝牙GATT的可读性和写入都有很大的问题。 30 minutes ago I was able to write a byte from my phone to my peripheral, and see it received. 30分钟前,我能够从手机写一个字节到我的外围设备,看到收到了。 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. 我从android开发人员示例中获得了大部分代码,但是根据我的需要对其进行了修改。 If I remove the call to broadcastUpdate here, or wrap it in a try block the error will occur in a different location (eg onServicesDiscovered) the same error, but it only ever appears EXACTLY once. 如果我在这里删除对broadcastUpdate的调用,或者将它包装在try块中,则错误将发生在不同的位置(例如onServicesDiscovered)同样的错误,但它只会出现一次。

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: 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 我很难过,我甚至不确定解决这个问题会解决我的问题

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. 对于我的问题的一些上下文:我在我的自定义对象中的Activity之外调用'sendBroadcast'。 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. 希望它能帮到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在空对象引用上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()' - Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference 尝试在空对象引用上调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()” - Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference 尝试在 Android Studio 中的 null object 参考上调用虚拟方法 'android.content.Context.getApplicationInfo()' - Attempt to invoke virtual method 'android.content.Context.getApplicationInfo()' on a null object reference in Android Studio 尝试在 null object 参考上调用虚拟方法“android.content.Context.getResources()” - Attempt to invoke virtual method 'android.content.Context.getResources()' on a null object reference 问题:尝试在 null object 引用上调用虚拟方法“android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()” - Problem: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference 例外:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResources()” - Exception: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 尝试在空对象引用上调用虚拟方法'android.content.res.Resources $ Theme android.content.Context.getTheme()' - Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference Android Studio蓝牙:-尝试在空对象引用上调用虚拟方法 - Android Studio Bluetooth :- attempt to invoke a virtual method on a null object reference 尝试在空对象引用上调用虚拟方法“java.lang.Object android.content.Context.getSystemService(java.lang.String)” - Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference 尝试在空对象引用上调用虚拟方法 'android.content.Context.getSharedPreferences(java.lang.String, int)' - Attempt to invoke virtual method 'android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM