亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

誰能幫我把下面的 藍牙通訊代碼寫下 翻譯啊,最好每一行都寫一下 或者偽代碼寫出來。謝謝;啊

誰能幫我把下面的 藍牙通訊代碼寫下 翻譯啊,最好每一行都寫一下 或者偽代碼寫出來。謝謝;啊

慕粉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 關注
  • 1891 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號