Tom*_*eck 7 android bluetooth android-broadcast
我需要在周围区域扫描蓝牙设备6至12秒.在此之后,我需要停止发现新设备.
以下代码应该:
问题是蓝牙发现永远不会被取消.在此代码运行一两分钟之后,onReceive将在同一秒内被调用数十次......
public void startTrackingButton(View view) {
Log.d("MAIN", "Track button pressed, isTracking: " + !isTracking);
if (isTracking) {
isTracking = false;
} else {
isTracking = true;
Thread keepScanning = new Thread(new Runnable() {
@Override
public void run() {
while (isTracking) {
if (mBluetoothAdapter.isDiscovering()) {
Log.d("MAIN", "Cancelling discovery!");
Log.d("MAIN", String.valueOf(mBluetoothAdapter.cancelDiscovery() + ":" + mBluetoothAdapter.getState()));
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
startTracking();
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
keepScanning.start();
}
}
private void startTracking() {
Log.d("MAIN", "Starting Discovery...");
mBluetoothAdapter.startDiscovery();
// Create a BroadcastReceiver for ACTION_FOUND
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("MAIN", "Device Found...");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a
// ListView
Log.d("MAIN:",
device.getName() + "\n" + device.getAddress());
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister
// during onDestroy
}
Run Code Online (Sandbox Code Playgroud)
这是我的logcat输出:
//onReceive gets called many times in the same second???
05-01 22:09:56.949: D/MAIN(3757): Cancelling discovery!
05-01 22:09:56.969: D/MAIN(3757): false:12 ///THIS SHOULD BE TRUE
05-01 22:09:56.969: D/MAIN(3757): Starting Discovery...
05-01 22:10:03.009: D/MAIN(3757): Starting Discovery...
05-01 22:10:03.579: D/MAIN(3757): Device Found...
05-01 22:10:03.579: D/MAIN:(3757): TOMSELLECK
05-01 22:10:03.579: D/MAIN:(3757): 06:07:08:09:A1:A1
05-01 22:10:03.579: D/MAIN(3757): Device Found...
05-01 22:10:03.579: D/MAIN:(3757): TOMSELLECK
05-01 22:10:03.579: D/MAIN:(3757): 06:07:08:09:A1:A1
05-01 22:10:03.589: D/MAIN(3757): Device Found...
05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK
05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1
05-01 22:10:03.589: D/MAIN(3757): Device Found...
05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK
05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1
05-01 22:10:03.589: D/MAIN(3757): Device Found...
05-01 22:10:03.589: D/MAIN:(3757): TOMSELLECK
05-01 22:10:03.589: D/MAIN:(3757): 06:07:08:09:A1:A1
Run Code Online (Sandbox Code Playgroud)
有谁知道如何正确取消所有当前和未决的蓝牙发现?
谢谢你的帮助!
PS我需要重复这个过程的原因是从附近的设备获得新的信号强度值.
每次打电话startTracking(),您都会创建并注册另一个BroadcastReceiver.因此,在您调用startTracking()十次后,您有十个接收器等待设备.找到设备后,所有这些接收器都会收到通知,这就解释了为什么会有这么多日志条目.
关于cancelDiscovery返回false,Android的蓝牙代码可能是其中最多的错误部分.看看我找到了什么.永远不要假设任何与Android上的蓝牙相关的内容都能在不同版本和设备上正常运行 出于调试目的,您可以为其添加侦听器BluetoothAdapter.ACTION_DISCOVERY_FINISHED.isDiscovering()取消后可能会返回false,但我可以想象取消请求和isDiscovering()返回false 之间会有短暂的延迟.
您的个人BUG建议遇到此错误的客户也喜欢:
EXTRA_DISCOVERABLE_DURATION,并且只能在120秒内启用可发现性.这应该由2.3.6修复,但我看到它发生在2.3.6的三星Galaxy Ace上,所以也许三星打破它或者它毕竟没有修复.不过我的4.x设备上不会发生这种情况.另请注意:由于您使用Thread.sleep,线程可能会暂停超过6秒的实时,因为Thread.sleep在CPU休眠时停止测量的时间.取决于设备的行为的同时进行扫描,这可能是太大超过6秒钟.如果你想使用基于实时的计时,你将不得不使用一个AlarmManager,不幸的是这是一个痛苦的屁股 - 你可能想要为它编写一个薄的包装器.
| 归档时间: |
|
| 查看次数: |
5816 次 |
| 最近记录: |