Scan bluetooth 4.0 module (bluetooth 4.0) in android studio

I am trying to develop an application for scanning a BLE device. However, it scans only once. I tried using the while loop to loop it, but it hangs there. Part of the scan is in the continue function:

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);

}

}

How to scan an application in a loop?

+4
source share
2 answers

If "by scanning in a loop?" you mean detecting all nearby broadcast devices, you did it. Line of code:

mBluetoothAdapter.startLeScan(mLeScanCallback) 

. "mLeScanCallback", . ,

mBluetoothAdapter.stopScan(mLeScanCallback) 

. : 50000000 .

, 1 , .

+1

, , , , . - :

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

, , , , Bluetooth, Android-.

, bluetooth, , .

BLE Android , , , - , Bluetooth. , LeScanCallback n . , , Android KitKat (BLE API 18).

, - , Android .

, Android JELLY_BEAN_MR2, , - .

kontakt.io iBeacon: : http://devdocs.kontakt.io.

0

All Articles