jeudi 26 février 2015

Android: sendBroadcast Unhandled exception in callback java.lang.NullPointerException



I am trying to send a broadcast from a service to my main activity. For some reason, when I call sendBroadcast I get the following warning.



02-26 14:55:40.615 11079-11090/com.example.sdp11.wmd W/BluetoothGatt﹕ Unhandled exception in callback
java.lang.NullPointerException
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:372)
at com.example.sdp11.wmd.BluetoothLEService.broadcastUpdate(BluetoothLEService.java:148)
at com.example.sdp11.wmd.BluetoothLEService.access$100(BluetoothLEService.java:28)
at com.example.sdp11.wmd.BluetoothLEService$1.onServicesDiscovered(BluetoothLEService.java:94)
at android.bluetooth.BluetoothGatt$1.onSearchComplete(BluetoothGatt.java:301)
at android.bluetooth.IBluetoothGattCallback$Stub.onTransact(IBluetoothGattCallback.java:215)
at android.os.Binder.execTransact(Binder.java:412)
at dalvik.system.NativeStart.run(Native Method)


The method I am using to call sendBroadcast function is as follows.



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


The above method is called in my Bluetooth Callback:



private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
//Connection established
if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_CONNECTED) {

Log.e(TAG, "Connected Successfully");
//Discover services
gatt.discoverServices();

} else if (status == BluetoothGatt.GATT_SUCCESS
&& newState == BluetoothProfile.STATE_DISCONNECTED) {
Log.e(TAG, "Disconnected");
//Handle a disconnect event
}

else {
Log.e(TAG, "Connection state changed. New state: " + newState);
}
}

@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.e(TAG, "Services discovered");
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.e(TAG, "Error, onServicesDiscovered received status: " + status);
}
}
}


Any help would be greatly appreciated.


EDIT


This is in my onCreate method for the main activity:



public static BluetoothLEService mBluetoothLEService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buildGoogleApiClient();
createLocationRequest();

mBluetoothLEService = new BluetoothLEService();
Intent gattServiceIntent = new Intent(this, BluetoothLEService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}

private final ServiceConnection mServiceConnection = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLEService = ((BluetoothLEService.LocalBinder) service).getService();
if (!mBluetoothLEService.initialize()) {
Log.e(TAG, "Unable to initialize Bluetooth");
finish();
}
// Automatically connects to the device upon successful start-up initialization.
//mBluetoothLEService.connect(mDeviceAddress);
Log.e(TAG, "Service Connected");
}

@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLEService = null;
}
};



Aucun commentaire:

Enregistrer un commentaire