java—向eclipse服务器发送数据时,如何同时发送多个信标的数据?

axzmvihb  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(155)

我发现向服务器发送信标数据并将其存储在数据库中有问题。它接收4个信标数据到数据库中(在sql语句中一次一个数据)。在这里,一段数据是beacon.getbluetoothname和beacon.getrssi以及beacon.getdistance的捆绑包。我用的是安卓信标库。
我最初的目的是更快地向服务器发送信标数据。我的主要问题是向数据库发送重复数据。我想从四个信标均匀地发送数据。如果将数据放入sndmsg()中,它将发送到服务器。服务器是eclipse服务器,数据库是mysql
我知道冗余数据来自于传输速度慢。我想提高速度。是图书馆的极限还是灯塔的极限(冥王星的)有什么办法可以解决这个问题吗?
在此处输入图像描述

public class MainActivity extends AppCompatActivity implements          
    BeaconConsumer
{
    private String TAG = MainActivity.class.getSimpleName();
    private BeaconManager beaconManager;
    private List<Beacon> beaconList = new ArrayList<>();
    TextView textView ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SystemRequirementsChecker.checkWithDefaultDialogs(this);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    textView = (TextView)findViewById(R.id.tvId);
    beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind(this);
    mHandler = new Handler();
    et =  findViewById(R.id.EditText01);
    Button btn =  findViewById(R.id.Button01);
    final TextView tv =  findViewById(R.id.TextView01);
    msgTV = findViewById(R.id.chatTV);
    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (et.getText().toString() != null || !et.getText().toString().equals("")) {
                ConnectThread th =new ConnectThread();
                th.start();
            }
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}

@Override
public void onBeaconServiceConnect() {
    beaconManager.addMonitorNotifier(new MonitorNotifier() {
        @Override
        public void didEnterRegion(Region region) {
        }
        @Override
        public void didExitRegion(Region region) {
            Log.d(TAG,"Exit : " + region.getId1());
        }
        @Override
        public void didDetermineStateForRegion(int i, Region region) {
        }
    });

    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                beaconList.clear();
                for (Beacon beacon : beacons) {
                    beaconList.add(beacon);
                }
            }
        }
    });
    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    }
    catch (RemoteException e) {

    }
}

public void OnButtonClicked(View view){
    handler.sendEmptyMessage(0);
}
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            for(Beacon beacon : beaconList)
            {

                textView.setText("ID : " + beacon.getBluetoothName() + " / " + "Distance : "
                        + beacon.getDistance()+ "m\n" + " rssi:"
                        + beacon.getRssi() + "txpower : " + beacon.getTxPower()  );

            }
            handler.sendEmptyMessageDelayed(0, 30);
        }
    };

private Handler mHandler;
Socket socket;
private String ip = "192.168.0.3"; // IP 주소
private int port = 7000; // PORT번호
EditText et;
TextView msgTV;

@Override
protected void onStop() {
    super.onStop();
    try {
        socket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class ConnectThread extends Thread{
    @SuppressLint("DefaultLocale")
    public void run(){
        try{
            while (true) {
                InetAddress serverAddr = InetAddress.getByName(ip);
                socket = new Socket(serverAddr, port);
                //입력 메시지
                String sndMsg = null;
                //String ClientID = et.getText().toString();
                for (Beacon beacon : beaconList) {
                    String rssi = Integer.toString(beacon.getRssi());
                    String dis1 = String.format("%.3f", beacon.getDistance());
                    double dis = 0.0;
                    double ratio = Double.valueOf(rssi) * (1.0) / beacon.getTxPower();

                    if (ratio < 1.0) {
                        dis = Math.pow(ratio, 10);
                    } else {
                        dis = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
                    }

                    //ClientID = et.getText().toString();
                    sndMsg = (et.getText().toString() + "," + beacon.getBluetoothName() + "," + rssi + "," + dis);

                    //데이터 송신
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                    out.println(sndMsg);

                    //데이터 수신
                    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String read = input.readLine();
                    //화면 출력
                    mHandler.post(new msgUpdate(read));
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.d("=============", read);
                }

                socket.close();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

// 받은 메시지 출력하는 클래스
class msgUpdate implements Runnable {
    private String msg;
    public msgUpdate(String str) {
        this.msg = str;
    }
    public void run() {
        msgTV.setText(msg);
    }
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题