react-native-beidou 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,8 +10,12 @@ import android.net.wifi.WifiInfo;
10
10
  import android.net.wifi.WifiManager;
11
11
  import android.nfc.Tag;
12
12
  import android.os.Build;
13
+ import android.os.Bundle;
14
+ import android.os.Handler;
15
+ import android.os.Looper;
13
16
  import android.util.Log;
14
17
  import android.location.Location;
18
+ import android.location.LocationListener;
15
19
  import android.location.LocationManager;
16
20
  import android.telephony.SubscriptionInfo;
17
21
  import android.telephony.SubscriptionManager;
@@ -27,6 +31,12 @@ import com.beidou.bluetooth.callback.BluetoothConnectionCallback;
27
31
  import com.beidou.bluetooth.callback.BluetoothDataCallback;
28
32
  import com.beidou.bluetooth.callback.BluetoothPushCallback;
29
33
  import com.beidou.bluetooth.model.BlueDeviceInfoEntity;
34
+ //import com.cmcc.bdmsg.bdmsgterminal.BDMsgAuthTerminalSdk;
35
+ //import com.cmcc.bdmsg.bdmsgterminal.model.BDMsgGetKeyAndAuthTerminalResponse;
36
+ //import com.cmcc.bdmsg.bdmsgterminal.model.BDMsgGetUserInfoTerminalResponse;
37
+ //import com.cmcc.bdmsgserver.bdmsgauth.model.BDMsgEphemerisFileDownloadResponse;
38
+ //import com.cmcc.bdmsgserver.bdmsgauth.model.BDMsgMailReplyResponse;
39
+ //import com.cmcc.bdmsgserver.bdmsgauth.model.BDMsgSMCryptResponse;
30
40
  import com.facebook.react.bridge.Callback;
31
41
  import com.facebook.react.bridge.Promise;
32
42
  import com.facebook.react.bridge.ReactApplicationContext;
@@ -44,10 +54,12 @@ import org.json.JSONException;
44
54
 
45
55
  import java.util.Arrays;
46
56
  import java.util.Base64;
57
+ import java.util.LinkedList;
47
58
  import java.util.Map;
48
59
  import java.util.Objects;
49
60
  import java.util.ArrayList;
50
61
  import java.util.List;
62
+ import java.util.concurrent.atomic.AtomicBoolean;
51
63
 
52
64
  public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
53
65
  private ReactContext reactContext;
@@ -385,7 +397,7 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
385
397
  }
386
398
 
387
399
  @Override
