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; // 藍(lán)牙通訊socket public final static String uuid = "00001101-0000-1000-8000-00805F9B34FB"; public final static String serviceName = "teacher"; public BluetoothDevice teacherBlueth;// 教師端藍(lán)牙 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();// 注冊(cè)屏幕廣播監(jiān)聽(tīng) 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已經(jīng)啟動(dòng)了"); } 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", "老師端關(guān)閉了簽到服務(wù)"); stopSelf();// 關(guān)閉后臺(tái)監(jiān)聽(tīng)服務(wù) } 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) {// 發(fā)送消息給服務(wù)器 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)) { // 開(kāi)屏,學(xué)生開(kāi)屏后發(fā)給老師 } 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", "用戶解開(kāi)屏幕了"); connectToTeacher(g.toJson(minfo)); } } }}
添加回答
舉報(bào)
0/150
提交
取消