簡體   English   中英

在android studio中掃描ble模塊(藍牙4.0)

[英]Scan for ble module (bluetooth 4.0) in android studio

我正在嘗試開發一個應用來掃描BLE設備。 但是,它只掃描一次。 我嘗試使用while循環來循環它但它掛在那里。 掃描部分處於繼續功能:

package com.example.user.myfriend;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;


public class MainActivity extends ActionBarActivity {
BluetoothAdapter mBluetoothAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {


    BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothAdapter = bluetoothManager.getAdapter();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    hello();

}

public void hello() {

    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

        startActivityForResult(enableBluetooth, 1);


    }
    proceed();
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            proceed();


        }

    }
    super.onActivityResult(requestCode, resultCode, data);
}

private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {


    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {

        int startByte = 2;
        boolean patternFound = false;
        while (startByte <= 5) {

            if (((int) scanRecord[startByte + 2] & 0xff) == 0x02 && //Identifies an iBeacon
                    ((int) scanRecord[startByte + 3] & 0xff) == 0x15) { //Identifies correct data length
                patternFound = true;
                break;
            }
            startByte++;
        }

        if (patternFound) {

            //Convert to hex String
            byte[] uuidBytes = new byte[16];
            System.arraycopy(scanRecord, startByte + 4, uuidBytes, 0, 16);
            String hexString = bytesToHex(uuidBytes);

            //Here is your UUID
            String uuid = hexString.substring(0, 8) + "-" +
                    hexString.substring(8, 12) + "-" +
                    hexString.substring(12, 16) + "-" +
                    hexString.substring(16, 20) + "-" +
                    hexString.substring(20, 32);

            //Here is your Major value
            int major = (scanRecord[startByte + 20] & 0xff) * 0x100 + (scanRecord[startByte + 21] & 0xff);

            //Here is your Minor value
            int minor = (scanRecord[startByte + 22] & 0xff) * 0x100 + (scanRecord[startByte + 23] & 0xff);

            if (major == 1) {
                RelativeLayout hai = (RelativeLayout) findViewById(R.id.hai);
                hai.setBackgroundColor(Color.YELLOW);

            }
            if (major == 2) {
                RelativeLayout hai = (RelativeLayout) findViewById(R.id.hai);
                hai.setBackgroundColor(Color.RED);

            }


        }


    }


};


private static String bytesToHex(byte[] bytes) {
    final char[] hexArray = "0123456789ABCDEF".toCharArray();
    char[] hexChars = new char[bytes.length * 2];
    for (int j = 0; j < bytes.length; j++) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

public void proceed() {
    boolean scanning = true;

    mBluetoothAdapter.startLeScan(mLeScanCallback);


    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        //  @Override
        public void run() {
            mBluetoothAdapter.stopLeScan(mLeScanCallback);


        }
    }, 50000000);

}

}

如何讓應用程序循環掃描?

如果“通過循環掃描?” 你的意思是檢測所有附近的廣播設備,你已經完成了。 代碼行:

mBluetoothAdapter.startLeScan(mLeScanCallback) 

導致掃描開始。 在掃描期間,將在發現每個廣播設備時調用回調“mLeScanCallback”。 掃描將一直有效,直到

mBluetoothAdapter.stopScan(mLeScanCallback) 

叫做。 在你的情況下,這將在很長一段時間后被調用:50000000毫秒。

也許您在該區域只有1個廣播設備,因此您在測試時只能獲得1個回調。

不幸的是,您測試應用程序的設備模型極有可能在掃描性能方面表現不佳。 正是因為這個bug:

https://code.google.com/p/android/issues/detail?id=65863

問題的確切性質是未知的,但根據上述問題的大多數參與者,這取決於安裝在Android設備中的藍牙芯片組。

有趣的是,有些設備會立即處理藍牙執行循環,有些設備只執行一次或多次掃描。

我每天都在處理Android中的BLE,而我解決問題的方法是創建Object封裝周期性強制藍牙掃描。 我的調度程序封裝了單個線程,其中LeScanCallback的實例每隔n百毫秒啟動和停止。 雖然它是一個黑客,它解決了問題,但從Android KitKat開始(BLE在API 18中引入)。

當然,在我的庫中使用周期性強制掃描調度程序是可選的,因為某些Android設備不需要它工作。

順便說一句,你遲早會在Android JELLY_BEAN_MR2上遇到系統崩潰,導致顯示出錯的對話框。

我建議嘗試kontakt.io iBeacon庫:文檔和howto可以在這里找到: http ://devdocs.kontakt.io。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM