簡體   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

我正在編寫我的第一個Android應用程序,並且已經解開了 - 我對藍牙GATT的可讀性和寫入都有很大的問題。 30分鍾前,我能夠從手機寫一個字節到我的外圍設備,看到收到了。 我現在不可以。 我開始認為這個間歇性的錯誤導致了問題:

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)同樣的錯誤,但它只會出現一次。

這是我綁定服務的地方:

@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);
    }
}

這是服務:

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;
    }
};

以下是發生錯誤的回調處理程序:

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);
}

我很難過,我甚至不確定解決這個問題會解決我的問題

我不知道這是否會對你有所幫助,但我收到了完全相同的錯誤信息。 對於我的問題的一些上下文:我在我的自定義對象中的Activity之外調用'sendBroadcast'。 我能夠通過替換這個來解決:

sendBroadcast(intent);

有了這個:

context.sendBroadcast(intent);

毋庸置疑,當活動第一次實例化所述對象時,我通過將其傳遞給構造函數中的對象來保持對我的對象中的上下文的引用:

new MyObject(this);

希望它能幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM