| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | 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 |
| | |
| | | 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"); |
| | |
| | | |
| | | new Thread(new TestThread()).start(); |
| | | |
| | | new Thread(new AutoUpdateMcuThread(this)).start(); |
| | | // new Thread(new AutoUpdateMcuThread(this)).start(); |
| | | |
| | | upgrade = Upgrade.getInstance(getApplicationContext()); |
| | | upgrade.CheckUpgrade(); |
| | |
| | | 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 { |
| | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | Thread.sleep(500); |
| | | Thread.sleep(30000); |
| | | |
| | | // Ota ota = new Ota(); |
| | | // ota.excuteUpdateZip(getApplicationContext(), ""); |
| | | } catch (InterruptedException e) { |
| | | |
| | | } |
| | |
| | | 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()); |
| | |
| | | } |
| | | } |
| | | |
| | | 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"); |
| | |
| | | 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); |
| | | } |