0

Ok So i have figured out my problem, i now have a new problem in that the value of the seek bar will not change in my Service class as i am swiping and changing its value.

Main Activity

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView seekBarValue = findViewById(R.id.seekBar2);
    seekbar= findViewById(R.id.seekBar1);
    seekbar.setMax(20);

// I added intents

      Intent serviceIntent = new Intent(this,BleServer.class);
      serviceIntent.putExtra("seekbar",seekbar.getProgress());
      this.startService(serviceIntent);


    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public int progressChangedValue = 0;
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChangedValue = progress;
            seekBarValue.setText(String.valueOf(progress));
            Log.i(TAG, "SeekBar Value: " + progress);

        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            Toast.makeText(MainActivity.this, "Seek bar progress is :" + progressChangedValue,
                    Toast.LENGTH_SHORT).show();
        }

    });

Service Activity

// I added onStartCommand

public int onStartCommand(Intent intent, int flags, int startId) {

    Integer seekvalue = intent.getIntExtra("seekbar", 0);
    Log.i(TAG, "What is the Brightness Value: " + seekvalue );
    //Toast.makeText(this,userID, Toast.LENGTH_LONG).show();
    return START_STICKY;

}

    MainActivity mainActivity = new MainActivity();
    public void startServer() {
    goForeground1();
    Log.d(TAG, "Service: Starting Server");
    mGattServer = mBluetoothManager.openGattServer(this, mGattServerCallback);
    if (mGattServer == null) {
        Log.w(TAG, "Unable to create GATT server");
        return;
    }

    BluetoothGattService service = new BluetoothGattService(SERVICE_UUID, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    BluetoothGattCharacteristic writeReadCharacteristic = new BluetoothGattCharacteristic(
            desiredDimness, BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE,BluetoothGattCharacteristic.PERMISSION_READ|BluetoothGattCharacteristic.PERMISSION_WRITE);
    writeReadCharacteristic.setValue(write);
    service.addCharacteristic(writeReadCharacteristic);
    mGattServer.addService(service);
}

So now i have a value from onStartCommand, but i am unable to set that value to a characteristic that is outside onStartCommand method, and the value does not change as i change seekbar.

Mikeyj
  • 1
  • 2
  • `MainActivity mainActivity = new MainActivity();` This is not how you start an Activity. Of course any UI widgets are not going to be available when you do this - so that is why your `mainActivity.seekbar` is null inside your service. – CzarMatt Mar 16 '18 at 21:09
  • My first class activity is called MainActivity and i thought i had to create that before i could call anything from the MainActivity to be used in another class? – Mikeyj Mar 16 '18 at 21:35
  • Sure, but you must actually _start_ the activity. Instantiating it with the "new" keyword will not call `onCreate` and other lifecycle methods. See [building an intent](https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent). – CzarMatt Mar 16 '18 at 21:40
  • Okay i will, thanks for the advice. – Mikeyj Mar 16 '18 at 21:45
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Viktor Yakunin Mar 17 '18 at 00:03

0 Answers0