Skip to content

Commit

Permalink
🎉 Release 1.3,背景运行(BETA),调整CH34X缓冲区(BETA),通讯期间中文编码问题修复(BETA)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hny0305Lin committed Jul 12, 2024
1 parent 71babff commit 8479fe1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/* 重命名来自: cn.wch.ch34xuartdriver.b */
Expand Down Expand Up @@ -39,7 +40,7 @@ public Ch34ReadThread(CH34xUARTDriver cH34xUARTDriver, UsbEndpoint usbEndpoint,
cH34xUARTDriver.getUsbRequests()[i] = new UsbRequest();
cH34xUARTDriver.getUsbRequests()[i].initialize(this.usbDeviceConnection, this.usbEndpoint);

cH34xUARTDriver.getByteBuffers()[i] = ByteBuffer.allocate(this.ch34xUARTDriver._32);
cH34xUARTDriver.getByteBuffers()[i] = ByteBuffer.allocate(this.ch34xUARTDriver._32 * 4); //缓冲区增加Test
}
this.setPriority(10);
}
Expand Down Expand Up @@ -79,6 +80,7 @@ public void run() {
this.ch34xUARTDriver.getUsbRequests()[i].queue(this.ch34xUARTDriver.getByteBuffers()[i], this.ch34xUARTDriver._32);
}
int count = 0;

root:
while (!Thread.interrupted()) {
do {
Expand All @@ -101,12 +103,25 @@ public void run() {
byte[] temp = this.ch34xUARTDriver.getByteBuffers()[i].array();
int tempLength = this.ch34xUARTDriver.getByteBuffers()[i].position();
if (tempLength > 0) {

try {
this.ch34xUARTDriver.getSemaphore().acquire();

byte[] bytes = Arrays.copyOf(temp, tempLength);

// 记录接收到的字节用于调试
Log.d(TAG, "Received bytes: " + Arrays.toString(bytes));

// 使用 UTF-8 编码将字节数组转换为字符串
String receivedData = new String(bytes, 0, tempLength, StandardCharsets.UTF_8);
Log.d(TAG, "Received data: " + receivedData);

// 如果有字节提取监听器,则调用其 value 方法
if (bytesExtract != null) bytesExtract.value(bytes);

} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.ch34xUARTDriver.getSemaphore().release();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
<service
android:name=".ChatService.MyForegroundService"
android:enabled="true"
android:exported="false"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://github.com/Hny0305Lin/LICENSE/blob/main/LICENSE */
package com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatService;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.haohanyh.linmengjia.nearlink.nlchat.fun.R;

public class MyForegroundService extends Service {
private static final String TAG = "MyForegroundService & NLChat";
private static final String CHANNEL_ID = "ForegroundServiceChannel";

@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "服务已创建");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "服务已启动");

createNotificationChannel();
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("NLChat保活活动")
.setContentText("保持软件后台运行,已启用,请注意电池消耗,消耗过快请关闭本程序。")
.setSmallIcon(R.drawable.app_icon_new)
.build();
startForeground(1, notification);

// Your background task code here

return START_STICKY;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "服务已销毁");
}

@Override
public IBinder onBind(Intent intent) {
return null; // We don't provide binding, so return null
}

@SuppressLint("ObsoleteSdkInt")
private void createNotificationChannel() {
Log.d(TAG, "背景服务通知已创建,可查看Android通知栏");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"背景服务通知",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
if (manager != null) {
manager.createNotificationChannel(serviceChannel);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUIBackgroundUtils;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUIUpdater;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatCore.ChatUtils;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.ChatService.MyForegroundService;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.Premission.NearLinkChatGetSomePermission;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R.array;
import com.haohanyh.linmengjia.nearlink.nlchat.fun.R.color;
Expand Down Expand Up @@ -200,6 +201,9 @@ private void Init() {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);intent.setData(Uri.parse("package:" + this.getPackageName()));startActivityForResult(intent, 1024);
}
}
//Service常驻
Intent serviceIntent = new Intent(this, MyForegroundService.class);
startForegroundService(serviceIntent);
//创建CH34x设备对象
MainAPP.CH34X = new CH34xUARTDriver(
(UsbManager) getSystemService(Context.USB_SERVICE), this,
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>
<string name="app_name">NLChat</string>
<string name="app_package">com.haohanyh.linmengjia.nearlink.nlchat.fun</string>
<string name="app_version">1.3.200.2024.0711</string>
<string name="app_version">1.3.210.2024.0713</string>

<string name="appwarn">NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。</string>
<string name="thanks3q">友情感谢</string>
Expand Down

0 comments on commit 8479fe1

Please sign in to comment.