388
- public void onDataReceived(String deviceNo, byte[] data, int dataType) {
400
+ public void onDataReceived(String deviceNo, byte[] data, int dataType, String uuid) {
389
401
  // 处理接收到的数据
390
402
  // byte[] cipher = Arrays.copyOfRange(data, 4, data.length);
391
403
  String message = Base64.getEncoder().encodeToString(data);
@@ -464,7 +476,7 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
464
476
  }
465
477
 
466
478
  @Override
467
- public void onDataReceived(String deviceNo, byte[] data, int dataType) {
479
+ public void onDataReceived(String deviceNo, byte[] data, int dataType, String uuid) {
468
480
  // 处理接收到的数据
469
481
  Log.d("BluetoothSDK", "writeInfo收到数据,长度: " + data.length);
470
482
  String base64String = Base64.getEncoder().encodeToString(data);
@@ -614,22 +626,29 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
614
626
  return;
615
627
  }
616
628
 
617
- boolean hasReadPhoneStatePermission = ContextCompat.checkSelfPermission(reactContext,
618
- Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
629
+ // 移除手动权限检查,直接调用系统API
630
+ // boolean hasReadPhoneStatePermission = ContextCompat.checkSelfPermission(reactContext,
631
+ // Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
619
632
 
620
- if (!hasReadPhoneStatePermission) {
621
- promise.reject("PERMISSION_DENIED", "缺少READ_PHONE_STATE权限");
622
- return;
633
+ // if (!hasReadPhoneStatePermission) {
634
+ // promise.reject("PERMISSION_DENIED", "缺少READ_PHONE_STATE权限");
635
+ // return;
636
+ // }
637
+
638
+ // 优先使用 Activity Context
639
+ Context context = reactContext.getCurrentActivity();
640
+ if (context == null) {
641
+ context = reactContext;
623
642
  }
624
643
 
625
644
  boolean hasReadPhoneNumbersPermission = true;
626
645
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
627
- hasReadPhoneNumbersPermission = ContextCompat.checkSelfPermission(reactContext,
646
+ hasReadPhoneNumbersPermission = ContextCompat.checkSelfPermission(context,
628
647
  Manifest.permission.READ_PHONE_NUMBERS) == PackageManager.PERMISSION_GRANTED;
629
648
  }
630
649
 
631
650
  WritableArray result = new WritableNativeArray();
632
- Context context = reactContext;
651
+ // Context context = reactContext; // 已在上方定义
633
652
 
634
653
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
635
654
  SubscriptionManager subscriptionManager = (SubscriptionManager) context
@@ -758,46 +777,84 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
758
777
  @ReactMethod
759
778
  public void BDAuthenticate(int slotId, Promise promise) {
760
779
  beidouAidlHelper.authenticate(slotId, promise);
780
+ // new Thread(() -> {
781
+ // BDMsgGetKeyAndAuthTerminalResponse ksKeyAndAuthResult = BDMsgAuthTerminalSdk.getInstance(reactContext).getKsKeyAndAuthResult(slotId);
782
+ // promise.resolve(ksKeyAndAuthResult.toString());
783
+ // }).start();
761
784
  }
762
785
 
763
786
  @ReactMethod
764
787
  public void getBDMsgStatus(int slotId, Promise promise) {
765
788
  beidouAidlHelper.getStatus(slotId, promise);
789
+ // new Thread(() -> {
790
+ // BDMsgGetUserInfoTerminalResponse currentUserInfo = BDMsgAuthTerminalSdk.getInstance(reactContext).getCurrentUserInfo(slotId);
791
+ // promise.resolve(currentUserInfo.toString());
792
+ // }).start();
766
793
  }
767
794
 
768
795
  @ReactMethod
769
796
  public void checkValidatedKey(int slotId, Promise promise) {
770
797
  beidouAidlHelper.checkValidatedKey(slotId, promise);
798
+ // new Thread(() -> {
799
+ // int i = BDMsgAuthTerminalSdk.getInstance(reactContext).checkKeyIsExistAndValidate(slotId);
800
+ // promise.resolve(i);
801
+ // }).start();
771
802
  }
772
803
 
773
804
  @ReactMethod
774
805
  public void commonBDMsgEncrypt(int slotId, String speedType, @Nullable ReadableArray receiveList, String inputMsg,
775
806
  Promise promise) {
776
807
  beidouAidlHelper.commonBDMsgEncrypt(slotId, speedType, receiveList, inputMsg, promise);
808
+ // new Thread(() -> {
809
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
810
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).commonMessageEncryptBDMsgByTerminal(slotId, speedType, receiveStringList, inputMsg);
811
+ // promise.resolve(bdMsgSMCryptResponses.toString());
812
+ // }).start();
777
813
  }
778
814
 
779
815
  @ReactMethod
780
816
  public void positionBDMsgEncrypt(int slotId, String speedType, @Nullable ReadableArray receiveList, String inputMsg,
781
817
  double longitude, double latitude, Promise promise) {
782
818
  beidouAidlHelper.positionBDMsgEncrypt(slotId, speedType, receiveList, inputMsg, longitude, latitude, promise);
819
+ // new Thread(() -> {
820
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
821
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).positionReportEncryptBDMsgByTerminal(slotId, speedType, receiveStringList, inputMsg, longitude, latitude);
822
+ // promise.resolve(bdMsgSMCryptResponses.toString());
823
+ // }).start();
783
824
  }
784
825
 
785
826
  @ReactMethod
786
827
  public void commonEmergencyBDMsgEncrypt(int slotId, String speedType, @Nullable ReadableArray receiveList, String inputMsg,
787
828
  Promise promise) {
788
829
  beidouAidlHelper.commonEmergencyBDMsgEncrypt(slotId, speedType, receiveList, inputMsg, promise);
830
+ // new Thread(() -> {
831
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
832
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).commonMessageEncryptBDMsgByEmergencyTerminal(slotId, speedType, receiveStringList, inputMsg);
833
+ // promise.resolve(bdMsgSMCryptResponses.toString());
834
+ // }).start();
789
835
  }
790
836
 
791
837
  @ReactMethod
792
838
  public void positionEmergencyBDMsgEncrypt(int slotId, String speedType, @Nullable ReadableArray receiveList, String inputMsg,
793
839
  double longitude, double latitude, Promise promise) {
794
840
  beidouAidlHelper.positionEmergencyBDMsgEncrypt(slotId, speedType, receiveList, inputMsg, longitude, latitude, promise);
841
+ // new Thread(() -> {
842
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
843
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).positionReportEncryptBDMsgByEmergencyTerminal(slotId, speedType, receiveStringList, inputMsg, longitude, latitude);
844
+ // promise.resolve(bdMsgSMCryptResponses.toString());
845
+ // }).start();
795
846
  }
