第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

通過藍(lán)牙將超聲波傳感器的數(shù)據(jù)從 Arduino 發(fā)送到 Android

通過藍(lán)牙將超聲波傳感器的數(shù)據(jù)從 Arduino 發(fā)送到 Android

楊魅力 2023-11-01 22:32:36
我正在寫我的文憑論文,但在使用藍(lán)牙與 Arduino -> Android 進(jìn)行通信時(shí)遇到問題。這是我的應(yīng)用程序: 我想要顯示到障礙物的距離的活動(dòng)在 TextView 中,我想將來自 Arduino 的數(shù)據(jù)與距離放在一起,我需要想法,我找不到東西,如何將數(shù)據(jù)從不同的傳感器發(fā)送到不同的視圖(例如前、后保險(xiǎn)杠、左和右)。這里有arduino代碼: #include <SoftwareSerial.h>// Mid-back sensor#define trigPinLeft 11 #define echoPinLeft 10// Right-back sensor (looking from back)#define trigPinRight 7#define echoPinRight 6SoftwareSerial btSerial = SoftwareSerial(0,1);void setup() {  // put your setup code here, to run once:  Serial.begin(115200);  btSerial.begin(115200);  // Mid-back sensor  pinMode(trigPinLeft, OUTPUT);  pinMode(echoPinLeft, INPUT);  // Right-back sensor   pinMode(trigPinRight, OUTPUT);  pinMode(echoPinRight, INPUT);}void loop() {  // put your main code here, to run repeatedly:  long durationLeft, distanceLeft;  digitalWrite(trigPinLeft, LOW);  delayMicroseconds(5);  digitalWrite(trigPinLeft, HIGH);  delayMicroseconds(5);  digitalWrite(trigPinLeft, LOW);  durationLeft = pulseIn(echoPinLeft, HIGH);  distanceLeft = (durationLeft *0.034 / 2);  if (distanceLeft>=400 || distanceLeft<=18){    Serial.println("Out of range");     btSerial.println("Out of range");  }  else{    Serial.print("BACK LEFT: ");    Serial.print(distanceLeft);    Serial.println(" cm");    btSerial.println(distanceLeft + "cm");  }  //delayMicroseconds(1);  long durationRight, distanceRight;  digitalWrite(trigPinRight, LOW);  delayMicroseconds(5);  digitalWrite(trigPinRight, HIGH);  delayMicroseconds(10);  digitalWrite(trigPinRight, LOW);  durationRight = pulseIn(echoPinRight, HIGH);  distanceRight = (durationRight *0.034 / 2);  if (distanceRight>=400 || distanceRight<=18){    Serial.println("Out of range");     btSerial.println("Out of range");  }  else{    Serial.print("BACK RIGHT: ");    Serial.print(distanceRight);    Serial.println(" cm");    btSerial.println(distanceRight + "cm");  }  delay(10000);}我編譯并運(yùn)行了應(yīng)用程序,但在看到閃屏后,我的手機(jī)出現(xiàn)白屏和延遲。我在 Android-Studio 中沒有錯(cuò)誤。感謝幫助。
查看完整描述

1 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

您顯然似乎有線程問題。

在您的代碼中HomeActivity,您已經(jīng)注釋掉了允許在手機(jī)上打開藍(lán)牙服務(wù)器的代碼,以便您的 Arduino 設(shè)備可以連接到它,并在UUIDRFCOM 模式下提供相關(guān)和其他相關(guān)參數(shù)。

然而,該代碼與網(wǎng)絡(luò)相關(guān)并且是阻塞的,因此永遠(yuǎn)不應(yīng)該在應(yīng)用程序UI 線程上執(zhí)行,該線程負(fù)責(zé)處理所有 UI 任務(wù),例如顯示視圖、監(jiān)視用戶交互(觸摸事件)等。

這就是您的手機(jī)顯示白屏且有延遲的原因。

因此,您絕對應(yīng)該在單獨(dú)的線程上執(zhí)行藍(lán)牙邏輯。

我建議使用以下類來處理所有與藍(lán)牙相關(guān)的邏輯。這非常簡單。

public class BluetoothHandler {


    private final Handler handler;

    private final BluetoothAdapter bluetoothAdapter;


    @Nullable

    private BluetoothServerSocket serverSocket;

    private BluetoothSocket bluetoothSocket;



    public BluetoothHandler(Context context) {

        final HandlerThread ht = new HandlerThread("Bluetooth Handler Thread", Thread.NORM_PRIORITY);

        ht.start(); // starting thread


        this.handler = new Handler(ht.getLooper());


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

            this.bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();

        } else {

            this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        }

    }



    public void startBluetoothServer() {

        // execute code in our background worker thread

        this.handler.post(new Runnable() {

            @Override

            public void run() {

                try {

                    serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord("name", "your UUID");

                    bluetoothSocket = serverSocket.accept(); // will wait as long as possible (no timeout) so there is blocking


                    // do your logic to retrieve in and out put streams to read / write data from / to your Arduino device


                }  catch (IOException ioe) {


                }

            } 

        });

    }



    @AnyThread

    public void writeData(byte[] data) {

        // remember, all network operation are to be executed in a background thread

        this.handler.post(new Runnable() {

            @Override

            public void run() {

                // write data in output stream     

            }

        });

    }



    @AnyThread

    public void readData(OnDataReadCallback callback) {

        // remember, all network operation are to be executed in a background thread

        this.handler.post(new Runnable() {

            @Override

            public void run() {

                // read data and notify via callback.

            }

        });

    }



    @AnyThread // should be call from your Activity onDestroy() to clear resources and avoid memory leaks.

    public void termainte() {

       try {

           if (serverSocket != null) {

               serverSocket.close();

           }


           if (bluetoothSocket != null) {

                bluetoothSocket.close();

           }

       } catch (IOException ioe) {


       }


        this.handler.getLooper().quit(); // will no longer be usable. Basically, this class instance is now trash.

    }



    public interface OnDataReadCallback {

        @WorkerThread // watch out if you need to update some view, user your Activity#runOnUiThread method !

        void onDataRead(byte[] data);

    }

}


查看完整回答
反對 回復(fù) 2023-11-01
  • 1 回答
  • 0 關(guān)注
  • 258 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號