修改 读取解析异常和东集分开读取
parent
730e2cfa3a
commit
4cad549e47
Binary file not shown.
@ -1,102 +0,0 @@
|
|||||||
package com.example.jinyu_rfid;
|
|
||||||
|
|
||||||
import androidx.annotation.RequiresApi;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
|
||||||
import android.bluetooth.BluetoothServerSocket;
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Build;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.os.Message;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import org.w3c.dom.Text;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class BlueToothServerActivity extends AppCompatActivity {
|
|
||||||
private BluetoothServerSocket serverSocket;
|
|
||||||
private BluetoothSocket socket;
|
|
||||||
@RequiresApi(api = Build.VERSION_CODES.S)
|
|
||||||
@SuppressLint("MissingInflatedId")
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setContentView(R.layout.activity_blue_tooth_server);
|
|
||||||
|
|
||||||
// 0000110a-0000-1000-8000-00805f9b34fb
|
|
||||||
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
||||||
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.BLUETOOTH_CONNECT}, 1);
|
|
||||||
}
|
|
||||||
// Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
|
|
||||||
// D:for (BluetoothDevice bluetoothDevice : devices) {
|
|
||||||
// Log.e("TAG", bluetoothDevice.getName() + ":" + bluetoothDevice.getUuids()[0].toString());
|
|
||||||
// /* // if (bluetoothDevice.getAddress().toString().equals("FC:0F:E7:B6:15:2F")) {
|
|
||||||
// if (bluetoothDevice.getName().equals("AUTOID UTouch")) {
|
|
||||||
// mmDevice = bluetoothDevice;
|
|
||||||
// Log.e("TAG", "连接成功");
|
|
||||||
// break D;
|
|
||||||
// }*/
|
|
||||||
// }
|
|
||||||
try {
|
|
||||||
// 创建一个BluetoothServerSocket来监听连接请求
|
|
||||||
serverSocket = bluetoothAdapter.listenUsingRfcommWithServiceRecord(
|
|
||||||
|
|
||||||
"abc",
|
|
||||||
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
|
|
||||||
// 无限循环以接受连接请求
|
|
||||||
while (true) {
|
|
||||||
socket = serverSocket.accept();
|
|
||||||
// 接收数据
|
|
||||||
receiveData(socket);
|
|
||||||
// 关闭socket
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void receiveData(BluetoothSocket socket) {
|
|
||||||
try {
|
|
||||||
InputStream inputStream = socket.getInputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int bytesRead;
|
|
||||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
||||||
// 处理接收到的数据
|
|
||||||
// (buffer, 0, bytesRead) 是接收到的数据
|
|
||||||
String receivedData = new String(buffer, 0, bytesRead);
|
|
||||||
Toast.makeText(this, receivedData, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void stopServer() {
|
|
||||||
try {
|
|
||||||
if (serverSocket != null) {
|
|
||||||
serverSocket.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
stopServer();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.example.jinyu_rfid;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.databinding.DataBindingUtil;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
|
import com.example.jinyu_rfid.base.BaseActivity;
|
||||||
|
import com.example.jinyu_rfid.been.IPConfigurationVM;
|
||||||
|
import com.example.jinyu_rfid.databinding.ActivityIpconfiguraBinding;
|
||||||
|
import com.example.jinyu_rfid.uitls.SharedPreferencesUtils;
|
||||||
|
|
||||||
|
public class IPConfiguraActivity extends BaseActivity {
|
||||||
|
private IPConfigurationVM vm;
|
||||||
|
private ActivityIpconfiguraBinding binding;
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
binding = DataBindingUtil. setContentView(this,R.layout.activity_ipconfigura);
|
||||||
|
|
||||||
|
binding.setTitle(getString(R.string.activity_name_configuration));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
if (url==null){
|
||||||
|
vm=new IPConfigurationVM();
|
||||||
|
}else {
|
||||||
|
vm=new IPConfigurationVM(url);
|
||||||
|
}
|
||||||
|
binding.setVm(vm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void urlSeve(View view) {
|
||||||
|
|
||||||
|
var ip = vm.getIp();
|
||||||
|
var port = vm.getPort();
|
||||||
|
if(ip==null||port==null||ip.isEmpty()|| port.isEmpty()) return;
|
||||||
|
SharedPreferencesUtils.putstring("url",vm.getUrl());
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
package com.example.jinyu_rfid.been;
|
||||||
|
|
||||||
|
import androidx.databinding.BaseObservable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author wanghao
|
||||||
|
* @date 2024/5/15 15:02
|
||||||
|
*/
|
||||||
|
public class IPConfigurationVM extends BaseObservable {
|
||||||
|
|
||||||
|
|
||||||
|
private String ip, port;
|
||||||
|
|
||||||
|
|
||||||
|
public String getIp() {
|
||||||
|
return ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIp(String ip) {
|
||||||
|
this.ip = ip;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPort() {
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPort(String port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IPConfigurationVM() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public IPConfigurationVM(String url) {
|
||||||
|
var split = url.split(":");
|
||||||
|
ip = split[0];
|
||||||
|
port = split[1];
|
||||||
|
notifyChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return ip + ":" + port;
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
tools:context=".BlueToothServerActivity">
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -0,0 +1,88 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<data>
|
||||||
|
<variable
|
||||||
|
name="title"
|
||||||
|
type="String" />
|
||||||
|
<variable
|
||||||
|
name="vm"
|
||||||
|
type="com.example.jinyu_rfid.been.IPConfigurationVM" />
|
||||||
|
</data>
|
||||||
|
|
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context=".IPConfiguraActivity">
|
||||||
|
|
||||||
|
<include
|
||||||
|
android:id="@+id/include"
|
||||||
|
layout="@layout/toolbar"
|
||||||
|
app:layout_constraintTop_toTopOf="parent"
|
||||||
|
app:title="@{title}"/>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/linearLayout"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="45dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginEnd="5dp"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/include">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
style="@style/text_name"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:text="@string/service_addr"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
style="@style/text_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@drawable/san_text"
|
||||||
|
android:text="@={vm.ip}" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="45dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginEnd="5dp"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
style="@style/text_name"
|
||||||
|
android:layout_width="120dp"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:text="@string/server_port" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
style="@style/text_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:background="@drawable/san_text"
|
||||||
|
android:text="@={vm.port}"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="55dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:layout_marginBottom="32dp"
|
||||||
|
android:onClick="urlSeve"
|
||||||
|
android:text="@string/configuration_save"
|
||||||
|
android:textSize="16sp"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent" />
|
||||||
|
|
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
|
</layout>
|
Loading…
Reference in New Issue