796
847
 
797
848
  @ReactMethod
798
849
  public void commonRichMediaBDMsgEncrypt(int slotId, String speedType, @Nullable ReadableArray receiveList, String msgType,
799
850
  @Nullable ReadableArray inputRichMediaData, Promise promise) {
800
851
  beidouAidlHelper.commonRichMediaBDMsgEncrypt(slotId, speedType, receiveList, msgType, inputRichMediaData, promise);
852
+ // new Thread(() -> {
853
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
854
+ // byte[] bytes = readableArrayToByteArray(inputRichMediaData);
855
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).commonRichMediaEncryptBDMsgByTerminal(slotId, speedType, receiveStringList,msgType,bytes);
856
+ // promise.resolve(bdMsgSMCryptResponses.toString());
857
+ // }).start();
801
858
  }
802
859
 
803
860
  @ReactMethod
@@ -805,6 +862,12 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
805
862
  @Nullable ReadableArray inputRichMediaData, double longitude, double latitude, Promise promise) {
806
863
  beidouAidlHelper.positionRichMediaBDMsgEncrypt(slotId, speedType, receiveList, msgType, inputRichMediaData, longitude,
807
864
  latitude, promise);
865
+ // new Thread(() -> {
866
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
867
+ // byte[] bytes = readableArrayToByteArray(inputRichMediaData);
868
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).positionRichMediaReportEncryptBDMsgByTerminal(slotId, speedType, receiveStringList, msgType, bytes, longitude, latitude);
869
+ // promise.resolve(bdMsgSMCryptResponses.toString());
870
+ // }).start();
808
871
  }
809
872
 
810
873
  @ReactMethod
@@ -812,6 +875,12 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
812
875
  String msgType, @Nullable ReadableArray inputRichMediaData, Promise promise) {
813
876
  beidouAidlHelper.commonRichMediaAndTextBDMsgEncrypt(slotId, speedType, receiveList, text, msgType, inputRichMediaData,
814
877
  promise);
878
+ // new Thread(() -> {
879
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
880
+ // byte[] bytes = readableArrayToByteArray(inputRichMediaData);
881
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).commonRichMediaAndTextEncryptBDMsgByTerminal(slotId, speedType, receiveStringList, text, msgType, bytes);
882
+ // promise.resolve(bdMsgSMCryptResponses.toString());
883
+ // }).start();
815
884
  }
816
885
 
817
886
  @ReactMethod
@@ -819,26 +888,49 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
819
888
  String msgType, @Nullable ReadableArray inputRichMediaData, double longitude, double latitude, Promise promise) {
820
889
  beidouAidlHelper.positionRichMediaAndTextBDMsgEncrypt(slotId, speedType, receiveList, text, msgType, inputRichMediaData,
821
890
  longitude, latitude, promise);
891
+ // new Thread(() -> {
892
+ // LinkedList<String> receiveStringList = readableArrayToStringList(receiveList);
893
+ // byte[] bytes = readableArrayToByteArray(inputRichMediaData);
894
+ // List<BDMsgSMCryptResponse> bdMsgSMCryptResponses = BDMsgAuthTerminalSdk.getInstance(reactContext).positionRichMediaAndTextReportEncryptBDMsgByTerminal(slotId, speedType, receiveStringList, text, msgType, bytes, longitude, latitude);
895
+ // promise.resolve(bdMsgSMCryptResponses.toString());
896
+ // }).start();
822
897
  }
823
898
 
824
899
  @ReactMethod
825
900
  public void BDMailboxQueryEncrypt(int slotId, Promise promise) {
826
901
  beidouAidlHelper.mailboxQueryEncrypt(slotId, promise);
902
+ // new Thread(() -> {
903
+ // BDMsgSMCryptResponse bdMsgSMCryptResponse = BDMsgAuthTerminalSdk.getInstance(reactContext).mailboxQueryEncryptBDMsg(slotId);
904
+ // promise.resolve(bdMsgSMCryptResponse.toString());
905
+ // }).start();
827
906
  }
