为了账号安全,请及时绑定邮箱和手机立即绑定

谁能帮我把下面的 蓝牙通讯代码写下 翻译啊,最好每一行都写一下 或者伪代码写出来。谢谢;啊

谁能帮我把下面的 蓝牙通讯代码写下 翻译啊,最好每一行都写一下 或者伪代码写出来。谢谢;啊

慕粉3468678 2016-06-09 17:18:38
package com.dashan.bluth.service;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.UUID;import android.app.Service;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothSocket;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.IBinder;import android.util.Log;import com.dashan.bluth.base.BaseApplication;import com.dashan.bluth.bean.MessageInfo;import com.google.gson.Gson;public class BluthSocketService extends Service { public BluetoothSocket socket = null; // 蓝牙通讯socket public final static String uuid = "00001101-0000-1000-8000-00805F9B34FB"; public final static String serviceName = "teacher"; public BluetoothDevice teacherBlueth;// 教师端蓝牙 public MessageInfo minfo = null;// 通信消息 public Gson g = new Gson(); public boolean isrun = true; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); ScreenBroadcastReceiver mScreenReceiver = new ScreenBroadcastReceiver();// 注册屏幕广播监听 IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_SCREEN_ON); filter.addAction(Intent.ACTION_SCREEN_OFF); filter.addAction(Intent.ACTION_USER_PRESENT); this.registerReceiver(mScreenReceiver, filter); BaseApplication ba = (BaseApplication) this.getApplication(); minfo = ba.getCurrentMinfo(); Log.d("msg", "service已经启动了"); } public void connectToTeacher(final String msg) {// 建立通信连接 BaseApplication ba = (BaseApplication) this.getApplication(); teacherBlueth = ba.getCurrentService(); new Thread() { public void run() { boolean connected = true; try { socket = teacherBlueth .createRfcommSocketToServiceRecord(UUID .fromString(uuid)); socket.connect(); } catch (IOException e) { // TODO Auto-generated catch block、 connected = false; Log.d("msg", "老师端关闭了签到服务"); stopSelf();// 关闭后台监听服务 } if (connected) { sendMsgToTeacher(msg); } }; }.start(); } @Override @Deprecated public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); } public void sendMsgToTeacher(final String msg) {// 发送消息给服务器 new Thread() { public void run() { try { Log.d("msg", msg); OutputStream outStream = socket.getOutputStream(); outStream.write(msg.getBytes()); } catch (IOException e) { Log.e("msg", "连接教师端通信失败" + e.toString()); } }; }.start(); } private class ScreenBroadcastReceiver extends BroadcastReceiver { private String action = null; @Override public void onReceive(Context context, Intent intent) { action = intent.getAction(); if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏,学生开屏后发给老师 } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏 } else if (Intent.ACTION_USER_PRESENT.equals(action)) { // 解锁 minfo.msgType = "2"; minfo.qr.setPhoneactive(Integer.parseInt(minfo.qr.phoneactive) + 1 + ""); Log.d("msg", "用户解开屏幕了"); connectToTeacher(g.toJson(minfo)); } } }}
查看完整描述

目前暂无任何回答

  • 0 回答
  • 1 关注
  • 1848 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信