所以我尝试在 Android 应用程序中提供服务,这是我在 Android 上的第一个应用程序(也在 Java 上)),所以我遇到了很多问题。但主要问题是“架构”我的后台服务。
我需要通过蓝牙设置与设备(我的自定义设备)的连接。我有自己的协议(protocol)(仍在进行中),因此我需要在后台服务中执行以下操作:
- 发送了一些东西
- 始终监听InputStream! (一直)
我如何在我的服务中创建这个? 我需要在我的服务中创建两个线程?
我已经尝试了很多关于如何做的“想法”?我有非常巨大的“脏”代码......每次我运行它时,它都会崩溃。
public class MyService extends Service {
public BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
public BluetoothSocket socket;
public String readMessage;
public int BTC_STATE;
ExecutorService es;
public void onCreate() {
super.onCreate();
es = Executors.newFixedThreadPool(2);
}
}
public void onDestroy() {
super.onDestroy();
}
// ЗАПУСК СЕРВИСА
public int onStartCommand(Intent intent, int flags, int startId) {
int time = intent.getIntExtra(DashboardActivity.PARAM_TIME, 1);
int task = intent.getIntExtra(DashboardActivity.PARAM_TASK, 0);
String strMAC = intent.getStringExtra(DashboardActivity.PARAM_MAC);
String strMESS = intent.getStringExtra(DashboardActivity.PARAM_MESSAGE);
MyRun mr = new MyRun(startId, time, task,strMAC,strMESS);
es.execute(mr);
return super.onStartCommand(intent, flags, startId);
}
public IBinder onBind(Intent arg0) {
return null;
}
class MyRun implements Runnable {
int time;
int startId;
int task;
int target;
String strMAC;
String strMESS;
public MyRun(int startId, int time, int task,String strMAC,String strMESS) {
this.time = time;
this.startId = startId;
this.task = task;
this.target = 1;
this.strMAC = strMAC;
this.strMESS = strMESS;
}
public void run() {
Intent intent = new Intent(DashboardActivity.BROADCAST_ACTION);
//here, depending on task id i sent reqwest. But where i need to create listning method?
}
在 run() 中我有不同的任务...但是我需要在哪里为我的 BTE 套接字 InPutStream 创建列表方法?
最佳答案
所以不,我有 3 项服务任务: 1)连接 2)开始监听我的BTE连接的inputStream 3)发送数据
try {
InputStream is = socket.getInputStream();
byte[] buffer = new byte[1];
int bytes;
//StringBuilder readMessage = new StringBuilder();
String readMess2;
while (1!=2) {
try {
bytes = is.read(buffer);
String readed = new String(buffer, 0, bytes);
//readMessage.append(readed);
readMess2=readed;
intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
intent.putExtra(DashboardActivity.PARAM_RESULT, "Listning: " + readMess2);
sendBroadcast(intent);
} catch (IOException e) {
BTC_STATE='0';
intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
intent.putExtra(DashboardActivity.PARAM_RESULT, "Ошибка уха!");
sendBroadcast(intent);
serviceNotifer("ADPTTO Service", "УХО ОШИБКА " + BTC_STATE);
break;
}
}
intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
intent.putExtra(DashboardActivity.PARAM_RESULT, "STOP УХО WHILE =)");
sendBroadcast(intent);
serviceNotifer("ADPTTO Service", "УХО WHILE OUT " + BTC_STATE);
} catch (IOException e) {
BTC_STATE='0';
intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
intent.putExtra(DashboardActivity.PARAM_RESULT, "GET ERROR");
sendBroadcast(intent);
serviceNotifer("ADPTTO Service", "УХО ОШИБКА 2 + " + BTC_STATE);
}
这段代码可以工作..但只是一段时间(1-2 分钟)。我可以一直听谁的歌?
关于java - 如何在android后台服务中监听蓝牙套接字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186760/