android - 在android studio中扫描ble模块(蓝牙4.0)

标签 android bluetooth-lowenergy android-bluetooth

我正在尝试开发一个应用程序来扫描 BLE 设备。但是,它只扫描一次。我尝试使用 while 循环来循环它,但它卡在那里。扫描部分在proceed函数:

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 个回调。

关于android - 在android studio中扫描ble模块(蓝牙4.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30685802/

相关文章:

bluetooth - 如何获取BLE广告 channel 索引号

ios - AirLocate 和 BeaconDemo : not showing up

安卓蓝牙 : Add service data without UUID

java - 工具栏的问题

iOS蓝牙后台模式

Android Activity 不会填满 WVGA 屏幕

java - 通过蓝牙将数据发送到 OBD-ii 设备时应用程序崩溃 (Android-Studio)

android - 无法通过低功耗蓝牙发现服务和特征 - BLE

java - 想要阻止毫秒增加

Android getAuthToken 返回空