发现Android中可用的蓝牙设备 [英] Discovering available bluetooth devices in android
问题描述
我刚刚开始学习Android开发。我正在写一个Android程序扫描可用的蓝牙设备,并列出它们在日志文件中。由于我是新来的Android,我无法弄清楚什么是错在下面的code段。
button2.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
ListView控件LV1 =(ListView控件)findViewById(R.id.myListView1);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
最后的BroadcastReceiver mReceiver =新的BroadcastReceiver()
{
公共无效的onReceive(上下文的背景下,意图意图)
{
串动= intent.getAction();
//当发现找到的设备
如果(BluetoothDevice.ACTION_FOUND.equals(动作))
{
//从意图获取BluetoothDevice类对象
BluetoothDevice类设备= intent.getParcelableExtra(
BluetoothDevice.EXTRA_DEVICE);
Log.v(蓝牙测试,device.getName()+\ N
+ device.getAddress());
}
}
};
字符串aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
startActivityForResult(新意图(aDiscoverable),DISCOVERY_REQUEST);
IntentFilter的过滤器=新的IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver,过滤器);
mBluetoothAdapter.startDiscovery();
}
});
当我尝试调试模式下,控制跳过的BroadcastReceiver()
。我找不到日志中的任何条目。你能不能帮我搞清楚的问题是什么,以及如何我可以列出可用的蓝牙设备。
我收到错误的应用程序意外停止:
logcat的错误:
10-28 20:08:24.201:ERROR / UpdateReceiver(914):ACTION_PACKAGE_REMOVED
10-28 20:08:28.415:ERROR / RequestPermissionActivity(431):超时= 120
10-28 20:08:44.291:ERROR / DTUN_HCID4(521):=== dtunops_stop_discovery()===
您应该具备蓝牙和BLUETOOTH_ADMIN权限您的清单
I just started learning Android development. I am writing an Android program to scan available bluetooth devices and list them in a log file. As I am new to Android, I am unable to figure out what is wrong in the below code snippet.
button2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
ListView lv1 = (ListView) findViewById(R.id.myListView1);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
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))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(
BluetoothDevice.EXTRA_DEVICE);
Log.v("BlueTooth Testing",device.getName() + "\n"
+ device.getAddress());
}
}
};
String aDiscoverable = BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
startActivityForResult(new Intent(aDiscoverable),DISCOVERY_REQUEST);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mBluetoothAdapter.startDiscovery();
}
});
When I try the debug mode, the control skips the "BroadcastReceiver()
". I could not find any entries in the log. Can you help me in figuring out what the problem is and how I can list the available bluetooth devices.
[edit] I am getting error that the application has stopped unexpectedly:
Logcat Errors:
10-28 20:08:24.201: ERROR/UpdateReceiver(914): ACTION_PACKAGE_REMOVED
10-28 20:08:28.415: ERROR/RequestPermissionActivity(431): Timeout = 120
10-28 20:08:44.291: ERROR/DTUN_HCID4(521): === dtunops_stop_discovery() ===
You should have the BLUETOOTH and BLUETOOTH_ADMIN permissions in your manifest
这篇关于发现Android中可用的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!