Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: why i get a NullPointerException when i want to make a ble Adapter?

Lennart:

I want to make a Ble Client, who sends Characteristics to a Server. But I get a NullPointerException. Can anyone help or know a good example?

This is the ErrorCode:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference at android.content.ContextWrapper.getSystemService(ContextWrapper.java:659) at com.nilhcem.blefun.mobile.interact.GattClient.initialize(GattClient.java:164) at com.nilhcem.blefun.mobile.interact.InteractActivity.onCreate(InteractActivity.java:23)


public boolean initialize() {
// For API level 18 and above, get a reference to BluetoothAdapter through
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager;
bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

return true;
}

public void onClickgoforward(View v) {
if (mGattClient != null) {
mGattClient.moveForward();
}
}


public void moveForward() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w("BLE", "BluetoothAdapter ist nicht installiert");
return;
}
BluetoothGattService mService = mBluetoothGatt.getService(SERVICE_UUID);
if (mService == null) {
Log.w("BLE", "Service wurde nicht gefunden.");
return;
}

BluetoothGattCharacteristic forwardcharacteristic = mService.getCharacteristic(CHARACTERISTIC_FORWARD_UUID);
if (!mBluetoothGatt.readCharacteristic(forwardcharacteristic)) {
Log.w("BLE", "Failed to read");
}
}


@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.interact_activity);

mGattClient.initialize();

mButton = (Button) findViewById(R.id.interact_button);



mButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mGattClient.moveforward();

}

});

This is my oncreate Method in the GattClient Activity


@Override
public void onCreate() {
super.onCreate();
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();




}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: why i get a NullPointerException when i want to make a ble Adapter?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×