适用于android的具有恒定小时间间隔的BLE连续RSSI扫描
我正在制作一个android应用程序,它应该能够连续扫描蓝牙低能设备RSSIs。我使用的是谷歌API,我的Android版本是4.3。我在智能手机上使用三星Galaxy S3,我正在尝试扫描tōd智能信标: 问题是,我得到的值是这样的: 从信标上快速获取8个RSSI值,每个值之间的距离约为0.4秒。 然后有一个5-6秒的大停顿,什么都没有发生。 然后我又从信标上得到了7-8个RSSI值。。。 又是一次大的停顿。。。 等等 我想要得到的是每个RSSI值之间的连续间隔。它们不一定是精确的间隔,但至少是接近的。我尝试过不同的例子,但不知道问题出在哪里。也许tod智能信标只在5秒之间发出广告?我可以通过更新固件来改变广告时间间隔。但是,在很短的时间间隔内获得许多值是没有意义的,而且这些值是不同的=它们不是每个值的副本 我一直使用的代码如下:适用于android的具有恒定小时间间隔的BLE连续RSSI扫描,android,bluetooth,Android,Bluetooth,我正在制作一个android应用程序,它应该能够连续扫描蓝牙低能设备RSSIs。我使用的是谷歌API,我的Android版本是4.3。我在智能手机上使用三星Galaxy S3,我正在尝试扫描tōd智能信标: 问题是,我得到的值是这样的: 从信标上快速获取8个RSSI值,每个值之间的距离约为0.4秒。 然后有一个5-6秒的大停顿,什么都没有发生。 然后我又从信标上得到了7-8个RSSI值。。。 又是一次大的停顿。。。 等等 我想要得到的是每个RSSI值之间的连续间隔。它们不一定是精确的间隔,但至少
public class MainActivity extends ListActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
private static final String TAG=MainActivity.class.getSimpleName();
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();
private int scanCount;
private static final int REQUEST_ENABLE_BT=123456;
// Stops scanning after 1000 seconds.
private static final long SCAN_PERIOD = 1000000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
checkBLE();
init();
boolean ret = enableBLE();
if(ret){
startScan(false);
}else{
Log.d(TAG,getCtx()+" onCreate Waiting for on onActivityResult");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void init(){
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
}
private void startScan(boolean success){
if(mBluetoothAdapter == null){
init();
}
if(success){
mScanning=true;
scanLeDevice(mScanning);
return;
}
if(enableBLE()){
mScanning=true;
scanLeDevice(mScanning);
}else{
Log.d(TAG,getCtx()+" startScan Waiting for on onActivityResult success:"+success);
}
}
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
Log.d(TAG,getCtx() + "run stopLeScan");
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
Log.d(TAG,getCtx()+" scanLeDevice startLeScan:"+enable);
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
Log.d(TAG,getCtx()+ " scanLeDevice stopLeScan:"+enable);
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
private static String getCtx(){
Date dt = new Date();
return dt+ " thread:"+Thread.currentThread().getName();
}
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, final int rssi,
final byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
scanCount++;
String msg=getCtx()+
"\nLeScanCallback.onLeScan stopLeScan run " +scanCount+
"\nDevice:" +device+
"\nRssi:" + rssi+
"\nScanRecord:";
Log.d(TAG,msg);
addItems(msg);
}
});
}
};
private void addItems(String msg) {
synchronized(listItems){
listItems.add(msg);
adapter.notifyDataSetChanged();
}
}
public void startScan(View v) {
startScan(false);
}
public void stopScan(View v) {
mScanning=false;
scanLeDevice(mScanning);
}
public void clear(View v) {
Log.d(TAG,getCtx()+" called clear");
synchronized(listItems){
listItems.clear();
adapter.notifyDataSetChanged();
}
}
private void checkBLE(){
// Use this check to determine whether BLE is supported on the device. Then
// you can selectively disable BLE-related features.
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}
}
private boolean enableBLE(){
boolean ret=true;
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Log.d(TAG,getCtx()+" enableBLE either mBluetoothAdapter == null or disabled:"+mBluetoothAdapter);
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);
ret=false;
}
return ret;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG,getCtx()+" onActivityResult requestCode="+requestCode+
", resultCode="+resultCode+", Intent:"+data
);
if(requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK){
startScan(true);
}
}
}有一种叫做蓝牙嗅探器的设备,你可以用它来检查信标是否像你预期的那样发送信号