828
907
 
829
908
  @ReactMethod
830
909
  public void EmergencyBDMailboxQueryEncrypt(int slotId, String replyMobile, Promise promise) {
831
910
  beidouAidlHelper.emergencyMailboxQueryEncrypt(slotId, replyMobile, promise);
911
+ // new Thread(() -> {
912
+ // BDMsgSMCryptResponse bdMsgSMCryptResponse = BDMsgAuthTerminalSdk.getInstance(reactContext).mailboxQueryEncryptBDMsgByEmergency(slotId,replyMobile);
913
+ // promise.resolve(bdMsgSMCryptResponse.toString());
914
+ // }).start();
832
915
  }
833
916
 
834
917
  @ReactMethod
835
918
  public void BDMsgDecrypt(int slotId, @Nullable ReadableArray msg, int len, Promise promise) {
836
919
  beidouAidlHelper.decryptMessage(slotId, msg, len, promise);
920
+ // new Thread(() -> {
921
+ // byte[] bytes = readableArrayToByteArray(msg);
922
+ // BDMsgMailReplyResponse bdMsgMailReplyResponse = BDMsgAuthTerminalSdk.getInstance(reactContext).mailboxReplyDecryptBDMsg(slotId, bytes, len);
923
+ // promise.resolve(bdMsgMailReplyResponse.toString());
924
+ // }).start();
837
925
  }
838
926
 
839
927
  @ReactMethod
840
928
  public void DLEphemerisFile(int slotId, String type, String version, Promise promise) {
841
929
  beidouAidlHelper.downloadEphemerisFile(slotId, type, version, promise);
930
+ // new Thread(() -> {
931
+ // BDMsgEphemerisFileDownloadResponse bdMsgEphemerisFileDownloadResponse = BDMsgAuthTerminalSdk.getInstance(reactContext).downloadEphemerisFile(slotId, type, version);
932
+ // promise.resolve(bdMsgEphemerisFileDownloadResponse.toString());
933
+ // }).start();
842
934
  }
843
935
 
844
936
  // 设置BLE密钥
@@ -856,12 +948,12 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
856
948
  }
857
949
 
858
950
  /**
859
- * 获取GPS经纬度(优先返回最近一次的已知位置)
951
+ * 获取GPS经纬度(优先返回最近一次的已知位置,无缓存则主动定位)
860
952
  * - 需要定位运行时权限(ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION)
861
953
  * - 返回:{ latitude, longitude, provider, accuracy, timestamp },若无可用位置则返回 null
862
954
  */
863
955
  @ReactMethod
