Android中的蓝牙设备发现——startDiscovery()
目标:构建一个Android应用程序,发现范围内BT设备的名称和地址,并将其值提交给Web服务。BT设备之前没有绑定到主机设备,我只想边走边轮询所有内容 我所做的:Android中的蓝牙设备发现——startDiscovery(),android,android-intent,bluetooth,android-adapter,android-context,Android,Android Intent,Bluetooth,Android Adapter,Android Context,目标:构建一个Android应用程序,发现范围内BT设备的名称和地址,并将其值提交给Web服务。BT设备之前没有绑定到主机设备,我只想边走边轮询所有内容 我所做的: 仔细研究文档 实现了主机设备的BT适配器的本地实例 实现了一个通知,以便在未启用BT时启用它 已注册的广播接收器,并打算解析操作来自startDiscovery() 在清单中注册了蓝牙和蓝牙管理权限 在startDiscovery()之前,一切都正常(通过增量控制台日志记录测试) 挫折感: startDiscovery()--我
操作
来自startDiscovery()startDiscovery()
之前,一切都正常(通过增量控制台日志记录测试)
挫折感:
- startDiscovery()--我怀疑我在错误的上下文中传递了此消息。此方法需要放置在什么上下文中才能正常工作
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);
}
}
}
}
此方法需要放置在什么上下文中才能正常工作
简单地说,当您希望应用程序发现本地蓝牙设备时,应该使用startDiscovery()
。。。例如,如果您想要实现一个ListActivity
,它可以扫描附近的蓝牙设备并将其动态添加到ListView
(请参阅)
您使用的startDiscovery()
方法应该如下所示:
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
mBtAdapter.startDiscovery();
ondestory
方法中的查找让我知道这是否有帮助。。。如果您仍然有问题,请使用logcat输出和/或您收到的任何错误消息更新您的答案,也许我可以帮您解决更多问题。了解您收到的错误会有所帮助。。。或者至少是什么让你相信
startDiscovery()
运行不正常。这很有帮助。此外,将结果传递给ArrayAdapter而不是Toast也确实有帮助。有没有办法在BG服务中保持连续扫描而不使用put Timer…任何系统级API。@Shubh我不这么认为。设置此限制的原因是为了确保应用程序不会通过在后台连续扫描而滥用蓝牙。。。我也在试着这么做,但会有什么错误呢-