android BLE 40 setCharacteristicNotification接收不到資料

來源:互聯網
上載者:User

標籤:代碼   .com   開發   turn   ted   art   char   tag   ica   

參考串連:http://www.codes51.com/article/detail_4021799.html

 

問題描述 最近在開發android BLE  讀寫資料

但是向裝置寫資料很順利,但是在接收裝置傳來的資料時,死活接收不到.

 /**     * Enables or disables notification on a give characteristic.     *     * @param characteristic Characteristic to act on.     * @param enabled        If true, enable notification.  False otherwise.     */    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,                                              boolean enabled) {        if (mBluetoothAdapter == null || mBluetoothGatt == null) {            Log.w(TAG, "BluetoothAdapter not initialized");            return;        }        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);        }

上面這樣寫 是收不到資料的 ,主要是少加了mBluetoothGatt.writeDescriptor(descriptor);

下面是修改後的代碼

   /**     * Enables or disables notification on a give characteristic.     *     * @param characteristic Characteristic to act on.     * @param enabled        If true, enable notification.  False otherwise.     */    public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,                                              boolean enabled) {        if (mBluetoothAdapter == null || mBluetoothGatt == null) {            Log.w(TAG, "BluetoothAdapter not initialized");            return;        }//        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);        boolean isEnableNotification =  mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);        if(isEnableNotification) {            List<BluetoothGattDescriptor> descriptorList = characteristic.getDescriptors();            if(descriptorList != null && descriptorList.size() > 0) {                for(BluetoothGattDescriptor descriptor : descriptorList) {                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);                    mBluetoothGatt.writeDescriptor(descriptor);                }            }        }    }

 如果 不是這種 情況接收不到資料就是另外一種可能了characteristic的 uuid設定不對

你可以看下 boolean isEnableNotification = mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

返回的是false 還是true,

另外還會再寫一篇關於藍芽資料讀寫的文章

 

以上就介紹了 android BLE 40 setCharacteristicNotification接收不到資料,包括了方面的內容,希望對Android開發有興趣的朋友有所協助。

本文網址連結:http://www.codes51.com/article/detail_4021799.html

android BLE 40 setCharacteristicNotification接收不到資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.