864
- public void getGpsLocation(Promise promise) {
956
+ public void getGpsLocation(final Promise promise) {
865
957
  try {
866
958
  if (reactContext == null) {
867
959
  promise.reject("CONTEXT_NULL", "React上下文不可用");
@@ -877,51 +969,169 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
877
969
  return;
878
970
  }
879
971
 
880
- LocationManager locationManager = (LocationManager) reactContext.getSystemService(Context.LOCATION_SERVICE);
972
+ final LocationManager locationManager = (LocationManager) reactContext.getSystemService(Context.LOCATION_SERVICE);
881
973
  if (locationManager == null) {
882
974
  promise.reject("LOCATION_SERVICE_UNAVAILABLE", "无法获取定位服务");
883
975
  return;
884
976
  }
885
977
 
886
- Location gpsLocation = null;
887
- Location networkLocation = null;
888
- try {
889
- gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
890
- } catch (Exception ignored) {
891
- }
978
+ // 1. 尝试获取最近已知位置(缓存)
979
+ Location bestLocation = null;
892
980
  try {
893
- networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
894
- } catch (Exception ignored) {
895
- }
981
+ Location gpsLocation = null;
982
+ Location networkLocation = null;
896
983
 
897
- Location bestLocation = null;
898
- if (gpsLocation != null && networkLocation != null) {
899
- bestLocation = gpsLocation.getTime() >= networkLocation.getTime() ? gpsLocation : networkLocation;
900
- } else if (gpsLocation != null) {
901
- bestLocation = gpsLocation;
902
- } else if (networkLocation != null) {
903
- bestLocation = networkLocation;
984
+ try {
985
+ if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
986
+ gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
987
+ }
988
+ } catch (Exception ignored) {}
989
+
990
+ try {
991
+ if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
992
+ networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
993
+ }
994
+ } catch (Exception ignored) {}
995
+
996
+ if (gpsLocation != null && networkLocation != null) {
997
+ // 选择更新鲜的那个
998
+ bestLocation = gpsLocation.getTime() >= networkLocation.getTime() ? gpsLocation : networkLocation;
999
+ } else if (gpsLocation != null) {
1000
+ bestLocation = gpsLocation;
1001
+ } else if (networkLocation != null) {
1002
+ bestLocation = networkLocation;
1003
+ }
1004
+ } catch (Exception e) {
1005
+ Log.e("BeiDouBluetoothModule", "获取缓存位置失败: " + e.getMessage());
904
1006
  }
905
1007
 
906
- if (bestLocation == null) {
907
- // 没有可用的最近位置,返回null
908
- promise.resolve(null);
1008
+ // 如果找到位置且位置在2分钟(120000ms)以内,认为有效,直接返回
1009
+ if (bestLocation != null && (System.currentTimeMillis() - bestLocation.getTime() < 120000)) {
1010
+ promise.resolve(locationToMap(bestLocation));
909
1011
  return;
910
1012
  }
911
1013
 
912
- WritableMap map = new WritableNativeMap();
913
- map.putDouble("latitude", bestLocation.getLatitude());
914
- map.putDouble("longitude", bestLocation.getLongitude());
915
- map.putString("provider", bestLocation.getProvider());
916
- map.putDouble("accuracy", bestLocation.hasAccuracy() ? bestLocation.getAccuracy() : -1.0);
917
- map.putDouble("timestamp", (double) bestLocation.getTime());
918
- promise.resolve(map);
1014
+ // 2. 如果没有位置或位置太旧,请求单次定位更新
1015
+ // 切换到主线程执行定位请求
1016
+ new Handler(Looper.getMainLooper()).post(new Runnable() {
1017
+ @Override
1018
+ public void run() {
1019
+ requestSingleLocationUpdate(locationManager, promise);
1020
+ }
1021
+ });
1022
+
919
1023
  } catch (Exception e) {
920
1024
  logger.e("BeiDouBluetoothModule", "获取GPS位置失败: " + e.getMessage(), e);
921
1025
  promise.reject("LOCATION_ERROR", "获取GPS位置失败: " + e.getMessage(), e);
922
1026
  }
923
1027
  }
924
1028
 
