首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bluetooth GATT尝试在空对象引用上调用虚拟方法的void android.content.Context.sendBroadcast(android.content.Intent)‘

Bluetooth GATT尝试在空对象引用上调用虚拟方法的void android.content.Context.sendBroadcast(android.content.Intent)‘
EN

Stack Overflow用户
提问于 2015-02-09 12:14:54
回答 1查看 5.5K关注 0票数 3

我正在编写我的第一个android应用程序,但却遇到了问题--蓝牙GATT的读写可靠性有很大问题。30分钟前,我能够从我的手机向我的外设写入一个字节,并看到它被接收到。现在我不能了。我开始认为这个间歇性错误是导致问题的原因:

代码语言:javascript
运行
复制
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)

我从android开发人员示例中获得了大部分代码,但根据需要对其进行了修改。如果我在这里删除对broadcastUpdate的调用,或者将它包装在一个try块中,错误将在不同的位置(例如onServicesDiscovered)发生相同的错误,但它只出现一次。

这是我绑定服务的地方:

代码语言:javascript
运行
复制
@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);
    }
}

这就是服务:

代码语言:javascript
运行
复制
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;
    }
};

以下是发生错误的回调处理程序:

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
private void broadcastUpdate(final String action) {
    final Intent intent = new Intent(action);
    sendBroadcast(intent);
}

我被难住了,我甚至不确定解决这个问题是否能解决我的问题

EN

回答 1

Stack Overflow用户

发布于 2015-04-17 07:32:14

我不知道这是否会对您有帮助,但我收到了完全相同的错误消息。关于我的问题的一些上下文:我在我的自定义对象中的一个活动之外调用了'sendBroadcast‘。我能够通过替换这个来解决这个问题:

代码语言:javascript
运行
复制
sendBroadcast(intent);

有了这个:

代码语言:javascript
运行
复制
context.sendBroadcast(intent);

不用说,当活动第一次实例化所述对象时,通过在其构造函数中将其传递给对象,我在对象中保留了对上下文的引用:

代码语言:javascript
运行
复制
new MyObject(this);

希望能对你有所帮助。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28402728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档