Lem*_*nen 15 android bluetooth android-intent android-context android-adapter
目标:构建一个Android应用程序,发现范围内BT设备的名称和地址,并将其值提交给Web服务.以前没有将BT设备绑定到主机设备,我只是想在我走动时查看所有内容.
我做了什么:
ACTION_FOUNDs即将到来.事情起作用(使用增量控制台记录测试)直到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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
Ale*_*ood 19
该方法需要在什么上下文中才能正常运行.
简单来说,您应该startDiscovery()在希望应用程序发现本地蓝牙设备时使用...例如,如果您想实施ListActivity扫描并动态添加附近的蓝牙设备ListView(请参阅参考资料DeviceListActivity).
您对该startDiscovery()方法的使用应如下所示:
定义表示本地蓝牙适配器的类变量.
BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
Run Code Online (Sandbox Code Playgroud)检查您的设备是否已"发现".如果是,则取消发现.
if (mBtAdapter.isDiscovering()) {
mBtAdapter.cancelDiscovery();
}
Run Code Online (Sandbox Code Playgroud)检查(并可能取消)发现模式后,立即通过调用启动发现,
mBtAdapter.startDiscovery();
Run Code Online (Sandbox Code Playgroud)一般要非常小心,不小心将设备置于发现模式.执行设备发现对于蓝牙适配器来说是一个繁重的过程,并且会消耗大量资源.例如,您要确保在尝试建立连接之前检查/取消发现.您很可能也希望在onDestroy方法中取消发现.
如果这有帮助,请告诉我......如果您仍然遇到问题,请使用您的logcat输出和/或您收到的任何错误消息更新您的答案,也许我可以帮助您更多.
| 归档时间: |
|
| 查看次数: |
52811 次 |
| 最近记录: |