1029
+ /**
1030
+ * 辅助方法:将 Location 对象转为 WritableMap
1031
+ */
1032
+ private WritableMap locationToMap(Location location) {
1033
+ WritableMap map = new WritableNativeMap();
1034
+ map.putDouble("latitude", location.getLatitude());
1035
+ map.putDouble("longitude", location.getLongitude());
1036
+ map.putString("provider", location.getProvider());
1037
+ map.putDouble("accuracy", location.hasAccuracy() ? location.getAccuracy() : -1.0);
1038
+ map.putDouble("timestamp", (double) location.getTime());
1039
+ return map;
1040
+ }
1041
+
1042
+ private LinkedList<String> readableArrayToStringList(@Nullable ReadableArray readableArray) {
1043
+ LinkedList<String> list = new LinkedList<>();
1044
+ if (readableArray == null) {
1045
+ return list;
1046
+ }
1047
+ for (int i = 0; i < readableArray.size(); i++) {
1048
+ list.add(readableArray.getString(i));
1049
+ }
1050
+ return list;
1051
+ }
1052
+ private byte[] readableArrayToByteArray(@Nullable ReadableArray readableArray) {
1053
+ if (readableArray == null) {
1054
+ return new byte[0];
1055
+ }
1056
+ byte[] bytes = new byte[readableArray.size()];
1057
+ for (int i = 0; i < readableArray.size(); i++) {
1058
+ bytes[i] = (byte) readableArray.getInt(i);
1059
+ }
1060
+ return bytes;
1061
+ }
1062
+ /**
1063
+ * 主动请求单次定位(带超时)
1064
+ */
1065
+ private void requestSingleLocationUpdate(final LocationManager locationManager, final Promise promise) {
1066
+ // 选择可用的 Provider
1067
+ final String provider;
1068
+ if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
1069
+ provider = LocationManager.NETWORK_PROVIDER;
1070
+ } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
1071
+ provider = LocationManager.GPS_PROVIDER;
1072
+ } else {
1073
+ promise.resolve(null); // 没有开启的Provider
1074
+ return;
1075
+ }
1076
+
1077
+ final AtomicBoolean isHandled = new AtomicBoolean(false);
1078
+ final Handler handler = new Handler(Looper.getMainLooper());
1079
+
1080
+ final LocationListener listener = new LocationListener() {
1081
+ @Override
1082
+ public void onLocationChanged(Location location) {
1083
+ // 确保只处理一次
1084
+ if (isHandled.compareAndSet(false, true)) {
1085
+ locationManager.removeUpdates(this);
1086
+ promise.resolve(locationToMap(location));
1087
+ }
1088
+ }
1089
+
1090
+ @Override
1091
+ public void onStatusChanged(String provider, int status, Bundle extras) {}
1092
+ @Override
1093
+ public void onProviderEnabled(String provider) {}
1094
+ @Override
1095
+ public void onProviderDisabled(String provider) {}
1096
+ };
1097
+
1098
+ try {
1099
+ // 请求定位更新:minTime=0, minDistance=0, 尽可能快地获取
1100
+ // 即使在 API 30+,requestLocationUpdates 依然有效,只是 requestSingleUpdate 被标记为 deprecated
1101
+ // 这里使用 requestLocationUpdates 模拟单次,兼容性更好
1102
+ locationManager.requestLocationUpdates(provider, 0L, 0f, listener, Looper.getMainLooper());
1103
+
1104
+ // 10秒超时机制
1105
+ handler.postDelayed(new Runnable() {
1106
+ @Override
1107
+ public void run() {
1108
+ if (isHandled.compareAndSet(false, true)) {
1109
+ // 超时后移除监听
1110
+ locationManager.removeUpdates(listener);
1111
+
1112
+ // 尝试最后一次获取缓存作为保底
1113
+ try {
1114
+ Location loc = locationManager.getLastKnownLocation(provider);
1115
+ if (loc != null) {
1116
+ promise.resolve(locationToMap(loc));
1117
+ } else {
1118
+ Log.w("BeiDouBluetoothModule", "定位超时,未获取到位置");
1119
+ promise.resolve(null);
1120
+ }
1121
+ } catch (Exception e) {
1122
+ promise.resolve(null);
1123
+ }
1124
+ }
1125
+ }
1126
+ }, 10000); // 10秒超时
1127
+
1128
+ } catch (SecurityException e) {
1129
+ if (isHandled.compareAndSet(false, true)) {
1130
+ promise.reject("PERMISSION_ERROR", e.getMessage());
1131
+ }
1132
+ }
1133
+ }
1134
+
925
1135
  // ================== 日志管理相关方法 ==================
926
1136
 
927
1137
  /**
@@ -73,8 +73,16 @@ public class BeidouAidlHelper {
73
73
  if (beidouService != null) {
74
74
  try {
75
75
  beidouService.setCallback(beidouServiceCallback);
76
+ if (beidouServicePromise != null) {
77
+ beidouServicePromise.resolve(true);
78
+ beidouServicePromise = null;
79
+ }
76
80
  } catch (Exception e) {
77
81
  Log.e("BeiDouService", "设置北斗服务回调失败: " + e.getMessage(), e);
82
+ if (beidouServicePromise != null) {
83
+ beidouServicePromise.reject("BIND_ERROR", "绑定北斗服务失败: " + e.getMessage(), e);
84
+ beidouServicePromise = null;
85
+ }
78
86
  }
79
87
  }
80
88
  }
@@ -91,16 +99,16 @@ public class BeidouAidlHelper {
91
99
  public BeidouAidlHelper(@Nullable ReactApplicationContext reactContext) {
92
100
  this.reactContext = reactContext;
93
101
  }
94
-
102
+ private Promise beidouServicePromise;
95
103
  public void bindService(String packageName, String actionName, Promise promise) {
96
104
  if (reactContext == null) {
97
105
  promise.reject("CONTEXT_NULL", "React上下文不可用");
98
106
  return;
99
107
  }
100
-
101
108
  try {
109
+ beidouServicePromise = promise;
102
110
  bindBeidouService(packageName, actionName);
103
- promise.resolve(true);
111
+ // promise.resolve(true);
104
112
  } catch (Exception e) {
105
113
  promise.reject("BIND_ERROR", "绑定北斗服务失败: " + e.getMessage(), e);
106
114
  }