2015-02-09 132 views
3

我正在編寫我的第一個android應用程序,並且已經脫鉤 - 我越來越大問題與藍牙GATT的可靠性讀取和寫入。 30分鐘前,我能夠從手機寫入一個字節到我的外設,並看到它收到。我現在不可以。我王建宇,山西高等學校認爲這陣性錯誤是造成問題的一個:藍牙GATT嘗試調用虛擬方法'void android.content.Context.sendBroadcast(android.content.Intent)'null對象引用

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

我很爲難,我不太確定在解決這個問題將解決我的問題

+0

'broadcastUpdate'方法代碼在哪裏? – 2015-02-09 04:31:13

+0

如果您只需要應用程序內部廣播,請勿使用sendBroadcast(intent)。爲了更好的效率和安全性,最好使用'LocalBroadcastManager.getInstance(this).sendBroadcast(intent)' – 2015-08-18 07:46:17

回答

4

我不知道這是否會幫助你,但我有完全相同的錯誤信息。對於我的問題的某些背景:我在我的自定義對象的Activity外調用了「sendBroadcast」。我能夠通過更換該解決:

sendBroadcast(intent); 

與此:

context.sendBroadcast(intent); 

不用說,我是保持在我對象的上下文的引用,將它傳遞給該對象在其構造,當活動首次實例化所述對象時:

new MyObject(this); 

希望它可以幫助你。

+1

我最終修復了它,但我不知道如何 - 它只是消失了,我會回過頭來看看我的問題並比較源代碼,看看我能否縮小它的範圍。感謝您的回答 – 2015-04-17 14:53:53

+0

您有沒有想過這個?我有一個類似的問題,我很想知道你發現了什麼 – Bostwickenator 2015-12-17 22:16:28

+0

將sendBroadcast(intent)更改爲context.sendBroadcast(intent),確實解決了問題 – Tiber 2016-04-28 15:09:02

相關問題