How to remove incompatible types error from android studio

Osama Billah :

Hi everyone I write the code for handler to handle a function and call it after every 5 second. I used the code below but it show me the following error. the main idea behind this is first run this after 5 second and gave the value to Bluetooth_service and the Bluetooth service will display this in notification bar. the notification bar work fine for only value. now through this I want to change the value after 5 second.

error: incompatible types: <anonymous BleReadCallback> cannot be converted to Runnable
                        mHandler.postDelayed(this, 5000);

code

        Handler mHandler = new Handler();

        Runnable mToastRunnable = new Runnable() {
        @Override
        public void run() {
            if(manager.getConnectedDevices().size()<=0){
                Toast.makeText(MainActivity.this,"No connected devices", Toast.LENGTH_LONG).show();
                return;
            }
            BleDevice device = manager.getConnectedDevices().get(0);
            Map<String , String> reciveData =  getSpecificServiceInfo(device , CHARACTERISTIC_READABLE);
            for (Map.Entry<String, String> e : reciveData.entrySet()){
                manager.read(device, e.getKey(), e.getValue(), new BleReadCallback() {
                    @Override
                    public void onRead(byte[] data, BleDevice device) {

                        Toast.makeText(MainActivity.this, "Read success!   data:  " + new String(data), Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(MainActivity.this , BluetoothService.class);
                        intent.putExtra("inputString" ,new String(data));
                        startService(intent);
                        mHandler.postDelayed(this, 5000);
//                        mHandler.postDelayed(MainActivity.this.getApplication() , 5000);
                        TextView textView = findViewById(R.id.textView);
                        textView.setText(new String(data));

                    }

                    @Override
                    public void onFail(int failCode, String info, BleDevice device) {
//                    Toast.makeText(MainActivity.this, "Read fail!   data:  " + info, Toast.LENGTH_LONG).show();

                    }
                });
            }

        }
    };
    private void readData(){
        mToastRunnable.run();
    }

Bluetooth_service:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
            String input = intent.getStringExtra("inputString");
            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                    notificationIntent, 0);
            Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setContentTitle("Bluetooth Services")
                    .setContentText(input)
                    .setSmallIcon(R.drawable.ic_android)
                    .setContentIntent(pendingIntent)
                    .build();
            startForeground(1, notification);
            return START_NOT_STICKY;
}
Alex Sunder Singh :

Add a new method in a class. Ex: newMethod And replace this with mToastRunnable Call the created method from override method onRead

void newMethod(byte[] data){
    Toast.makeText(MainActivity.this, "Read success!   data:  " + new String(data), Toast.LENGTH_LONG).show();
    Intent intent = new Intent(MainActivity.this , BluetoothService.class);
    intent.putExtra("inputString" ,new String(data));
    startService(intent);
    mHandler.postDelayed(mToastRunnable, 5000);//use mToastRunnable instead of this
//                        mHandler.postDelayed(MainActivity.this.getApplication() , 5000);
    TextView textView = findViewById(R.id.textView);
    textView.setText(new String(data));
}

main code

final Handler mHandler = new Handler();
final Runnable mToastRunnable = new Runnable() {
    @Override
    public void run() {
        if(manager.getConnectedDevices().size()<=0){
            Toast.makeText(MainActivity.this,"No connected devices", Toast.LENGTH_LONG).show();
            return;
        }
        BleDevice device = manager.getConnectedDevices().get(0);
        Map<String , String> reciveData =  getSpecificServiceInfo(device , CHARACTERISTIC_READABLE);
        for (Map.Entry<String, String> e : reciveData.entrySet()){
            manager.read(device, e.getKey(), e.getValue(), new BleReadCallback() {
                @Override
                public void onRead(byte[] data, BleDevice device) {
                    newMethod(data);//call the created method here
                }

                @Override
                public void onFail(int failCode, String info, BleDevice device) {
//                    Toast.makeText(MainActivity.this, "Read fail!   data:  " + info, Toast.LENGTH_LONG).show();

                }
            });
        }

    }
};

private void readData(){
    mToastRunnable.run();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24426&siteId=1