オサマBillah:
皆さん、こんにちは、私は、関数を処理し、すべての5秒後にそれを呼び出すためのハンドラのコードを記述します。私は以下のコードを使用しますが、それは私に次のエラーが表示されます。この背後にある主要なアイデアは、最初の5秒後にこれを実行すると、値を与えているBluetooth_serviceとBluetoothサービスは、通知バーでこれを表示します。値のみのための通知バーの作業罰金。今、このを通じて私は5秒後に値を変更したいです。
error: incompatible types: <anonymous BleReadCallback> cannot be converted to Runnable
mHandler.postDelayed(this, 5000);
コード
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;
}
アレックスサンダー・シン:
クラスに新しいメソッドを追加します。例:newMethod
そして置き換えるthis
とmToastRunnable
オーバーライドメソッドから呼び出しを作成する方法を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));
}
手コード
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();
}