Android 蓝牙连续搜索不起作用?
我编写了以下代码,并希望我的蓝牙持续工作,是否有任何正确的方法可以让我的 startdiscovery 一直被扫描,直到我的程序运行?
mBluetoothAdapter.startDiscovery();
System.out.println("flow is here");
// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
System.out.println("Print in IF");
// 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
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
//System.out.println(mNewDevicesArrayAdapter);
//info = mNewDevicesArrayAdapter.getItem(x).toString();
System.out.println("Found :\t"+device.getAddress()+"\tName :"+device.getName());
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.prinlnt("Else BOdy ");
mBluetoothAdapter.startDiscovery();
}
}
甚至 System.out.println("Else BOdy") 根本没有显示...
int delay = 10000; // delay for 1 sec.
int period = 15000; // repeat every 10 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run()
{
displayData(); // display the data
}
private void displayData() {
System.out.println("Veer Suthar is calling method after 10 seconds");
// TODO Auto-generated method stub
mBluetoothAdapter.startDiscovery();
}
}, delay, period);
这就是我所做的,所以它被调用,并且
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
这个方法对我不起作用。
I have written the following code and want my bluetooth continously working, and is there any proper way so that my startdiscovery should be scan until my program in running?
mBluetoothAdapter.startDiscovery();
System.out.println("flow is here");
// Create a BroadcastReceiver for ACTION_FOUND
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
System.out.println("Print in IF");
// 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
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
//System.out.println(mNewDevicesArrayAdapter);
//info = mNewDevicesArrayAdapter.getItem(x).toString();
System.out.println("Found :\t"+device.getAddress()+"\tName :"+device.getName());
}
else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.prinlnt("Else BOdy ");
mBluetoothAdapter.startDiscovery();
}
}
Even System.out.println("Else BOdy") is not showing at all...
int delay = 10000; // delay for 1 sec.
int period = 15000; // repeat every 10 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run()
{
displayData(); // display the data
}
private void displayData() {
System.out.println("Veer Suthar is calling method after 10 seconds");
// TODO Auto-generated method stub
mBluetoothAdapter.startDiscovery();
}
}, delay, period);
This is what simply I have done, so it is called, and
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
This method didn't work with me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论