From 6e0f29b08a040d14576d7053c1206a8439936570 Mon Sep 17 00:00:00 2001 From: yy1717 <fctom1215@outlook.com> Date: 星期二, 24 十一月 2020 14:18:03 +0800 Subject: [PATCH] 坐标 --- lib/src/main/java/com/anyun/exam/lib/RemoteService.java | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 162 insertions(+), 3 deletions(-) diff --git a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java index 76ec97f..f368d69 100644 --- a/lib/src/main/java/com/anyun/exam/lib/RemoteService.java +++ b/lib/src/main/java/com/anyun/exam/lib/RemoteService.java @@ -1,22 +1,31 @@ package com.anyun.exam.lib; +import android.Manifest; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.database.Cursor; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; +import android.os.Build; +import android.os.Handler; import android.os.IBinder; +import android.os.Message; import android.os.Process; import android.os.RemoteCallbackList; import android.os.RemoteException; import android.util.Base64; import android.util.Log; +import com.anyun.exam.lib.util.Bluetooth; +import com.anyun.exam.lib.util.BluetoothChatService; +import com.anyun.exam.lib.util.BluetoothChatServiceCallback; +import com.anyun.exam.lib.util.Constants; import com.anyun.exam.lib.util.DESUtil; import com.anyun.exam.lib.util.NetUtils; import com.anyun.exam.lib.util.ProperUtil; @@ -24,6 +33,8 @@ import com.anyun.exam.lib.util.SpeakerCallback; import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; import java.io.IOException; import java.io.InputStream; @@ -32,6 +43,9 @@ import java.util.ArrayList; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Pattern; + +import static android.content.pm.PackageManager.PERMISSION_GRANTED; /** * MyApplication2 @@ -52,6 +66,13 @@ private Uri ringUri = null; private Upgrade upgrade = null; + + private Bluetooth mBluetooth = null; + private BluetoothChatService mChatService = null; + private String mConnectedDeviceName = null; + private String mTargetBluetooth = null; + private String mTargetBluetoothAddr = null; + private boolean mDiscoveryBluetooth = false; private IRemoteInterface.Stub iRemoteInterface = new IRemoteInterface.Stub(){ @Override @@ -75,7 +96,7 @@ if (cmd == 0x8100) { try { byte [] file = value.getBytes("ISO-8859-1"); - Log.d(TAG, byte2hex(file)); +// Log.d(TAG, byte2hex(file)); MainProcBinMsgEntry(cmd, file, file.length); } catch (UnsupportedEncodingException e) { Log.d(TAG, "UnsupportedEncodingException"); @@ -102,7 +123,7 @@ new Thread(new TestThread()).start(); - new Thread(new AutoUpdateMcuThread(this)).start(); +// new Thread(new AutoUpdateMcuThread(this)).start(); upgrade = Upgrade.getInstance(getApplicationContext()); upgrade.CheckUpgrade(); @@ -114,6 +135,11 @@ Log.d(TAG, "鍩哄甫鐗堟湰 " + getBaseband_Ver()); String ver = getBaseband_Ver(); + + mBluetooth = Bluetooth.getInstance(getApplicationContext(), mHandler); + mBluetooth.OpenBluetooth(); + // Initialize the BluetoothChatService to perform mBluetooth connections + mChatService = new BluetoothChatService(this, mHandler); } class AutoUpdateMcuThread implements Runnable { @@ -232,7 +258,10 @@ @Override public void run() { try { - Thread.sleep(500); + Thread.sleep(30000); + +// Ota ota = new Ota(); +// ota.excuteUpdateZip(getApplicationContext(), ""); } catch (InterruptedException e) { } @@ -250,6 +279,15 @@ public void onDestroy() { super.onDestroy(); Log.i(TAG,"onDestroy"); + + if (mChatService != null) { + mChatService.stop(); + } + if (mBluetooth != null) { + mBluetooth.CloseBluetooth(); + mBluetooth.unRegisterReceiver(); + } + mIsServiceDestroyed.set(true); Log.d(TAG, "onDestory executed 杩涚▼ " + Process.myPid()); @@ -362,6 +400,125 @@ } } + public void ConnectBluetooth(String name, String pin) { + if (mBluetooth != null && mChatService != null) { + mBluetooth.SetPin(pin); + mTargetBluetooth = name; + if (isMac(name)) { + mTargetBluetoothAddr = name; + handlerConnectBluetooth.removeCallbacks(runnableConnectBluetooth); + handlerConnectBluetooth.postDelayed(runnableConnectBluetooth, 500); + } else { + mDiscoveryBluetooth = true; + mBluetooth.doDiscovery(name); + } + } + } + + Handler handlerConnectBluetooth = new Handler(); + Runnable runnableConnectBluetooth = new Runnable() { + @Override + public void run() { + if (mChatService != null && mTargetBluetoothAddr != null) { + mChatService.connect(mTargetBluetoothAddr, true); + } + } + }; + + public boolean isMac(String addr) { + String pattern = "^([0-9A-Fa-f]{2})(:[0-9A-Fa-f]{2}){5}$"; + return Pattern.matches(pattern, addr); + } + + public void WriteBluetooth(byte []data) { + if (mChatService != null && mChatService.getState() == BluetoothChatService.STATE_CONNECTED) { + mChatService.write(data); + } + } + + private final Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + switch (msg.what) { + case Constants.MESSAGE_BLUETOOTH_STATUS: + if (msg.arg1 == 1) { + Log.d(TAG, "钃濈墮寮�鍚�"); +// mBluetooth.doDiscovery(); + if (mChatService != null) { +// mChatService.connect("00:1D:43:9A:E0:79", true); +// mChatService.connect("00:1A:7D:DA:71:11", true); + } + BluetoothStatusChange(1); + } else { + Log.d(TAG, "钃濈墮鍏抽棴"); + BluetoothStatusChange(0); + } + break; + case Constants.MESSAGE_BLUETOOTH_FOUND: + String btaddr = msg.getData().getString(Constants.DEVICE_ADDRESS); + Log.d(TAG, "鍙戠幇璁惧 " + btaddr); + if (mDiscoveryBluetooth) { + mTargetBluetoothAddr = btaddr; + } + break; + case Constants.MESSAGE_BLUETOOTH_DISCOVERY_FINISHED: + Log.d(TAG, "鎼滅储缁撴潫"); + if (mDiscoveryBluetooth) { + handlerConnectBluetooth.removeCallbacks(runnableConnectBluetooth); + handlerConnectBluetooth.postDelayed(runnableConnectBluetooth, 500); + } + mDiscoveryBluetooth = false; + break; + case Constants.MESSAGE_STATE_CHANGE: + switch (msg.arg1) { + case BluetoothChatService.STATE_CONNECTED: + Log.d(TAG, "钃濈墮宸茶繛鎺�"); + BluetoothStatusChange(3); + break; + case BluetoothChatService.STATE_CONNECTING: + Log.d(TAG, "钃濈墮杩炴帴涓�..."); + break; + case BluetoothChatService.STATE_LISTEN: + Log.d(TAG, "钃濈墮鐩戝惉"); + BluetoothStatusChange(2); + break; + case BluetoothChatService.STATE_NONE: + Log.d(TAG, "钃濈墮鏃犳晥"); + BluetoothStatusChange(2); + handlerConnectBluetooth.removeCallbacks(runnableConnectBluetooth); + handlerConnectBluetooth.postDelayed(runnableConnectBluetooth, 5000); + break; + } + break; + case Constants.MESSAGE_WRITE: + break; + case Constants.MESSAGE_READ: +// Log.d(TAG, String.format("钃濈墮鏁版嵁 length = %d 绾跨▼ %d", msg.arg1, Thread.currentThread().getId())); + BluetoothDataComeIn((byte[]) msg.obj, msg.arg1); + /*if (ndkUtils != null) { + byte[] readBuf = (byte[]) msg.obj; + ndkUtils.bluetoothMoreData(readBuf, msg.arg1); + }*/ + break; + case Constants.MESSAGE_DEVICE_NAME: + // save the connected device's name + mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME); + Log.d(TAG, "MESSAGE_DEVICE_NAME: " + mConnectedDeviceName); + break; + case Constants.MESSAGE_TOAST: + break; + default:break; + } + } + }; + + class DataCallback implements BluetoothChatServiceCallback { + @Override + public void iDataComeIn(byte []data, int length) { + + } + } + // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); @@ -372,4 +529,6 @@ public native void MainProcBinMsgEntry(int cmd, byte []data, int length); public native void UpgradeMcu(String vercode, byte []rom); public native void TextSpeakEnd(int id); + public native void BluetoothStatusChange(int status); + public native void BluetoothDataComeIn(byte []data, int length); } -- Gitblit v1.8.0