Android中的蓝牙设备发现——startDiscovery()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10560319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bluetooth device discovery in Android -- startDiscovery()
提问by Lemminkainen
Goal:Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about.
目标:构建一个 Android 应用程序,该应用程序可以发现范围内 BT 设备的名称和地址,并将它们的值提交给网络服务。BT 设备以前没有绑定到主机设备,我只想在走动时轮询所有内容。
What I've done:
我所做的:
- Pored over documentation.
- Implemented a local instance of the host device's BT adapter.
- Implemented a notification to enable BT if it isn't enabled.
- Registered Broadcast Receivers and Intents to parse the
ACTION_FOUNDs
coming off of startDiscovery(). - Registered BLUETOOTHand BLUETOOTH_ADMINpermissions in the manifest.
- 仔细阅读文档。
- 实现了主机设备的 BT 适配器的本地实例。
- 如果未启用 BT,则实现了启用 BT 的通知。
- 注册广播接收机和意图解析
ACTION_FOUNDs
的脱落startDiscovery() 。 - 在清单中注册BLUETOOTH和BLUETOOTH_ADMIN权限。
Things work (as tested with incremental console logging) up until startDiscovery()
.
直到startDiscovery()
.
Frustration:
挫折:
- startDiscovery()-- I suspect I am passing this in the wrong context. What context does this method need to be placed within to function properly?
- startDiscovery()——我怀疑我在错误的上下文中传递了这个。这个方法需要放在什么上下文中才能正常运行?
If you have been able to get this method working, I would very much appreciate your wisdom.
如果您能够使这种方法发挥作用,我将非常感谢您的智慧。
UPDATE- here's a stripped down simplified version of the code that is causing me grief; this simplification recapitulates my error. This code runs, it throws no cat.log
errors or other errors, it simply doesn't give any output.
更新- 这是让我感到悲伤的代码的精简版;这种简化概括了我的错误。此代码运行,它不会引发cat.log
错误或其他错误,它只是不提供任何输出。
package aqu.bttest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;
public class BT2Activity extends Activity {
private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//register local BT adapter
mBTA = BluetoothAdapter.getDefaultAdapter();
//check to see if there is BT on the Android device at all
if (mBTA == null){
int duration = Toast.LENGTH_SHORT;
Toast.makeText(this, "No Bluetooth on this handset", duration).show();
}
//let's make the user enable BT if it isn't already
if (!mBTA.isEnabled()){
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, 0xDEADBEEF);
}
//cancel any prior BT device discovery
if (mBTA.isDiscovering()){
mBTA.cancelDiscovery();
}
//re-start discovery
mBTA.startDiscovery();
//let's make a broadcast receiver to register our things
mReceiver = new SingBroadcastReceiver();
IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(mReceiver, ifilter);
}
private class SingBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); //may need to chain this to a recognizing function
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 Toast
String derp = device.getName() + " - " + device.getAddress();
Toast.makeText(context, derp, Toast.LENGTH_LONG);
}
}
}
}
}
回答by Alex Lockwood
What context does this method need to be placed within to function properly.
此方法需要放置在什么上下文中才能正常运行。
To put it simply, you should use startDiscovery()
when you want your application to discover local Bluetooth devices... for instance, if you wanted to implement a ListActivity
that scans and dynamically adds nearby Bluetooth devices to a ListView
(see DeviceListActivity
).
简而言之,startDiscovery()
当您希望您的应用程序发现本地蓝牙设备时,您应该使用它……例如,如果您想实现一个ListActivity
扫描并将附近的蓝牙设备动态添加到一个ListView
(请参阅 参考资料DeviceListActivity
)的 。
Your usage of the startDiscovery()
method should look something like this:
您对该startDiscovery()
方法的使用应如下所示:
Define a class variable representing the local Bluetooth adapter.
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Check to see if your device is already "discovering". If it is, then cancel discovery.
if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); }
Immediately after checking (and possibly canceling) discovery-mode, start discovery by calling,
mBtAdapter.startDiscovery();
Be very careful in general about accidentally leaving your device in discovery-mode. Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. For instance, you want to make sure you check/cancel discovery prior to attempting to make a connection. You most likely want to cancel discovery in your
onDestroy
method too.
定义一个表示本地蓝牙适配器的类变量。
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
检查您的设备是否已经在“发现”。如果是,则取消发现。
if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); }
在检查(并可能取消)发现模式后,立即通过调用开始发现,
mBtAdapter.startDiscovery();
通常要非常小心,以免意外将设备置于发现模式。执行设备发现对于蓝牙适配器来说是一个繁重的过程,会消耗大量资源。例如,您要确保在尝试建立连接之前检查/取消发现。您很可能也想在您的
onDestroy
方法中取消发现。
Let me know if this helped... and if you are still having trouble, update your answer with your logcat output and/or any error messages you are getting, and maybe I can help you out a bit more.
让我知道这是否有帮助...如果您仍然遇到问题,请使用您的 logcat 输出和/或您收到的任何错误消息更新您的答案,也许我可以为您提供更多帮助。