react-native-beidou 1.0.2 → 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 +710 -0
- package/Compass.android.tsx +20 -0
- package/TestPage.ts +6 -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 +5 -2
- 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
|
@@ -2,6 +2,7 @@ package com.cmcc_rn_module;
|
|
|
2
2
|
|
|
3
3
|
import androidx.annotation.NonNull;
|
|
4
4
|
|
|
5
|
+
import com.cmcc_rn_module.view.CompassManager;
|
|
5
6
|
import com.facebook.react.ReactPackage;
|
|
6
7
|
import com.facebook.react.bridge.NativeModule;
|
|
7
8
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
@@ -22,6 +23,8 @@ public class BeiDouBluetoothPackage implements ReactPackage {
|
|
|
22
23
|
@NonNull
|
|
23
24
|
@Override
|
|
24
25
|
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
|
|
25
|
-
return
|
|
26
|
+
return Arrays.<ViewManager>asList(
|
|
27
|
+
new CompassManager(reactContext)
|
|
28
|
+
);
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
package com.cmcc_rn_module;
|
|
2
|
+
|
|
3
|
+
import android.content.ContentValues;
|
|
4
|
+
import android.content.Context;
|
|
5
|
+
import android.database.Cursor;
|
|
6
|
+
import android.database.sqlite.SQLiteDatabase;
|
|
7
|
+
import android.database.sqlite.SQLiteOpenHelper;
|
|
8
|
+
import android.util.Log;
|
|
9
|
+
|
|
10
|
+
import java.util.ArrayList;
|
|
11
|
+
import java.util.HashMap;
|
|
12
|
+
import java.util.List;
|
|
13
|
+
import java.util.Map;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 聊天数据库管理器
|
|
17
|
+
* 用于管理北斗蓝牙短信的存储和查询
|
|
18
|
+
*/
|
|
19
|
+
public class ChatDBManager extends SQLiteOpenHelper {
|
|
20
|
+
private static final String TAG = "ChatDBManager";
|
|
21
|
+
private static final String DATABASE_NAME = "beidou_chat.db";
|
|
22
|
+
private static final int DATABASE_VERSION = 1;
|
|
23
|
+
|
|
24
|
+
// 消息表
|
|
25
|
+
private static final String TABLE_MESSAGES = "messages";
|
|
26
|
+
private static final String COLUMN_ID = "id";
|
|
27
|
+
private static final String COLUMN_PHONE_NUMBER = "phone_number";
|
|
28
|
+
private static final String COLUMN_CONTENT = "content";
|
|
29
|
+
private static final String COLUMN_TIMESTAMP = "timestamp";
|
|
30
|
+
private static final String COLUMN_IS_SEND = "is_send";
|
|
31
|
+
|
|
32
|
+
// 会话表
|
|
33
|
+
private static final String TABLE_CONVERSATIONS = "conversations";
|
|
34
|
+
private static final String COLUMN_LAST_MESSAGE = "last_message";
|
|
35
|
+
|
|
36
|
+
// 创建消息表的SQL语句
|
|
37
|
+
private static final String CREATE_MESSAGES_TABLE =
|
|
38
|
+
"CREATE TABLE " + TABLE_MESSAGES + " (" +
|
|
39
|
+
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
|
40
|
+
COLUMN_PHONE_NUMBER + " TEXT NOT NULL, " +
|
|
41
|
+
COLUMN_CONTENT + " TEXT NOT NULL, " +
|
|
42
|
+
COLUMN_TIMESTAMP + " INTEGER NOT NULL, " +
|
|
43
|
+
COLUMN_IS_SEND + " INTEGER NOT NULL DEFAULT 0" +
|
|
44
|
+
")";
|
|
45
|
+
|
|
46
|
+
// 创建会话表的SQL语句
|
|
47
|
+
private static final String CREATE_CONVERSATIONS_TABLE =
|
|
48
|
+
"CREATE TABLE " + TABLE_CONVERSATIONS + " (" +
|
|
49
|
+
COLUMN_PHONE_NUMBER + " TEXT PRIMARY KEY, " +
|
|
50
|
+
COLUMN_LAST_MESSAGE + " TEXT NOT NULL, " +
|
|
51
|
+
COLUMN_TIMESTAMP + " INTEGER NOT NULL, " +
|
|
52
|
+
COLUMN_IS_SEND + " INTEGER NOT NULL DEFAULT 0" +
|
|
53
|
+
")";
|
|
54
|
+
|
|
55
|
+
public ChatDBManager(Context context) {
|
|
56
|
+
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Override
|
|
60
|
+
public void onCreate(SQLiteDatabase db) {
|
|
61
|
+
db.execSQL(CREATE_MESSAGES_TABLE);
|
|
62
|
+
db.execSQL(CREATE_CONVERSATIONS_TABLE);
|
|
63
|
+
Log.d(TAG, "数据库创建成功");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@Override
|
|
67
|
+
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
|
68
|
+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);
|
|
69
|
+
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONVERSATIONS);
|
|
70
|
+
onCreate(db);
|
|
71
|
+
Log.d(TAG, "数据库升级完成");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 插入消息
|
|
76
|
+
* @param content 消息内容
|
|
77
|
+
* @param phoneNumber 手机号
|
|
78
|
+
* @param isSend 是否为发送的消息
|
|
79
|
+
* @param timestamp 时间戳
|
|
80
|
+
* @return 插入成功返回true
|
|
81
|
+
*/
|
|
82
|
+
public boolean insertMessage(String content, String phoneNumber, boolean isSend, long timestamp) {
|
|
83
|
+
SQLiteDatabase db = null;
|
|
84
|
+
try {
|
|
85
|
+
db = this.getWritableDatabase();
|
|
86
|
+
db.beginTransaction();
|
|
87
|
+
|
|
88
|
+
// 插入消息记录
|
|
89
|
+
ContentValues messageValues = new ContentValues();
|
|
90
|
+
messageValues.put(COLUMN_PHONE_NUMBER, phoneNumber);
|
|
91
|
+
messageValues.put(COLUMN_CONTENT, content);
|
|
92
|
+
messageValues.put(COLUMN_TIMESTAMP, timestamp);
|
|
93
|
+
messageValues.put(COLUMN_IS_SEND, isSend ? 1 : 0);
|
|
94
|
+
|
|
95
|
+
long messageResult = db.insert(TABLE_MESSAGES, null, messageValues);
|
|
96
|
+
|
|
97
|
+
if (messageResult != -1) {
|
|
98
|
+
// 更新对话表(原子操作:如果不存在则插入,存在则更新)
|
|
99
|
+
ContentValues conversationValues = new ContentValues();
|
|
100
|
+
conversationValues.put(COLUMN_PHONE_NUMBER, phoneNumber);
|
|
101
|
+
conversationValues.put(COLUMN_LAST_MESSAGE, content);
|
|
102
|
+
conversationValues.put(COLUMN_TIMESTAMP, timestamp);
|
|
103
|
+
conversationValues.put(COLUMN_IS_SEND, isSend ? 1 : 0);
|
|
104
|
+
|
|
105
|
+
long conversationResult = db.insertWithOnConflict(TABLE_CONVERSATIONS, null,
|
|
106
|
+
conversationValues, SQLiteDatabase.CONFLICT_REPLACE);
|
|
107
|
+
|
|
108
|
+
if (conversationResult != -1) {
|
|
109
|
+
db.setTransactionSuccessful();
|
|
110
|
+
Log.d(TAG, String.format("插入消息成功 - 手机号: %s, 内容: %s, 是否发送: %b",
|
|
111
|
+
phoneNumber, content, isSend));
|
|
112
|
+
return true;
|
|
113
|
+
} else {
|
|
114
|
+
Log.e(TAG, "更新会话表失败");
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
Log.e(TAG, "插入消息表失败");
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
} catch (Exception e) {
|
|
122
|
+
Log.e(TAG, "插入消息失败: " + e.getMessage(), e);
|
|
123
|
+
return false;
|
|
124
|
+
} finally {
|
|
125
|
+
if (db != null) {
|
|
126
|
+
try {
|
|
127
|
+
db.endTransaction();
|
|
128
|
+
} catch (Exception e) {
|
|
129
|
+
Log.e(TAG, "结束事务失败: " + e.getMessage());
|
|
130
|
+
}
|
|
131
|
+
if (db.isOpen()) {
|
|
132
|
+
db.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 获取所有会话列表
|
|
140
|
+
* @return 会话列表
|
|
141
|
+
*/
|
|
142
|
+
public List<Map<String, Object>> getAllConversations() {
|
|
143
|
+
List<Map<String, Object>> conversations = new ArrayList<>();
|
|
144
|
+
SQLiteDatabase db = null;
|
|
145
|
+
Cursor cursor = null;
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
db = this.getReadableDatabase();
|
|
149
|
+
|
|
150
|
+
// 直接从会话表查询,按时间戳倒序排列
|
|
151
|
+
String query = "SELECT * FROM " + TABLE_CONVERSATIONS + " ORDER BY " + COLUMN_TIMESTAMP + " DESC";
|
|
152
|
+
cursor = db.rawQuery(query, null);
|
|
153
|
+
|
|
154
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
155
|
+
do {
|
|
156
|
+
Map<String, Object> conversation = new HashMap<>();
|
|
157
|
+
conversation.put("phone", cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PHONE_NUMBER)));
|
|
158
|
+
conversation.put("lastMsg", cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_LAST_MESSAGE)));
|
|
159
|
+
conversation.put("timestamp", cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TIMESTAMP)));
|
|
160
|
+
conversation.put("is_send", cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_SEND)) == 1);
|
|
161
|
+
conversations.add(conversation);
|
|
162
|
+
} while (cursor.moveToNext());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
Log.d(TAG, "获取会话列表成功,共 " + conversations.size() + " 个会话");
|
|
166
|
+
|
|
167
|
+
} catch (Exception e) {
|
|
168
|
+
Log.e(TAG, "获取会话列表失败: " + e.getMessage(), e);
|
|
169
|
+
} finally {
|
|
170
|
+
if (cursor != null) {
|
|
171
|
+
cursor.close();
|
|
172
|
+
}
|
|
173
|
+
if (db != null && db.isOpen()) {
|
|
174
|
+
db.close();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return conversations;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 获取指定手机号的消息列表
|
|
183
|
+
* @param phoneNumber 手机号
|
|
184
|
+
* @param page 页码(从0开始)
|
|
185
|
+
* @param pageSize 每页大小
|
|
186
|
+
* @return 消息列表
|
|
187
|
+
*/
|
|
188
|
+
public List<Map<String, Object>> getMessagesForPhone(String phoneNumber, int page, int pageSize) {
|
|
189
|
+
List<Map<String, Object>> messages = new ArrayList<>();
|
|
190
|
+
SQLiteDatabase db = null;
|
|
191
|
+
Cursor cursor = null;
|
|
192
|
+
|
|
193
|
+
try {
|
|
194
|
+
db = this.getReadableDatabase();
|
|
195
|
+
|
|
196
|
+
// 注意:iOS版本中查询的表是 uploadForm,但这里应该查询 messages 表
|
|
197
|
+
// 修正查询语句,使用正确的表名和字段名
|
|
198
|
+
String query = String.format("SELECT * FROM %s WHERE %s = ? ORDER BY %s DESC LIMIT %d OFFSET %d",
|
|
199
|
+
TABLE_MESSAGES, COLUMN_PHONE_NUMBER, COLUMN_TIMESTAMP, pageSize, page * pageSize);
|
|
200
|
+
|
|
201
|
+
cursor = db.rawQuery(query, new String[]{phoneNumber});
|
|
202
|
+
|
|
203
|
+
if (cursor != null && cursor.moveToFirst()) {
|
|
204
|
+
do {
|
|
205
|
+
Map<String, Object> message = new HashMap<>();
|
|
206
|
+
message.put("content", cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_CONTENT)));
|
|
207
|
+
message.put("is_send", cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_IS_SEND)) == 1);
|
|
208
|
+
message.put("timestamp", cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TIMESTAMP)));
|
|
209
|
+
messages.add(message);
|
|
210
|
+
} while (cursor.moveToNext());
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
Log.d(TAG, String.format("获取消息详情成功 - 手机号: %s, 页码: %d, 页大小: %d, 消息数: %d",
|
|
214
|
+
phoneNumber, page, pageSize, messages.size()));
|
|
215
|
+
|
|
216
|
+
} catch (Exception e) {
|
|
217
|
+
Log.e(TAG, "获取消息详情失败: " + e.getMessage(), e);
|
|
218
|
+
} finally {
|
|
219
|
+
if (cursor != null) {
|
|
220
|
+
cursor.close();
|
|
221
|
+
}
|
|
222
|
+
if (db != null && db.isOpen()) {
|
|
223
|
+
db.close();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return messages;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 删除指定手机号的所有消息
|
|
232
|
+
* @param phoneNumber 手机号
|
|
233
|
+
* @return 删除成功返回true
|
|
234
|
+
*/
|
|
235
|
+
public boolean deleteMessagesForPhone(String phoneNumber) {
|
|
236
|
+
SQLiteDatabase db = null;
|
|
237
|
+
try {
|
|
238
|
+
db = this.getWritableDatabase();
|
|
239
|
+
db.beginTransaction();
|
|
240
|
+
|
|
241
|
+
// 删除消息记录
|
|
242
|
+
int deletedMessages = db.delete(TABLE_MESSAGES, COLUMN_PHONE_NUMBER + " = ?", new String[]{phoneNumber});
|
|
243
|
+
|
|
244
|
+
// 删除会话记录
|
|
245
|
+
int deletedConversations = db.delete(TABLE_CONVERSATIONS, COLUMN_PHONE_NUMBER + " = ?", new String[]{phoneNumber});
|
|
246
|
+
|
|
247
|
+
db.setTransactionSuccessful();
|
|
248
|
+
|
|
249
|
+
Log.d(TAG, String.format("删除消息 - 手机号: %s, 删除消息数: %d, 删除会话数: %d",
|
|
250
|
+
phoneNumber, deletedMessages, deletedConversations));
|
|
251
|
+
return deletedMessages > 0;
|
|
252
|
+
} catch (Exception e) {
|
|
253
|
+
Log.e(TAG, "删除消息失败: " + e.getMessage(), e);
|
|
254
|
+
return false;
|
|
255
|
+
} finally {
|
|
256
|
+
if (db != null) {
|
|
257
|
+
try {
|
|
258
|
+
db.endTransaction();
|
|
259
|
+
} catch (Exception e) {
|
|
260
|
+
Log.e(TAG, "结束事务失败: " + e.getMessage());
|
|
261
|
+
}
|
|
262
|
+
if (db.isOpen()) {
|
|
263
|
+
db.close();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 清空所有消息
|
|
271
|
+
* @return 清空成功返回true
|
|
272
|
+
*/
|
|
273
|
+
public boolean clearAllMessages() {
|
|
274
|
+
SQLiteDatabase db = null;
|
|
275
|
+
try {
|
|
276
|
+
db = this.getWritableDatabase();
|
|
277
|
+
db.beginTransaction();
|
|
278
|
+
|
|
279
|
+
// 清空消息表
|
|
280
|
+
int deletedMessages = db.delete(TABLE_MESSAGES, null, null);
|
|
281
|
+
|
|
282
|
+
// 清空会话表
|
|
283
|
+
int deletedConversations = db.delete(TABLE_CONVERSATIONS, null, null);
|
|
284
|
+
|
|
285
|
+
db.setTransactionSuccessful();
|
|
286
|
+
|
|
287
|
+
Log.d(TAG, String.format("清空所有消息,删除消息数: %d, 删除会话数: %d", deletedMessages, deletedConversations));
|
|
288
|
+
return true;
|
|
289
|
+
} catch (Exception e) {
|
|
290
|
+
Log.e(TAG, "清空消息失败: " + e.getMessage(), e);
|
|
291
|
+
return false;
|
|
292
|
+
} finally {
|
|
293
|
+
if (db != null) {
|
|
294
|
+
try {
|
|
295
|
+
db.endTransaction();
|
|
296
|
+
} catch (Exception e) {
|
|
297
|
+
Log.e(TAG, "结束事务失败: " + e.getMessage());
|
|
298
|
+
}
|
|
299
|
+
if (db.isOpen()) {
|
|
300
|
+
db.close();
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
@@ -12,7 +12,7 @@ import java.util.List;
|
|
|
12
12
|
import java.util.Map;
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
|
|
15
|
+
* @author hbb
|
|
16
16
|
* @Description: 基于Android原生org.json封装的jsonUtil
|
|
17
17
|
* @date 2019年1月1日
|
|
18
18
|
*/
|
|
@@ -21,21 +21,127 @@ public class JsonUtil {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
|
|
24
|
+
* 对象转成json
|
|
25
25
|
* 注意:Android原生JSON工具需要手动构建对象,不能直接序列化POJO
|
|
26
26
|
*/
|
|
27
27
|
public static String objectToJson(Object object) throws JSONException {
|
|
28
28
|
if (object instanceof Map) {
|
|
29
29
|
return new JSONObject((Map) object).toString();
|
|
30
30
|
} else if (object instanceof List) {
|
|
31
|
+
// 检查是否是BlueDeviceInfo对象列表
|
|
32
|
+
List<?> list = (List<?>) object;
|
|
33
|
+
if (!list.isEmpty()) {
|
|
34
|
+
Object firstItem = list.get(0);
|
|
35
|
+
if (firstItem != null) {
|
|
36
|
+
String className = firstItem.getClass().getSimpleName();
|
|
37
|
+
if (className.contains("BlueDeviceInfo") || className.contains("DeviceInfo") ||
|
|
38
|
+
className.contains("BlueDeviceInfoEntity")) {
|
|
39
|
+
return convertBlueDeviceInfoListToJson(list);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
31
43
|
return new JSONArray((List) object).toString();
|
|
44
|
+
} else if (object instanceof java.util.Collection) {
|
|
45
|
+
// 处理 Collection 类型(如 Map.values())
|
|
46
|
+
List<Object> list = new ArrayList<>((java.util.Collection<?>) object);
|
|
47
|
+
return objectToJson(list);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 特殊处理:如果是BlueDeviceInfo相关的对象,尝试转换为Map
|
|
51
|
+
if (object != null) {
|
|
52
|
+
String className = object.getClass().getSimpleName();
|
|
53
|
+
if (className.contains("BlueDeviceInfo") || className.contains("DeviceInfo") ||
|
|
54
|
+
className.contains("BlueDeviceInfoEntity")) {
|
|
55
|
+
Map<String, Object> deviceMap = convertBlueDeviceInfoToMap(object);
|
|
56
|
+
return new JSONObject(deviceMap).toString();
|
|
57
|
+
}
|
|
32
58
|
}
|
|
59
|
+
|
|
33
60
|
// 对于自定义对象,需要先转换为Map
|
|
34
61
|
throw new UnsupportedOperationException("Android原生JSON工具不支持直接序列化POJO,请先将对象转换为Map");
|
|
35
62
|
}
|
|
36
63
|
|
|
37
64
|
/**
|
|
38
|
-
|
|
65
|
+
* 将BlueDeviceInfo对象转换为Map
|
|
66
|
+
* 使用反射获取对象属性
|
|
67
|
+
*/
|
|
68
|
+
private static Map<String, Object> convertBlueDeviceInfoToMap(Object deviceInfo) {
|
|
69
|
+
Map<String, Object> map = new HashMap<>();
|
|
70
|
+
try {
|
|
71
|
+
Class<?> clazz = deviceInfo.getClass();
|
|
72
|
+
|
|
73
|
+
// 尝试获取常见的蓝牙设备信息字段
|
|
74
|
+
try {
|
|
75
|
+
java.lang.reflect.Field macAddressField = clazz.getDeclaredField("macAddress");
|
|
76
|
+
macAddressField.setAccessible(true);
|
|
77
|
+
Object macAddress = macAddressField.get(deviceInfo);
|
|
78
|
+
map.put("macAddress", macAddress != null ? macAddress.toString() : "");
|
|
79
|
+
} catch (Exception e) {
|
|
80
|
+
map.put("macAddress", "");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
java.lang.reflect.Field deviceNameField = clazz.getDeclaredField("deviceName");
|
|
85
|
+
deviceNameField.setAccessible(true);
|
|
86
|
+
Object deviceName = deviceNameField.get(deviceInfo);
|
|
87
|
+
map.put("deviceName", deviceName != null ? deviceName.toString() : "");
|
|
88
|
+
} catch (Exception e) {
|
|
89
|
+
map.put("deviceName", "");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
java.lang.reflect.Field deviceNoField = clazz.getDeclaredField("deviceNo");
|
|
94
|
+
deviceNoField.setAccessible(true);
|
|
95
|
+
Object deviceNo = deviceNoField.get(deviceInfo);
|
|
96
|
+
map.put("deviceNo", deviceNo != null ? deviceNo.toString() : "");
|
|
97
|
+
} catch (Exception e) {
|
|
98
|
+
map.put("deviceNo", "");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// 如果上述字段都获取失败,尝试获取所有字段
|
|
102
|
+
if (map.isEmpty() || (map.get("macAddress").equals("") && map.get("deviceName").equals("")
|
|
103
|
+
&& map.get("deviceNo").equals(""))) {
|
|
104
|
+
java.lang.reflect.Field[] fields = clazz.getDeclaredFields();
|
|
105
|
+
for (java.lang.reflect.Field field : fields) {
|
|
106
|
+
try {
|
|
107
|
+
field.setAccessible(true);
|
|
108
|
+
Object value = field.get(deviceInfo);
|
|
109
|
+
map.put(field.getName(), value != null ? value.toString() : "");
|
|
110
|
+
} catch (Exception e) {
|
|
111
|
+
// 忽略无法访问的字段
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
} catch (Exception e) {
|
|
117
|
+
// 如果反射失败,返回基本信息
|
|
118
|
+
map.put("macAddress", "");
|
|
119
|
+
map.put("deviceName", "Unknown Device");
|
|
120
|
+
map.put("deviceNo", "");
|
|
121
|
+
map.put("error", "反射获取设备信息失败: " + e.getMessage());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return map;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 将BlueDeviceInfo对象列表转换为JSON字符串
|
|
129
|
+
*/
|
|
130
|
+
private static String convertBlueDeviceInfoListToJson(List<?> deviceList) throws JSONException {
|
|
131
|
+
JSONArray jsonArray = new JSONArray();
|
|
132
|
+
|
|
133
|
+
for (Object device : deviceList) {
|
|
134
|
+
if (device != null) {
|
|
135
|
+
Map<String, Object> deviceMap = convertBlueDeviceInfoToMap(device);
|
|
136
|
+
jsonArray.put(new JSONObject(deviceMap));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return jsonArray.toString();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Json转成对象
|
|
39
145
|
*/
|
|
40
146
|
public static <T> T jsonToBean(String json, Class<T> cls) throws JSONException {
|
|
41
147
|
JSONObject jsonObject = new JSONObject(json);
|
|
@@ -43,7 +149,7 @@ public class JsonUtil {
|
|
|
43
149
|
}
|
|
44
150
|
|
|
45
151
|
/**
|
|
46
|
-
|
|
152
|
+
* json转成list<T>
|
|
47
153
|
*/
|
|
48
154
|
public static <T> ArrayList<T> jsonToList(String json, Class<T> cls) throws JSONException {
|
|
49
155
|
JSONArray jsonArray = new JSONArray(json);
|
|
@@ -56,7 +162,7 @@ public class JsonUtil {
|
|
|
56
162
|
}
|
|
57
163
|
|
|
58
164
|
/**
|
|
59
|
-
|
|
165
|
+
* json转成list中有map的
|
|
60
166
|
*/
|
|
61
167
|
public static <T> List<Map<String, T>> jsonToListMaps(String json) throws JSONException {
|
|
62
168
|
JSONArray jsonArray = new JSONArray(json);
|
|
@@ -68,14 +174,14 @@ public class JsonUtil {
|
|
|
68
174
|
}
|
|
69
175
|
|
|
70
176
|
/**
|
|
71
|
-
|
|
177
|
+
* json转成map
|
|
72
178
|
*/
|
|
73
179
|
public static <T> Map<String, T> jsonToMap(String json) throws JSONException {
|
|
74
180
|
return jsonObjectToMap(new JSONObject(json));
|
|
75
181
|
}
|
|
76
182
|
|
|
77
183
|
/**
|
|
78
|
-
|
|
184
|
+
* 解析MQTT消息
|
|
79
185
|
*/
|
|
80
186
|
public static <T> T jsonWithTypeToObject(String json, Type type) throws JSONException {
|
|
81
187
|
// Android原生JSON工具没有直接的Type支持,需要根据实际类型处理
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
package com.cmcc_rn_module.view;
|
|
2
|
+
|
|
3
|
+
import android.view.View;
|
|
4
|
+
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
|
|
7
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
8
|
+
import com.facebook.react.bridge.ReactContext;
|
|
9
|
+
import com.facebook.react.uimanager.SimpleViewManager;
|
|
10
|
+
import com.facebook.react.uimanager.ThemedReactContext;
|
|
11
|
+
import com.facebook.react.uimanager.annotations.ReactProp;
|
|
12
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
13
|
+
|
|
14
|
+
public class CompassManager extends SimpleViewManager<CompassView> {
|
|
15
|
+
public static final String REACT_CLASS = "CompassView";
|
|
16
|
+
|
|
17
|
+
private CompassView compassView;
|
|
18
|
+
|
|
19
|
+
public CompassManager(ReactApplicationContext reactContext) {}
|
|
20
|
+
|
|
21
|
+
@NonNull
|
|
22
|
+
@Override
|
|
23
|
+
public String getName() {
|
|
24
|
+
return REACT_CLASS;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
@NonNull
|
|
28
|
+
@Override
|
|
29
|
+
protected CompassView createViewInstance(@NonNull ThemedReactContext reactContext) {
|
|
30
|
+
compassView = new CompassView(reactContext, null);
|
|
31
|
+
return compassView;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@Override
|
|
35
|
+
public void onDropViewInstance(@NonNull CompassView view) {
|
|
36
|
+
super.onDropViewInstance(view);
|
|
37
|
+
if (compassView != null) {
|
|
38
|
+
compassView.stop();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|