@@ -4,6 +4,7 @@ import static com.mogo.eagle.core.data.deva.chain.ChainConstant.CHAIN_ALIAS_CODE
import static com.mogo.eagle.core.data.deva.chain.ChainConstant.CHAIN_LINK_INIT ;
import static com.mogo.eagle.core.data.deva.chain.ChainConstant.CHAIN_LINK_LOG_CONNECT_STATUS ;
import static com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.M_HMI ;
import static com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.M_MAP ;
import android.bluetooth.BluetoothAdapter ;
import android.bluetooth.BluetoothDevice ;
@@ -31,6 +32,7 @@ import com.mogo.eagle.core.function.call.autopilot.CallerAutoPilotManager;
import com.mogo.eagle.core.function.call.setting.CallerSkinModeListenerManager ;
import com.mogo.eagle.core.function.hmi.R ;
import com.mogo.eagle.core.function.main.moujie.BluetoothMonitorReceiver ;
import com.mogo.eagle.core.function.main.moujie.ConnectBluetoothEvent ;
import com.mogo.eagle.core.utilcode.mogo.AppIdentityModeUtils ;
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger ;
import com.mogo.eagle.core.utilcode.mogo.storage.SharedPrefsMgr ;
@@ -45,9 +47,11 @@ import com.zhjt.service.chain.TracingConstants;
import org.greenrobot.eventbus.EventBus ;
import org.greenrobot.eventbus.Subscribe ;
import org.greenrobot.eventbus.ThreadMode ;
import java.lang.reflect.InvocationTargetException ;
import java.lang.reflect.Method ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Set ;
import java.util.Timer ;
@@ -69,6 +73,8 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
private Timer timerAcc ;
private BluetoothMonitorReceiver mBluetoothReceiver = null ;
private BluetoothAdapter mBluetoothAdapter ;
private List < BluetoothDevice > mAreadlyConnectedList = new ArrayList < > ( ) ; //已连接设备集合
@Override
@@ -78,12 +84,22 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
EventBus . getDefault ( ) . register ( this ) ;
CallerLogger . INSTANCE . i ( M_HMI + TAG , " onCreate " ) ;
SharedPrefsMgr . getInstance ( getContext ( ) ) . putBoolean ( " BLUETOOTH " , false ) ;
// initBluetooth();
// mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
@Override
protected void onResume ( ) {
super . onResume ( ) ;
initBluetooth ( ) ;
mBluetoothAdapter = BluetoothAdapter . getDefaultAdapter ( ) ;
if ( ! mBluetoothAdapter . isEnabled ( ) ) {
mBluetoothAdapter . enable ( ) ;
}
mBluetoothAdapter . startDiscovery ( ) ;
showBondedDevice ( mBluetoothAdapter ) ;
}
@Override
protected void initViews ( ) {
@@ -190,28 +206,24 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
@Override
public boolean onKeyDown ( int keyCode , KeyEvent event ) {
if ( keyCode = = KeyEvent . KEYCODE_VOLUME_UP ) { //向上长按 开启自动驾驶
setMapControl ( ) ;
long currentTime = System . currentTimeMillis ( ) / ( 1000 ) ;
long oldTime = SharedPrefsMgr . getInstance ( getContext ( ) ) . getLong ( " old_time_up " , 0 ) ;
// Log.d("liyz", "time = " + (currentTime - oldTime));
if ( currentTime - oldTime > 8 ) {
if ( currentTime - oldTime > 6 ) {
SharedPrefsMgr . getInstance ( getContext ( ) ) . putLong ( " old_time_up " , System . currentTimeMillis ( ) / 1000 ) ;
// ToastUtils.showShort("长按 ↑↑↑ 开启自动驾驶");
CallerAutoPilotManager . INSTANCE . setControlAutopilotCarAuto ( true ) ;
}
recoverMapControl ( ) ;
return true ;
} else if ( keyCode = = KeyEvent . KEYCODE_VOLUME_DOWN ) { //下和中间键盘长按,减速为-2
setMapControl ( ) ;
long currentTime = System . currentTimeMillis ( ) / ( 1000 ) ;
long oldTime = SharedPrefsMgr . getInstance ( getContext ( ) ) . getLong ( " old_time_down " , 0 ) ;
if ( currentTime - oldTime > 8 ) {
if ( currentTime - oldTime > 6 ) {
SharedPrefsMgr . getInstance ( getContext ( ) ) . putLong ( " old_time_down " , System . currentTimeMillis ( ) / 1000 ) ;
// ToastUtils.showShort("长按 ↓↓↓ 减速 -2 ");
sendAcc ( true , - 2 ) ;
}
recoverMapControl ( ) ;
return true ;
}
return super . onKeyDown ( keyCode , event ) ;
@@ -221,16 +233,12 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
public boolean dispatchTouchEvent ( MotionEvent event ) {
// Log.d("liyz", "dispatchTouchEvent event.getX() = " + event.getX() + "--event.getY() = " + event.getY() + "--action = " + event.getAction());
if ( event . getX ( ) = = - 1469 . 6875 & & event . getY ( ) = = 530 . 0 ) { //向左变道
setMapControl ( ) ;
// ToastUtils.showShort("← 向左变道");
// MogoMapUIController.getInstance().setLockMode( true );
CallerAutoPilotManager . INSTANCE . sendOperatorChangeLaneLeft ( ) ;
recoverMapControl ( ) ;
return true ;
} else if ( event . getX ( ) = = - 1096 . 25 & & event . getY ( ) = = 132 . 5 ) { //鸣笛
setMapControl ( ) ;
// ToastUtils.showShort("↑ 鸣笛 ");
CallerAutoPilotManager . INSTANCE . sendOperatorSetHorn ( 1 ) ;
@@ -245,29 +253,20 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
}
} , 500 ) ;
recoverMapControl ( ) ;
return true ;
} else if ( event . getX ( ) = = - 863 . 4375 & & event . getY ( ) = = 690 . 0 ) { //向右变道
setMapControl ( ) ;
CallerAutoPilotManager . INSTANCE . sendOperatorChangeLaneRight ( ) ;
// ToastUtils.showShort("→ 向右变道");
recoverMapControl ( ) ;
return true ;
} else if ( event . getX ( ) = = - 1096 . 25 & & event . getY ( ) = = 1099 . 0 ) { // -1减速
setMapControl ( ) ;
sendAcc ( true , - 1 ) ;
// ToastUtils.showShort("↓ 减速 -1 ");
recoverMapControl ( ) ;
return true ;
} else if ( event . getX ( ) = = - 1096 . 25 & & event . getY ( ) = = 690 . 0 ) { //复原
setMapControl ( ) ;
// ToastUtils.showShort("O 复原 ");
sendAcc ( false , 0 . 0 ) ;
recoverMapControl ( ) ;
return true ;
}
@@ -340,20 +339,6 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
}
}
private void setMapControl ( ) {
// UiThreadHandler.removeCallbacks(delayRunnable);
// MogoMapUIController.getInstance().setScrollGesturesEnable(false);
}
private void recoverMapControl ( ) {
// UiThreadHandler.postDelayed(delayRunnable, 5_00L);
}
private Runnable delayRunnable = ( ) - > {
MogoMapUIController . getInstance ( ) . setScrollGesturesEnable ( true ) ;
} ;
/**
* 初始化蓝牙广播
*/
@@ -371,19 +356,31 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
registerReceiver ( this . mBluetoothReceiver , intentFilter ) ;
}
/**
* 查找蓝牙连接过的蓝牙设备
*
* @param bluetoothAdapter
*/
private void showBondedDevice ( BluetoothAdapter bluetoothAdapter ) {
mAreadlyConnectedList . clear ( ) ;
Set < BluetoothDevice > deviceList = bluetoothAdapter . getBondedDevices ( ) ;
for ( BluetoothDevice device : deviceList ) {
Log . d ( " liyz " , " Name: " + device . getName ( ) + " Mac: " + device . getAddress ( ) ) ;
try {
//使用反射调用获取设备连接状态方法
Method isConnectedMethod = BluetoothDevice . class . getDeclaredMethod ( " isConnected " , ( Class [ ] ) null ) ;
isConnectedMethod . setAccessible ( true ) ;
boolean isConnected = ( boolean ) isConnectedMethod . invoke ( device , ( Object [ ] ) null ) ;
Log. d ( " liyz " , " isConnected: " + isConnected) ;
isConnected = ( boolean ) isConnectedMethod . invoke ( device , ( Object [ ] ) null ) ;
Log . e ( " liyz " , " showBondedDevice Name: " + device . getName ( ) + " Mac: " + device . getAddress ( ) + " ---isConnected = " + isConnected ) ;
// Log.e("liyz", "showBondedDevice Name:" + device.getName() + " Mac:" + device.getAddress() + "---isConnected = " + isConnected) ;
mAreadlyConnectedList . add ( device ) ;
if ( device . getName ( ) . equals ( " JX-05 " ) ) {
SharedPrefsMgr . getInstance ( getContext ( ) ) . putString ( " BT_MAC " , device . getAddress ( ) ) ;
}
if ( ! isConnected ) {
UiThreadHandler . postDelayed ( ( ) - > {
connectBluetooth ( ) ; //主动连接一次
} , 4000L ) ;
}
} catch ( NoSuchMethodException e ) {
e . printStackTrace ( ) ;
} catch ( IllegalAccessException e ) {
@@ -394,6 +391,41 @@ public class MainLauncherActivity extends MainActivity implements IMogoIntentLis
}
}
@Subscribe ( threadMode = ThreadMode . MAIN )
public void onAutoConnection ( ConnectBluetoothEvent event ) {
connectBluetooth ( ) ;
}
/**
* 主动连接最近的一次连接
*/
private void connectBluetooth ( ) {
// Log.d("liyz", "connectBluetooth ----> size() = " + mAreadlyConnectedList.size());
if ( mAreadlyConnectedList . size ( ) > 0 ) {
try {
Method method = BluetoothDevice . class . getMethod ( " createBond " ) ;
String macAddress = SharedPrefsMgr . getInstance ( getContext ( ) ) . getString ( " BT_MAC " ) ;
int pos = - 1 ;
for ( int i = 0 ; i < mAreadlyConnectedList . size ( ) ; i + + ) {
if ( macAddress . equals ( mAreadlyConnectedList . get ( i ) . getAddress ( ) ) ) {
pos = i ;
}
}
// Log.d("liyz", "connectBluetooth macAddress = " + macAddress + "---pos = " + pos);
method . invoke ( mAreadlyConnectedList . get ( pos ) ) ;
} catch ( NoSuchMethodException e ) {
e . printStackTrace ( ) ;
} catch ( IllegalAccessException e ) {
e . printStackTrace ( ) ;
} catch ( InvocationTargetException e ) {
e . printStackTrace ( ) ;
}
}
}
}