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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android BLE 寫入特性鎖定 onCharacteristicWrite/

Android BLE 寫入特性鎖定 onCharacteristicWrite/

大話西游666 2023-10-13 14:53:42
我有一個用于發(fā)送消息緩沖區(qū)的消息線程。每條消息都會排隊等待發(fā)送一次,一旦onCharacteristicWrite成功,該特征就會寫入下一條消息。特性也設置為WRITE_TYPE_NO_RESPONSE,因此特性寫入調(diào)用之間的消息緩沖隊列非??欤ù蠹s 0-7 毫秒)。主要問題:“堵塞”特征在大多數(shù)情況下,這效果很好。當存在大量消息時,似乎會出現(xiàn)此問題(可能會在消息較少時發(fā)生,但在發(fā)送大量消息時更明顯)。發(fā)生的情況是writeCharacteristicwill 被調(diào)用,并且該特性似乎鎖定,因為onCharacteristicChanged不再讀取任何新數(shù)據(jù),并且無法訪問onCharacteristicWrite.我注意到的其他事情:每次添加 5-10ms 的睡眠延遲characteristicWrite似乎有幫助,但我不明白為什么藍牙 GATT 對象在成功返回時需要延遲onCharacteristicWrite。onConnectionStateChange有時我會收到狀態(tài) 8、設備超出范圍的回調(diào)。但這并不總是發(fā)生。有時characteristicWrite返回 false;然而,它也可以在進入上述“堵塞特征”狀態(tài)之前返回 true消息線程代碼:    private boolean stopMessageThread = false;    private boolean characteristicWriteSuccessful = true;    private ArrayList<byte[]> messageQueue = new ArrayList<byte[]>();    private Thread messageThread =  new Thread( new Runnable() {        private long lastTime = 0;        private int count = 0;        @Override        public void run() {            while (!Thread.currentThread().isInterrupted() && !stopMessageThread) {                if(messageQueue.size() != 0 && characteristicWriteSuccessful) {                    Log.i(TAG, ""+(System.currentTimeMillis()-lastTime));                    Log.i(TAG, "Queue count: "+messageQueue.size());                    characteristicWriteSuccessful = false;                    byte[] message = messageQueue.remove(0);                    customCharacteristic.setValue(message);                    boolean status = bluetoothGatt.writeCharacteristic(customCharacteristic);                    Log.i(TAG, "write characteristic status "+status);                    lastTime = System.currentTimeMillis();                    //sleep(10); // this kinda helps but can still throw the error                }            }        }    });
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經(jīng)驗 獲得超11個贊

除了繁忙的等待(這會阻塞整個 CPU 并快速耗盡電池)之外,我看不到任何同步。有共享數(shù)據(jù)結(jié)構(gòu)(可能是stopMessageThread、characteristicWriteSuccessful和messageQueue)和多個訪問它們的線程。如果沒有同步,就會出現(xiàn)競爭條件,并且堵塞可能是其表現(xiàn)。


所以我建議采用更簡單的設計,特別是沒有用于發(fā)送消息的線程:


private ArrayList<byte[]> messageQueue = new ArrayList<byte[]>();

private boolean isSending = false;


void sendMessage(byte[] message) {

    synchronized (this) {

        if (isSending) {

            messageQueue.add(message);

            return;

        }

        isSending = true;

    }

    customCharacteristic.setValue(message);

    bluetoothGatt.writeCharacteristic(customCharacteristic);

}


public void onCharacteristicWrite (BluetoothGatt gatt, 

                BluetoothGattCharacteristic characteristic, 

                int status) {

    byte[] message;

    synchronized (this) {

        if (messageQueue.size() == 0) {

            isSending = false;

            return;

        }

        message = messageQueue.remove(0);

    }

    customCharacteristic.setValue(message);

    bluetoothGatt.writeCharacteristic(customCharacteristic); 

}

該解決方案的假設是writeCharacteristic不會阻塞并且速度很快。這是一個安全的假設,因為該方法在設計上是異步的:它有一個回調(diào),將在操作完成時調(diào)用。


因此回調(diào)onCharacteristicWrite用于發(fā)送緩沖區(qū)中的下一條消息。因此,對線程的需求消失了——相關(guān)的復雜性也消失了。


當從后臺線程調(diào)用回調(diào)時,仍然涉及多個線程。因此,對共享數(shù)據(jù)的訪問是同步的。


查看完整回答
反對 回復 2023-10-13
  • 1 回答
  • 0 關(guān)注
  • 646 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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