如何使用Android Beacon

时间:2015-06-04 10:38:28

标签: android ibeacon

我试过this, 我能够扫描所有设备,但无法使用Android核心类计算主要和次要设备。

我试过这个

public class TagBluetooth {

private Context context;
private List uuidList;
private iTagBle iTagble;
private BluetoothManager bluetoothManager;
private BluetoothAdapter bluetoothAdapter;
private BluetoothLeScanner bluetoothLeScanner;
private final int REQUEST_ENABLE_BT = 101;
private boolean mScanning;
private Handler handler;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
private BluetoothAdapter.LeScanCallback leScanCallback;
private List list;


public TagBluetooth(iTagBle activity){
    this.context= (Context) activity;
    iTagble=activity;
}


public TagBluetooth(iTagBle activity,List list){
    this.context= (Context) activity;
    iTagble=activity;
    uuidList=list;
}

/**
 * initialize ble component
 */
public void initialize(){

    if (isBLEAvailable()) {
        bluetoothManager = (BluetoothManager) context.getSystemService(context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();
        handler = new Handler();

        leScanCallback = new BluetoothAdapter.LeScanCallback() {
            @Override
            public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
                iTagble.onScanComplete(device,rssi,scanRecord);
            }
        };

    }

    checkBluetoothStatus();
}

/**
 * Ensures Bluetooth is available on the device and it is enabled. If not,
 * displays a dialog requesting user permission to enable Bluetooth.
 *
 * @return true for device bluetooth available and false for bluetooth not available
 */
private void checkBluetoothStatus() {

    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        context.startActivity(enableBtIntent);

    }
}


/**
 * ebale and disable ble devices scanning
 *
 * @param enable
 */

public void scanLeDevice(final boolean enable) {

    if(!isBLEAvailable())
        return;

    if (enable) {

        // Stops scanning after a pre-defined scan period.
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mScanning = false;
                bluetoothAdapter.stopLeScan(leScanCallback);
            }
        }, SCAN_PERIOD);

        mScanning = true;
        bluetoothAdapter.startLeScan(leScanCallback);
    } else {
        mScanning = false;
        bluetoothAdapter.stopLeScan(leScanCallback);
    }
}




/**
 * Use this check to determine whether BLE is supported on the device. Then
 * you can selectively disable BLE-related features.
 *
 * @return true for BLE support and false for BLE unsupported
 */
private boolean isBLEAvailable() {

    if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        iTagble.showMessage("BLE not supported");
        return false;
    }

    return true;
}

}

要求:

  1. 扫描所有信标设备(包括不同的制造商)。
  2. 跟踪信标的进入和退出。
  3. 次要和主要范围。
  4. 扫描设备甚至应用程序已关闭。
  5. 如果有人帮助我,我会感激不尽。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用here中的Kontakt API。但是,您无法确定任何API的准确进入和退出时间。见here

相关问题