react-native-beidou 1.0.3 → 1.0.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.
- package/BeiDouAIDLTestPage.tsx +7 -7
- package/Compass.android.tsx +20 -0
- package/android/.gradle/8.9/checksums/checksums.lock +0 -0
- package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +1 -1
- package/android/.idea/AndroidProjectSystem.xml +6 -0
- package/android/.idea/gradle.xml +13 -0
- package/android/.idea/migrations.xml +10 -0
- package/android/.idea/misc.xml +9 -0
- package/android/.idea/runConfigurations.xml +17 -0
- package/android/.idea/vcs.xml +6 -0
- package/android/build.gradle +3 -0
- package/android/src/main/AndroidManifest.xml +7 -0
- package/android/src/main/java/com/cmcc_rn_module/BeiDouBluetoothModule.java +343 -101
- package/android/src/main/java/com/cmcc_rn_module/BeiDouBluetoothPackage.java +4 -1
- package/android/src/main/java/com/cmcc_rn_module/ChatDBManager.java +306 -0
- package/android/src/main/java/com/cmcc_rn_module/JsonUtil.java +113 -7
- package/android/src/main/java/com/cmcc_rn_module/view/CompassManager.java +41 -0
- package/android/src/main/java/com/cmcc_rn_module/view/CompassView.java +277 -0
- package/android/src/main/libs/bluetooth-sdk.aar +0 -0
- package/index.ts +188 -10
- package/ios/BDTCompassViewManager.h +15 -0
- package/ios/BDTCompassViewManager.m +24 -0
- package/ios/BeiDouBluetoothModule.h +20 -0
- package/ios/BeiDouBluetoothModule.m +424 -0
- package/ios/BeidouBluetooth.framework/BeidouBluetooth +0 -0
- package/ios/BeidouBluetooth.framework/Headers/BDTBluetoothManager.h +76 -0
- package/ios/BeidouBluetooth.framework/Headers/BDTChatDBManager.h +41 -0
- package/ios/BeidouBluetooth.framework/Headers/BDTDataPacketBuilder.h +62 -0
- package/ios/BeidouBluetooth.framework/Headers/BeidouBluetooth.h +21 -0
- package/ios/BeidouBluetooth.framework/Info.plist +0 -0
- package/ios/BeidouBluetooth.framework/Modules/module.modulemap +6 -0
- package/ios/BeidouBluetooth.framework/_CodeSignature/CodeDirectory +0 -0
- package/ios/BeidouBluetooth.framework/_CodeSignature/CodeRequirements +0 -0
- package/ios/BeidouBluetooth.framework/_CodeSignature/CodeRequirements-1 +0 -0
- package/ios/BeidouBluetooth.framework/_CodeSignature/CodeResources +177 -0
- package/ios/BeidouBluetooth.framework/_CodeSignature/CodeSignature +0 -0
- package/ios/CompassView.h +20 -0
- package/ios/CompassView.m +201 -0
- package/package.json +2 -1
- package/android/.gradle/8.8/checksums/checksums.lock +0 -0
- package/android/.gradle/8.8/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/8.8/fileChanges/last-build.bin +0 -0
- package/android/.gradle/8.8/fileHashes/fileHashes.bin +0 -0
- package/android/.gradle/8.8/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/8.8/gc.properties +0 -0
- package/ios/Podfile +0 -79
- package/ios/build_sdk.sh +0 -30
- package/ios/cmcc_rn_module/AppDelegate.h +0 -8
- package/ios/cmcc_rn_module/AppDelegate.mm +0 -108
- package/ios/cmcc_rn_module/CMCCAssetsLoaderModule.h +0 -17
- package/ios/cmcc_rn_module/CMCCAssetsLoaderModule.m +0 -38
- package/ios/cmcc_rn_module/Images.xcassets/AppIcon.appiconset/Contents.json +0 -53
- package/ios/cmcc_rn_module/Images.xcassets/Contents.json +0 -6
- package/ios/cmcc_rn_module/Info.plist +0 -62
- package/ios/cmcc_rn_module/LaunchScreen.storyboard +0 -47
- package/ios/cmcc_rn_module/main.m +0 -10
- package/ios/cmcc_rn_module.xcodeproj/project.pbxproj +0 -708
- package/ios/cmcc_rn_module.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/cmcc_rn_module.xcodeproj/xcshareddata/xcschemes/cmcc_rn_module.xcscheme +0 -88
- package/ios/cmcc_rn_module.xcworkspace/contents.xcworkspacedata +0 -10
- package/ios/cmcc_rn_module.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
- package/ios/cmcc_rn_moduleTests/Info.plist +0 -24
- package/ios/cmcc_rn_moduleTests/cmcc_rn_moduleTests.m +0 -66
- package/ios/reset_env.sh +0 -24
- package/ios/scripts/RNReanimated.podspec +0 -90
- package/ios/scripts/generate_dynamic_pod.rb +0 -166
- package/ios/scripts/native_modules_dy.rb +0 -317
- package/ios/scripts/react_native_pods_dy.rb +0 -773
|
@@ -8,8 +8,11 @@ import android.net.ConnectivityManager;
|
|
|
8
8
|
import android.net.NetworkInfo;
|
|
9
9
|
import android.net.wifi.WifiInfo;
|
|
10
10
|
import android.net.wifi.WifiManager;
|
|
11
|
+
import android.nfc.Tag;
|
|
11
12
|
import android.os.Build;
|
|
12
13
|
import android.util.Log;
|
|
14
|
+
import android.location.Location;
|
|
15
|
+
import android.location.LocationManager;
|
|
13
16
|
|
|
14
17
|
import androidx.annotation.NonNull;
|
|
15
18
|
import androidx.annotation.Nullable;
|
|
@@ -19,6 +22,7 @@ import androidx.core.content.ContextCompat;
|
|
|
19
22
|
import com.beidou.bluetooth.BluetoothSDK;
|
|
20
23
|
import com.beidou.bluetooth.callback.BluetoothConnectionCallback;
|
|
21
24
|
import com.beidou.bluetooth.callback.BluetoothDataCallback;
|
|
25
|
+
import com.beidou.bluetooth.callback.BluetoothPushCallback;
|
|
22
26
|
import com.beidou.bluetooth.model.BlueDeviceInfoEntity;
|
|
23
27
|
import com.facebook.react.bridge.Callback;
|
|
24
28
|
import com.facebook.react.bridge.Promise;
|
|
@@ -49,17 +53,17 @@ import android.content.ServiceConnection;
|
|
|
49
53
|
import android.content.ComponentName;
|
|
50
54
|
import android.content.Intent;
|
|
51
55
|
import android.os.IBinder;
|
|
52
|
-
import android.os.RemoteException;
|
|
53
56
|
import com.facebook.react.bridge.ReadableArray;
|
|
54
57
|
import com.facebook.react.bridge.WritableArray;
|
|
55
58
|
import com.facebook.react.bridge.WritableNativeArray;
|
|
56
59
|
|
|
57
60
|
public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
58
61
|
private ReactContext reactContext;
|
|
59
|
-
|
|
62
|
+
|
|
60
63
|
// 北斗AIDL服务相关字段
|
|
61
64
|
private IBeidouMsgAidlInterface beidouService;
|
|
62
65
|
private boolean isBeidouServiceBound = false;
|
|
66
|
+
|
|
63
67
|
@NonNull
|
|
64
68
|
@Override
|
|
65
69
|
public String getName() {
|
|
@@ -69,41 +73,106 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
69
73
|
public BeiDouBluetoothModule(@Nullable ReactApplicationContext reactContext) {
|
|
70
74
|
super(reactContext);
|
|
71
75
|
this.reactContext = reactContext;
|
|
76
|
+
BluetoothSDK.getInstance(reactContext).setPushCallback(new BluetoothPushCallback() {
|
|
77
|
+
@Override
|
|
78
|
+
public void onDevicePush(String deviceNo, String data, int cmd) {
|
|
79
|
+
Log.d("BeiDouBluetoothModule","收到设备推送消息: "+data);
|
|
80
|
+
// 处理接收到的数据
|
|
81
|
+
WritableMap writableMap = new WritableNativeMap();
|
|
82
|
+
writableMap.putInt("type", 0);
|
|
83
|
+
writableMap.putString("message", data);
|
|
84
|
+
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
85
|
+
.emit("listenBluetoolth", writableMap);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
72
88
|
}
|
|
73
89
|
|
|
90
|
+
// 消息数据库管理器实例
|
|
91
|
+
private ChatDBManager chatDBManager;
|
|
92
|
+
|
|
93
|
+
// 获取数据库管理器实例
|
|
94
|
+
private ChatDBManager getChatDBManager() {
|
|
95
|
+
if (chatDBManager == null) {
|
|
96
|
+
chatDBManager = new ChatDBManager(reactContext);
|
|
97
|
+
}
|
|
98
|
+
return chatDBManager;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 插入北斗蓝牙短信
|
|
74
102
|
@ReactMethod
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
103
|
+
public void insertMsg(String phone, String content, Double timestamp, Boolean isSend) {
|
|
104
|
+
try {
|
|
105
|
+
long timeInterval = timestamp.longValue();
|
|
106
|
+
boolean send = isSend != null ? isSend : false;
|
|
107
|
+
getChatDBManager().insertMessage(content, phone, send, timeInterval);
|
|
108
|
+
Log.d("BeiDouBluetoothModule", String.format("插入消息成功 - 手机号: %s, 是否发送: %b", phone, send));
|
|
109
|
+
} catch (Exception e) {
|
|
110
|
+
Log.e("BeiDouBluetoothModule", "插入消息失败: " + e.getMessage());
|
|
111
|
+
}
|
|
78
112
|
}
|
|
79
113
|
|
|
80
|
-
|
|
114
|
+
// 获取会话列表
|
|
81
115
|
@ReactMethod
|
|
82
|
-
public void
|
|
116
|
+
public void getAllConversations(Promise promise) {
|
|
117
|
+
try {
|
|
118
|
+
List<Map<String, Object>> conversations = getChatDBManager().getAllConversations();
|
|
119
|
+
String jsonString = JsonUtil.objectToJson(conversations);
|
|
120
|
+
promise.resolve(jsonString);
|
|
121
|
+
} catch (Exception e) {
|
|
122
|
+
Log.e("BeiDouBluetoothModule", "获取会话列表失败: " + e.getMessage());
|
|
123
|
+
promise.reject("json_error", "序列化失败", e);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 获取会话详情
|
|
128
|
+
@ReactMethod
|
|
129
|
+
public void getMessagesForPhone(String phone, Integer page, Integer pageSize, Callback callback) {
|
|
130
|
+
try {
|
|
131
|
+
int pageNum = page != null ? page : 0;
|
|
132
|
+
int size = pageSize != null ? pageSize : 20;
|
|
133
|
+
|
|
134
|
+
List<Map<String, Object>> messages = getChatDBManager().getMessagesForPhone(phone, pageNum, size);
|
|
135
|
+
String jsonString = JsonUtil.objectToJson(messages);
|
|
136
|
+
Log.d("BeiDouBluetoothModule", "获取消息详情: " + jsonString);
|
|
137
|
+
callback.invoke(jsonString);
|
|
138
|
+
} catch (Exception e) {
|
|
139
|
+
Log.e("BeiDouBluetoothModule", "获取消息详情失败: " + e.getMessage());
|
|
140
|
+
callback.invoke("[]");
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 开始扫描蓝牙设备
|
|
145
|
+
@ReactMethod
|
|
146
|
+
public void startScanForService() {
|
|
83
147
|
BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
84
148
|
Log.d("BluetoothSDK", "MyModule-> 开始扫描");
|
|
85
149
|
instance.startScan();
|
|
86
150
|
}
|
|
87
|
-
|
|
151
|
+
|
|
152
|
+
// 停止扫描蓝牙设备
|
|
88
153
|
@ReactMethod
|
|
89
|
-
public void stopScanning(){
|
|
154
|
+
public void stopScanning() {
|
|
90
155
|
BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
91
156
|
instance.stopScan();
|
|
92
157
|
}
|
|
93
|
-
|
|
158
|
+
|
|
159
|
+
// 获取扫描结果
|
|
94
160
|
@ReactMethod
|
|
95
161
|
public void getDiscoveredDevices(Promise promise) throws JSONException {
|
|
96
162
|
BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
97
163
|
Map<String, BlueDeviceInfoEntity> deviceInfoMap = instance.getScannedDevices();
|
|
98
|
-
|
|
99
|
-
|
|
164
|
+
|
|
165
|
+
// 将 BlueDeviceInfoEntity 对象转换为 List 以便序列化
|
|
166
|
+
List<BlueDeviceInfoEntity> deviceList = new ArrayList<>(deviceInfoMap.values());
|
|
167
|
+
String s = JsonUtil.objectToJson(deviceList);
|
|
168
|
+
Log.d("BluetoothSDK", "获取设备列表:" + s);
|
|
100
169
|
promise.resolve(s);
|
|
101
170
|
}
|
|
102
171
|
|
|
103
|
-
|
|
172
|
+
// 根据 deviceNo 连接蓝牙设备
|
|
104
173
|
@ReactMethod
|
|
105
|
-
public void connectToDeviceFromCloudDeviceNO(String deviceNO,Double a, Promise promise){
|
|
106
|
-
if (StringUtils.isEmpty(deviceNO)){
|
|
174
|
+
public void connectToDeviceFromCloudDeviceNO(String deviceNO, Double a, Promise promise) {
|
|
175
|
+
if (StringUtils.isEmpty(deviceNO)) {
|
|
107
176
|
Log.e("BluetoothSDK", "connectToDeviceFromCloudDeviceNO: deviceNo isEmpty");
|
|
108
177
|
promise.resolve("fail");
|
|
109
178
|
return;
|
|
@@ -142,24 +211,24 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
142
211
|
*/
|
|
143
212
|
@ReactMethod
|
|
144
213
|
public void getAndroidNetWorkInfo(Callback callback) {
|
|
145
|
-
// -1
|
|
214
|
+
// -1 是未知网络 0 是无网络 1 是移动蜂窝网络 2 是WIFI
|
|
146
215
|
Number net = 0;
|
|
147
216
|
ConnectivityManager connMgr = (ConnectivityManager) reactContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
148
|
-
|
|
217
|
+
// 无线WIFI网络
|
|
149
218
|
NetworkInfo WifiNetWorkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
|
150
|
-
boolean Wificonnected = WifiNetWorkInfo.isConnected();
|
|
219
|
+
boolean Wificonnected = WifiNetWorkInfo.isConnected(); // 判断是否连接WiFi
|
|
151
220
|
System.out.println("获取本机token::::::::::::::Wificonnected" + Wificonnected);
|
|
152
|
-
|
|
221
|
+
// 移动网络
|
|
153
222
|
NetworkInfo MobilenetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
|
154
|
-
boolean Mobileconnected = MobilenetworkInfo.isConnected()
|
|
223
|
+
boolean Mobileconnected = MobilenetworkInfo.isConnected();// 判断是否连接移动网络
|
|
155
224
|
System.out.println("获取本机token::::::::::::::Mobileconnected" + Mobileconnected);
|
|
156
225
|
|
|
157
|
-
|
|
226
|
+
// 未知网络
|
|
158
227
|
// NetworkInfo[] networkInfos= connMgr.getAllNetworkInfo();
|
|
159
228
|
// for(NetworkInfo info:networkInfos){
|
|
160
|
-
//
|
|
161
|
-
//
|
|
162
|
-
//
|
|
229
|
+
// if(info.isAvailable()){
|
|
230
|
+
// net = -1;
|
|
231
|
+
// }
|
|
163
232
|
// }
|
|
164
233
|
if (Mobileconnected || Wificonnected) {
|
|
165
234
|
net = -1;
|
|
@@ -186,10 +255,13 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
186
255
|
String ret = "";
|
|
187
256
|
System.out.println("获取Android版本1111::::::::::::::Build.VERSION.SDK_INT" + Build.VERSION.SDK_INT);
|
|
188
257
|
System.out.println("获取Android版本1::::::::::::::Build.VERSION.SDK_INT" + Build.VERSION.SDK_INT);
|
|
189
|
-
// 从Android 12(API 31)开始,蓝牙权限有所变化,新增了 BLUETOOTH_SCAN 和 BLUETOOTH_CONNECT 权限。你需要在
|
|
258
|
+
// 从Android 12(API 31)开始,蓝牙权限有所变化,新增了 BLUETOOTH_SCAN 和 BLUETOOTH_CONNECT 权限。你需要在
|
|
259
|
+
// AndroidManifest.xml 中声明这些权限,并在代码中动态请求。
|
|
190
260
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
191
|
-
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(reactContext.getCurrentActivity()),
|
|
192
|
-
|
|
261
|
+
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(reactContext.getCurrentActivity()),
|
|
262
|
+
Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ||
|
|
263
|
+
ContextCompat.checkSelfPermission(reactContext.getCurrentActivity(),
|
|
264
|
+
Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
193
265
|
// 请求权限
|
|
194
266
|
ret = "NO31";
|
|
195
267
|
} else {
|
|
@@ -198,9 +270,13 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
198
270
|
}
|
|
199
271
|
} else { // 从Android 6.0(API 23)开始,部分权限需要在运行时动态申请。蓝牙权限和位置权限通常需要动态申请。
|
|
200
272
|
// 检查蓝牙权限
|
|
201
|
-
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(reactContext.getCurrentActivity()),
|
|
202
|
-
|
|
203
|
-
ContextCompat.checkSelfPermission(reactContext.getCurrentActivity(),
|
|
273
|
+
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(reactContext.getCurrentActivity()),
|
|
274
|
+
Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED ||
|
|
275
|
+
ContextCompat.checkSelfPermission(reactContext.getCurrentActivity(),
|
|
276
|
+
Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED
|
|
277
|
+
||
|
|
278
|
+
ContextCompat.checkSelfPermission(reactContext.getCurrentActivity(),
|
|
279
|
+
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
204
280
|
// 请求权限
|
|
205
281
|
ret = "NO23";
|
|
206
282
|
} else {
|
|
@@ -219,13 +295,15 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
219
295
|
String ret = "";
|
|
220
296
|
System.out.println("获取Android版本2::::::::::::::Build.VERSION.SDK_INT" + Build.VERSION.SDK_INT);
|
|
221
297
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
222
|
-
if (ActivityCompat.shouldShowRequestPermissionRationale(this.reactContext.getCurrentActivity(),
|
|
298
|
+
if (ActivityCompat.shouldShowRequestPermissionRationale(this.reactContext.getCurrentActivity(),
|
|
299
|
+
Manifest.permission.BLUETOOTH_SCAN)) {
|
|
223
300
|
ret = "OK"; // 设置里面禁止了之后此处会弹窗
|
|
224
301
|
} else {
|
|
225
302
|
ret = "NO";
|
|
226
303
|
}
|
|
227
304
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
|
228
|
-
if (ActivityCompat.shouldShowRequestPermissionRationale(this.reactContext.getCurrentActivity(),
|
|
305
|
+
if (ActivityCompat.shouldShowRequestPermissionRationale(this.reactContext.getCurrentActivity(),
|
|
306
|
+
Manifest.permission.ACCESS_FINE_LOCATION)) {
|
|
229
307
|
ret = "OK"; // 设置里面禁止了之后此处会弹窗
|
|
230
308
|
} else {
|
|
231
309
|
ret = "NO";
|
|
@@ -255,8 +333,8 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
255
333
|
// 蓝牙未启用,可以请求用户启用蓝牙
|
|
256
334
|
ret = "NO"; // 设备没有开启蓝牙
|
|
257
335
|
// 你可以使用以下代码启动蓝牙启用请求
|
|
258
|
-
//
|
|
259
|
-
//
|
|
336
|
+
// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
337
|
+
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
|
260
338
|
} else {
|
|
261
339
|
ret = "OK"; // 设备已经开启蓝牙
|
|
262
340
|
}
|
|
@@ -267,40 +345,35 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
267
345
|
}
|
|
268
346
|
|
|
269
347
|
@ReactMethod(isBlockingSynchronousMethod = true)
|
|
270
|
-
public String
|
|
348
|
+
public String generateRandom16BytesString() {
|
|
271
349
|
byte[] bytes = ByteUtil.generateRandomHexBytes(32);
|
|
272
350
|
Log.d("BluetoothSDK", "获取随机字节数组:" + Arrays.toString(bytes));
|
|
273
|
-
//
|
|
351
|
+
// String b = java.util.Base64.getEncoder().encodeToString(bytes);
|
|
274
352
|
String b = "2PJFUoUNB502/KJdVQg5Kg==";
|
|
275
|
-
Log.d("BluetoothSDK", "随机字节数组加密 Base64字符串:" +
|
|
353
|
+
Log.d("BluetoothSDK", "随机字节数组加密 Base64字符串:" + b);
|
|
276
354
|
return b;
|
|
277
355
|
}
|
|
278
356
|
|
|
279
|
-
//
|
|
280
|
-
//
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
//
|
|
284
|
-
//
|
|
285
|
-
//
|
|
286
|
-
//
|
|
287
|
-
//
|
|
357
|
+
// @ReactMethod
|
|
358
|
+
// public void writeData(String randomString,String deviceNo, Integer
|
|
359
|
+
// command,Integer attrType, Callback writeCallback){
|
|
360
|
+
// BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
361
|
+
// Log.d(BluetoothSDK.TAG, "收到写入请求" + command +" "+attrType+" "+randomString);
|
|
362
|
+
// Callback callback = args -> {
|
|
363
|
+
// writeCallback.invoke(args);
|
|
364
|
+
// };
|
|
365
|
+
// instance.writeData(randomString, deviceNo, command, attrType,true,callback);
|
|
366
|
+
// }
|
|
288
367
|
@ReactMethod
|
|
289
|
-
public void writeData(String randomString,String deviceNo, Integer command,Integer attrType,
|
|
368
|
+
public void writeData(String randomString, String deviceNo, Integer command, Integer attrType,
|
|
369
|
+
Callback writeCallback) {
|
|
290
370
|
BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
291
|
-
Log.d("BluetoothSDK", "收到写入请求" + command +" "+attrType+" "+randomString);
|
|
371
|
+
Log.d("BluetoothSDK", "收到写入请求" + command + " " + attrType + " " + randomString);
|
|
292
372
|
BluetoothDataCallback dataCallback = new BluetoothDataCallback() {
|
|
293
373
|
@Override
|
|
294
374
|
public void onDataWriteSuccess(String deviceNo, byte[] data) {
|
|
295
|
-
byte[] cipher = Arrays.copyOfRange(data, 4, data.length);
|
|
296
|
-
String message = Base64.getEncoder().encodeToString(cipher);
|
|
297
|
-
|
|
298
|
-
WritableMap writableMap = new WritableNativeMap();
|
|
299
|
-
writableMap.putInt("type", 0);
|
|
300
|
-
writableMap.putString("message", message);
|
|
301
375
|
writeCallback.invoke(1);
|
|
302
|
-
|
|
303
|
-
.emit("listenBluetoolth",writableMap);
|
|
376
|
+
|
|
304
377
|
}
|
|
305
378
|
|
|
306
379
|
@Override
|
|
@@ -311,6 +384,14 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
311
384
|
@Override
|
|
312
385
|
public void onDataReceived(String deviceNo, byte[] data, int dataType) {
|
|
313
386
|
// 处理接收到的数据
|
|
387
|
+
// byte[] cipher = Arrays.copyOfRange(data, 4, data.length);
|
|
388
|
+
String message = Base64.getEncoder().encodeToString(data);
|
|
389
|
+
|
|
390
|
+
WritableMap writableMap = new WritableNativeMap();
|
|
391
|
+
writableMap.putInt("type", 0);
|
|
392
|
+
writableMap.putString("message", message);
|
|
393
|
+
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
394
|
+
.emit("listenBluetoolth", writableMap);
|
|
314
395
|
}
|
|
315
396
|
|
|
316
397
|
@Override
|
|
@@ -324,29 +405,109 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
324
405
|
}
|
|
325
406
|
};
|
|
326
407
|
instance.setDataCallback(dataCallback);
|
|
327
|
-
instance.writeData(randomString, deviceNo, command, attrType
|
|
408
|
+
instance.writeData(randomString, deviceNo, command, attrType);
|
|
328
409
|
}
|
|
329
410
|
|
|
411
|
+
// 存储BLE密钥的字段
|
|
412
|
+
private String bleKeyHex = "";
|
|
413
|
+
|
|
330
414
|
@ReactMethod
|
|
331
|
-
public void writeInfo(String
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
//
|
|
337
|
-
|
|
415
|
+
public void writeInfo(@Nullable String base64Payload, String deviceNO, Integer command, Integer opCode,
|
|
416
|
+
Integer attrType, Boolean encrypted, Promise promise) {
|
|
417
|
+
try {
|
|
418
|
+
BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
419
|
+
|
|
420
|
+
// 解码base64载荷
|
|
421
|
+
byte[] decodedData = null;
|
|
422
|
+
if (base64Payload != null && base64Payload.length() > 0) {
|
|
423
|
+
try {
|
|
424
|
+
decodedData = Base64.getDecoder().decode(base64Payload);
|
|
425
|
+
Log.d("BluetoothSDK", "解码成功,数据长度: " + decodedData.length);
|
|
426
|
+
} catch (Exception e) {
|
|
427
|
+
Log.e("BluetoothSDK", "解码base64String失败: " + e.getMessage());
|
|
428
|
+
promise.reject("DECODE_ERROR", "解码base64String失败", e);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
} else {
|
|
432
|
+
decodedData = new byte[0];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// 转换参数类型
|
|
436
|
+
int commandValue = command & 0xFF;
|
|
437
|
+
int opCodeValue = opCode & 0xFF;
|
|
438
|
+
int attrTypeValue = attrType & 0xFFFF;
|
|
439
|
+
boolean isEncrypted = encrypted != null ? encrypted : false;
|
|
440
|
+
|
|
441
|
+
Log.d("BluetoothSDK", String.format("writeInfo - deviceNO: %s, command: 0x%02X, opCode: 0x%02X, attrType: 0x%04X, encrypted: %b",
|
|
442
|
+
deviceNO, commandValue, opCodeValue, attrTypeValue, isEncrypted));
|
|
443
|
+
|
|
444
|
+
// 设置数据回调
|
|
445
|
+
BluetoothDataCallback dataCallback = new BluetoothDataCallback() {
|
|
446
|
+
@Override
|
|
447
|
+
public void onDataWriteSuccess(String deviceNo, byte[] data) {
|
|
448
|
+
Log.d("BluetoothSDK", "writeInfo写入成功");
|
|
449
|
+
// 返回成功结果
|
|
450
|
+
// WritableMap resDict = new WritableNativeMap();
|
|
451
|
+
// resDict.putInt("type", 1);
|
|
452
|
+
// resDict.putString("data", "");
|
|
453
|
+
// promise.resolve(resDict);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
@Override
|
|
457
|
+
public void onDataWriteFailed(String deviceNo, byte[] data, int errorCode, String errorMessage) {
|
|
458
|
+
Log.e("BluetoothSDK", "writeInfo写入失败: " + errorMessage);
|
|
459
|
+
promise.reject(String.valueOf(errorCode), "写入失败: " + errorMessage);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
@Override
|
|
463
|
+
public void onDataReceived(String deviceNo, byte[] data, int dataType) {
|
|
464
|
+
// 处理接收到的数据
|
|
465
|
+
Log.d("BluetoothSDK", "writeInfo收到数据,长度: " + data.length);
|
|
466
|
+
String base64String = Base64.getEncoder().encodeToString(data);
|
|
467
|
+
|
|
468
|
+
WritableMap resDict = new WritableNativeMap();
|
|
469
|
+
resDict.putInt("type", 1);
|
|
470
|
+
resDict.putString("data", base64String);
|
|
471
|
+
promise.resolve(resDict);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
@Override
|
|
475
|
+
public void onDataReadSuccess(String deviceNo, byte[] data) {
|
|
476
|
+
// 不需要处理
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
@Override
|
|
480
|
+
public void onDataReadFailed(String deviceNo, int errorCode, String errorMessage) {
|
|
481
|
+
// 不需要处理
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
instance.setDataCallback(dataCallback);
|
|
486
|
+
|
|
487
|
+
// 调用SDK写入数据
|
|
488
|
+
String dataString = Base64.getEncoder().encodeToString(decodedData);
|
|
489
|
+
instance.writeData(dataString, deviceNO, commandValue, attrTypeValue,opCodeValue, isEncrypted);
|
|
490
|
+
|
|
491
|
+
} catch (Exception e) {
|
|
492
|
+
Log.e("BluetoothSDK", "writeInfo异常: " + e.getMessage());
|
|
493
|
+
promise.reject("WRITE_ERROR", "写入数据异常: " + e.getMessage(), e);
|
|
494
|
+
}
|
|
338
495
|
}
|
|
496
|
+
|
|
339
497
|
@ReactMethod
|
|
340
|
-
public void writeValueInfo(String randomString,String deviceNo, Integer command,Integer attrType,Integer
|
|
341
|
-
|
|
342
|
-
//
|
|
343
|
-
//
|
|
344
|
-
//
|
|
345
|
-
//
|
|
346
|
-
//
|
|
498
|
+
public void writeValueInfo(String randomString, String deviceNo, Integer command, Integer attrType, Integer value,
|
|
499
|
+
Callback writeCallback) {
|
|
500
|
+
// BluetoothSDK instance = BluetoothSDK.getInstance(reactContext);
|
|
501
|
+
// Log.d(BluetoothSDK.TAG, "收到写入请求" + command +" "+attrType+" "+randomString);
|
|
502
|
+
// Callback callback = args -> {
|
|
503
|
+
// writeCallback.invoke(args);
|
|
504
|
+
// };
|
|
505
|
+
// instance.writeData(randomString, deviceNo, command,
|
|
506
|
+
// attrType,encrypt,callback);
|
|
347
507
|
}
|
|
508
|
+
|
|
348
509
|
@ReactMethod
|
|
349
|
-
public void disConnectWithDeviceUUID(String deviceNo,Promise promise) {
|
|
510
|
+
public void disConnectWithDeviceUUID(String deviceNo, Promise promise) {
|
|
350
511
|
BluetoothConnectionCallback disConnectCallback = new BluetoothConnectionCallback() {
|
|
351
512
|
@Override
|
|
352
513
|
public void onConnected(String deviceNo) {
|
|
@@ -371,8 +532,10 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
371
532
|
BluetoothSDK.getInstance(reactContext).disconnectDevice(deviceNo, disConnectCallback);
|
|
372
533
|
}
|
|
373
534
|
|
|
535
|
+
|
|
536
|
+
|
|
374
537
|
@ReactMethod
|
|
375
|
-
public void fetchCurrentWifiName(Callback callback){
|
|
538
|
+
public void fetchCurrentWifiName(Callback callback) {
|
|
376
539
|
String wifiName = "未连接";
|
|
377
540
|
|
|
378
541
|
try {
|
|
@@ -384,8 +547,7 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
384
547
|
// 检查ACCESS_FINE_LOCATION权限(Android 10+需要)
|
|
385
548
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
386
549
|
if (ContextCompat.checkSelfPermission(reactContext,
|
|
387
|
-
Manifest.permission.ACCESS_FINE_LOCATION)
|
|
388
|
-
!= PackageManager.PERMISSION_GRANTED) {
|
|
550
|
+
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
389
551
|
Log.w("WifiInfo", "缺少ACCESS_FINE_LOCATION权限,无法获取WiFi名称");
|
|
390
552
|
callback.invoke("权限不足");
|
|
391
553
|
return;
|
|
@@ -460,11 +622,11 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
460
622
|
private void bindBeidouService(String packageName, String actionName) {
|
|
461
623
|
if (!isBeidouServiceBound && reactContext != null) {
|
|
462
624
|
Intent intent = new Intent();
|
|
463
|
-
|
|
464
|
-
intent.
|
|
625
|
+
// 使用ComponentName方式,actionName作为className
|
|
626
|
+
intent.setComponent(new android.content.ComponentName(packageName, actionName));
|
|
465
627
|
try {
|
|
466
628
|
boolean result = reactContext.bindService(intent, beidouServiceConnection, Context.BIND_AUTO_CREATE);
|
|
467
|
-
Log.d("BeiDouService", "绑定北斗服务结果: " + result + ", 包名: " + packageName + ",
|
|
629
|
+
Log.d("BeiDouService", "绑定北斗服务结果: " + result + ", 包名: " + packageName + ", 类名: " + actionName);
|
|
468
630
|
} catch (Exception e) {
|
|
469
631
|
Log.e("BeiDouService", "绑定北斗服务失败: " + e.getMessage());
|
|
470
632
|
}
|
|
@@ -538,11 +700,11 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
538
700
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
539
701
|
return;
|
|
540
702
|
}
|
|
541
|
-
|
|
703
|
+
|
|
542
704
|
try {
|
|
543
705
|
boolean result = beidouService.BDAuthenticate();
|
|
544
706
|
promise.resolve(result);
|
|
545
|
-
} catch (
|
|
707
|
+
} catch (Exception e) {
|
|
546
708
|
promise.reject("REMOTE_ERROR", "北斗鉴权失败: " + e.getMessage());
|
|
547
709
|
}
|
|
548
710
|
}
|
|
@@ -553,7 +715,7 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
553
715
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
554
716
|
return;
|
|
555
717
|
}
|
|
556
|
-
|
|
718
|
+
|
|
557
719
|
try {
|
|
558
720
|
BDStatusResponse response = beidouService.getBDMsgStatus();
|
|
559
721
|
WritableMap map = new WritableNativeMap();
|
|
@@ -561,7 +723,7 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
561
723
|
map.putString("statusMsg", response.statusMsg);
|
|
562
724
|
map.putInt("msgQuotaRemain", response.msgQuotaRemain);
|
|
563
725
|
promise.resolve(map);
|
|
564
|
-
} catch (
|
|
726
|
+
} catch (Exception e) {
|
|
565
727
|
promise.reject("REMOTE_ERROR", "获取北斗状态失败: " + e.getMessage());
|
|
566
728
|
}
|
|
567
729
|
}
|
|
@@ -572,11 +734,11 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
572
734
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
573
735
|
return;
|
|
574
736
|
}
|
|
575
|
-
|
|
737
|
+
|
|
576
738
|
try {
|
|
577
739
|
int result = beidouService.checkValidatedKey();
|
|
578
740
|
promise.resolve(result);
|
|
579
|
-
} catch (
|
|
741
|
+
} catch (Exception e) {
|
|
580
742
|
promise.reject("REMOTE_ERROR", "检查北斗密钥失败: " + e.getMessage());
|
|
581
743
|
}
|
|
582
744
|
}
|
|
@@ -587,40 +749,42 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
587
749
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
588
750
|
return;
|
|
589
751
|
}
|
|
590
|
-
|
|
752
|
+
|
|
591
753
|
try {
|
|
592
754
|
List<String> receiveStringList = readableArrayToStringList(receiveList);
|
|
593
755
|
BDMsgEncryptResponse response = beidouService.commonBDMsgEncrypt(receiveStringList, inputMsg);
|
|
594
|
-
|
|
756
|
+
|
|
595
757
|
WritableMap map = new WritableNativeMap();
|
|
596
758
|
map.putInt("code", response.code);
|
|
597
759
|
map.putString("statusMsg", response.statusMsg);
|
|
598
760
|
map.putArray("data", byteArrayToWritableArray(response.data));
|
|
599
761
|
map.putString("sendMobile", response.sendMobile);
|
|
600
762
|
promise.resolve(map);
|
|
601
|
-
} catch (
|
|
763
|
+
} catch (Exception e) {
|
|
602
764
|
promise.reject("REMOTE_ERROR", "通用报文加密失败: " + e.getMessage());
|
|
603
765
|
}
|
|
604
766
|
}
|
|
605
767
|
|
|
606
768
|
@ReactMethod
|
|
607
|
-
public void positionBDMsgEncrypt(ReadableArray receiveList, String inputMsg, double longitude, double latitude,
|
|
769
|
+
public void positionBDMsgEncrypt(ReadableArray receiveList, String inputMsg, double longitude, double latitude,
|
|
770
|
+
Promise promise) {
|
|
608
771
|
if (!isBeidouServiceBound || beidouService == null) {
|
|
609
772
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
610
773
|
return;
|
|
611
774
|
}
|
|
612
|
-
|
|
775
|
+
|
|
613
776
|
try {
|
|
614
777
|
List<String> receiveStringList = readableArrayToStringList(receiveList);
|
|
615
|
-
BDMsgEncryptResponse response = beidouService.positionBDMsgEncrypt(receiveStringList, inputMsg, longitude,
|
|
616
|
-
|
|
778
|
+
BDMsgEncryptResponse response = beidouService.positionBDMsgEncrypt(receiveStringList, inputMsg, longitude,
|
|
779
|
+
latitude);
|
|
780
|
+
|
|
617
781
|
WritableMap map = new WritableNativeMap();
|
|
618
782
|
map.putInt("code", response.code);
|
|
619
783
|
map.putString("statusMsg", response.statusMsg);
|
|
620
784
|
map.putArray("data", byteArrayToWritableArray(response.data));
|
|
621
785
|
map.putString("sendMobile", response.sendMobile);
|
|
622
786
|
promise.resolve(map);
|
|
623
|
-
} catch (
|
|
787
|
+
} catch (Exception e) {
|
|
624
788
|
promise.reject("REMOTE_ERROR", "位置报文加密失败: " + e.getMessage());
|
|
625
789
|
}
|
|
626
790
|
}
|
|
@@ -631,17 +795,17 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
631
795
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
632
796
|
return;
|
|
633
797
|
}
|
|
634
|
-
|
|
798
|
+
|
|
635
799
|
try {
|
|
636
800
|
BDMsgEncryptResponse response = beidouService.BDMailboxQueryEncrypt();
|
|
637
|
-
|
|
801
|
+
|
|
638
802
|
WritableMap map = new WritableNativeMap();
|
|
639
803
|
map.putInt("code", response.code);
|
|
640
804
|
map.putString("statusMsg", response.statusMsg);
|
|
641
805
|
map.putArray("data", byteArrayToWritableArray(response.data));
|
|
642
806
|
map.putString("sendMobile", response.sendMobile);
|
|
643
807
|
promise.resolve(map);
|
|
644
|
-
} catch (
|
|
808
|
+
} catch (Exception e) {
|
|
645
809
|
promise.reject("REMOTE_ERROR", "信箱查询加密失败: " + e.getMessage());
|
|
646
810
|
}
|
|
647
811
|
}
|
|
@@ -652,18 +816,18 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
652
816
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
653
817
|
return;
|
|
654
818
|
}
|
|
655
|
-
|
|
819
|
+
|
|
656
820
|
try {
|
|
657
821
|
byte[] msgBytes = readableArrayToByteArray(msg);
|
|
658
822
|
BDMsgDecryptResponse response = beidouService.BDMsgDecrypt(msgBytes, len);
|
|
659
|
-
|
|
823
|
+
|
|
660
824
|
WritableMap map = new WritableNativeMap();
|
|
661
825
|
map.putInt("statusMsg", response.statusMsg);
|
|
662
826
|
map.putInt("isRemainMsg", response.isRemainMsg);
|
|
663
827
|
map.putString("rcvMobile", response.rcvMobile);
|
|
664
828
|
map.putString("data", response.data);
|
|
665
829
|
promise.resolve(map);
|
|
666
|
-
} catch (
|
|
830
|
+
} catch (Exception e) {
|
|
667
831
|
promise.reject("REMOTE_ERROR", "报文解密失败: " + e.getMessage());
|
|
668
832
|
}
|
|
669
833
|
}
|
|
@@ -674,19 +838,97 @@ public class BeiDouBluetoothModule extends ReactContextBaseJavaModule {
|
|
|
674
838
|
promise.reject("SERVICE_NOT_BOUND", "北斗服务未绑定,请先调用bindBeidouServiceMethod");
|
|
675
839
|
return;
|
|
676
840
|
}
|
|
677
|
-
|
|
841
|
+
|
|
678
842
|
try {
|
|
679
843
|
BDMsgEphemerisFileDLResponse response = beidouService.DLEphemerisFile(type, version);
|
|
680
|
-
|
|
844
|
+
|
|
681
845
|
WritableMap map = new WritableNativeMap();
|
|
682
846
|
map.putInt("code", response.code);
|
|
683
847
|
map.putString("statusMsg", response.statusMsg);
|
|
684
848
|
map.putArray("ephemerisZipFile", byteArrayToWritableArray(response.ephemerisZipFile));
|
|
685
849
|
map.putString("version", response.version);
|
|
686
850
|
promise.resolve(map);
|
|
687
|
-
} catch (
|
|
851
|
+
} catch (Exception e) {
|
|
688
852
|
promise.reject("REMOTE_ERROR", "下载星历文件失败: " + e.getMessage());
|
|
689
853
|
}
|
|
690
854
|
}
|
|
691
855
|
|
|
856
|
+
// 设置BLE密钥
|
|
857
|
+
@ReactMethod
|
|
858
|
+
public void setBLEKey(String bleKey, Promise promise) {
|
|
859
|
+
try {
|
|
860
|
+
this.bleKeyHex = bleKey;
|
|
861
|
+
BluetoothSDK.getInstance(reactContext).setBleKey(bleKey); // 32位hex
|
|
862
|
+
Log.d("BeiDouBluetoothModule", "设置BLE密钥成功: " + bleKey);
|
|
863
|
+
promise.resolve(1); // 返回成功标志
|
|
864
|
+
} catch (Exception e) {
|
|
865
|
+
Log.e("BeiDouBluetoothModule", "设置BLE密钥失败: " + e.getMessage());
|
|
866
|
+
promise.reject("SET_KEY_ERROR", "设置BLE密钥失败: " + e.getMessage(), e);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* 获取GPS经纬度(优先返回最近一次的已知位置)
|
|
872
|
+
* - 需要定位运行时权限(ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION)
|
|
873
|
+
* - 返回:{ latitude, longitude, provider, accuracy, timestamp },若无可用位置则返回 null
|
|
874
|
+
*/
|
|
875
|
+
@ReactMethod
|
|
876
|
+
public void getGpsLocation(Promise promise) {
|
|
877
|
+
try {
|
|
878
|
+
if (reactContext == null) {
|
|
879
|
+
promise.reject("CONTEXT_NULL", "React上下文不可用");
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
boolean fineGranted = ContextCompat.checkSelfPermission(reactContext,
|
|
884
|
+
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
|
885
|
+
boolean coarseGranted = ContextCompat.checkSelfPermission(reactContext,
|
|
886
|
+
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
|
|
887
|
+
if (!fineGranted && !coarseGranted) {
|
|
888
|
+
promise.reject("PERMISSION_DENIED", "缺少定位权限");
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
LocationManager locationManager = (LocationManager) reactContext.getSystemService(Context.LOCATION_SERVICE);
|
|
893
|
+
if (locationManager == null) {
|
|
894
|
+
promise.reject("LOCATION_SERVICE_UNAVAILABLE", "无法获取定位服务");
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
Location gpsLocation = null;
|
|
899
|
+
Location networkLocation = null;
|
|
900
|
+
try {
|
|
901
|
+
gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
|
902
|
+
} catch (Exception ignored) { }
|
|
903
|
+
try {
|
|
904
|
+
networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
|
905
|
+
} catch (Exception ignored) { }
|
|
906
|
+
|
|
907
|
+
Location bestLocation = null;
|
|
908
|
+
if (gpsLocation != null && networkLocation != null) {
|
|
909
|
+
bestLocation = gpsLocation.getTime() >= networkLocation.getTime() ? gpsLocation : networkLocation;
|
|
910
|
+
} else if (gpsLocation != null) {
|
|
911
|
+
bestLocation = gpsLocation;
|
|
912
|
+
} else if (networkLocation != null) {
|
|
913
|
+
bestLocation = networkLocation;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
if (bestLocation == null) {
|
|
917
|
+
// 没有可用的最近位置,返回null
|
|
918
|
+
promise.resolve(null);
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
WritableMap map = new WritableNativeMap();
|
|
923
|
+
map.putDouble("latitude", bestLocation.getLatitude());
|
|
924
|
+
map.putDouble("longitude", bestLocation.getLongitude());
|
|
925
|
+
map.putString("provider", bestLocation.getProvider());
|
|
926
|
+
map.putDouble("accuracy", bestLocation.hasAccuracy() ? bestLocation.getAccuracy() : -1.0);
|
|
927
|
+
map.putDouble("timestamp", (double) bestLocation.getTime());
|
|
928
|
+
promise.resolve(map);
|
|
929
|
+
} catch (Exception e) {
|
|
930
|
+
Log.e("BeiDouBluetoothModule", "获取GPS位置失败: " + e.getMessage());
|
|
931
|
+
promise.reject("LOCATION_ERROR", "获取GPS位置失败: " + e.getMessage(), e);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
692
934
|
}
|