Android上的蓝牙:调试startDiscovery() [英] Bluetooth on Android: Debuging startDiscovery()
问题描述
我正在开发一个搜索可发现设备并将其显示为按钮的应用程序.
调用startDiscovery()
时,根据我当前使用BroadcastReceiver
和ACTION_DISCOVERY_FINISHED
调试的方式,我会说它工作30%的时间.
I'm working on an app that searches for discoverable devices and displays them as buttons.
When calling startDiscovery()
I would say it works 30% of the time, based on the way I'm currently debugging it, with the BroadcastReceiver
and ACTION_DISCOVERY_FINISHED
.
我也在使用isDiscovering()
来测试startDiscovery()
函数是否被调用,但是它返回false.
I'm also using isDiscovering()
to test if the startDiscovery()
function is called but it returns false.
有没有办法知道是否成功调用了startDiscovery()
?并且您可以在我的代码中识别出不会失败的东西吗?
Is there a way to know if startDiscovery()
is called successfully? And can you identify something in my code that would make it not fail?
不好.:我同时具有BLUETOOTH
和BLUETOOTH_ADMIN
权限.
Obs.: I have both BLUETOOTH
AND BLUETOOTH_ADMIN
permissions.
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String Address;
// 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);
Address = device.getAddress();
System.out.println("Found Address: " + Address ); //DEBUG
//Do something with Address
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.println("Discovery finished");
}
}
};
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
MainActivity.mBluetoothAdapter.startDiscovery();
if (MainActivity.mBluetoothAdapter.isDiscovering()) {
System.out.println("Discovering..."); //DEBUG
}
}
尽管我有一些可发现的设备,但没有一个会用ACTION_FOUND
触发onReceive()
Although I have a few discoverable devices available, none of them trigger onReceive()
with ACTION_FOUND
更新:运行该应用程序时,我转到了蓝牙设置"下的扫描",并且无法扫描新设备.我禁用/启用了蓝牙并返回到应用程序,问题已解决.我不知道这是否表明适配器正忙或暂停.
UPDATE: I went to "Scan" under Bluetooth Settings while the app was running and I could not scan for new devices. I disabled/enabled Bluetooth and returned to the app and the problem was resolved. I don't know if that indicates that the adapter is busy or halted somehow.
推荐答案
我确认了这个问题.
在某些电话上,您只需要禁用/激活BT.您可以通过编程方式进行操作
On some telephones you just need to disable/active BT. You can doit programatically with
mBluetoothAdapter.disable();
mBluetoothAdapter.enable();
在某些电话上,这还不够(三星S5).为了检测它,我使用计时器,并且在超时结束时未收到BT广播状态的更改(BluetoothAdapter.ACTION_DISCOVERY_STARTED或BluetoothAdapter.ACTION_DISCOVERY_FINISHED)=>表示BT无法正常工作的迹象.实际上,我显示了一个对话框,建议用户重新启动电话.
On some telephones its not enough ( Samsung S5 ). To detect it, I use timer, and if on end of timeout the change of BT broadcast state (BluetoothAdapter.ACTION_DISCOVERY_STARTED or BluetoothAdapter.ACTION_DISCOVERY_FINISHED ) wasnt received => its sign that BT is not working. Actually I show dialog which propose to user reboot the telephone.
这篇关于Android上的蓝牙:调试startDiscovery()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!