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;@OverrIDeprotected 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();}@OverrIDeprotected 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,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 studio中扫描ble模块(蓝牙4.0)全部内容,希望文章能够帮你解决在android studio中扫描ble模块(蓝牙4.0)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)