我正在编写一个应用程序来打开蓝牙,扫描附近的蓝牙可发现设备,包括已配对但未配对,以便将它们添加到列表中。
这是我的代码:
public class Main extends Activity {
private BluetoothAdapter ba;
private Set<BluetoothDevice>pairedDevices;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.bluetooth_device_listview);
initiate();
}
public void initiate() {
ba = BluetoothAdapter.getDefaultAdapter();
if (ba == null) {
// Device does not support Bluetooth
AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
localBuilder.setTitle("No Bluetooth Support");
localBuilder.setMessage("Device does not support Bluetooth feature.\nThis"
+ "will quit the application.");
localBuilder.setCancelable(false);
localBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
{
Main.this.finish();
}
});
localBuilder.create().show();
}
}
public void turnOn(View view) {
if (!ba.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 0);
if(ba.isEnabled())
Toast.makeText(getApplicationContext(), "Turned On",
Toast.LENGTH_SHORT).show();
scan();
} else
Toast.makeText(getApplicationContext(), "Already On",
Toast.LENGTH_LONG).show();
}
public void scan() {
final ArrayList<String> deviceList = new ArrayList<String>();
if (ba.isDiscovering()) ba.cancelDiscovery();
ba.startDiscovery();
// Get a list of paired devices
pairedDevices = ba.getBondedDevices();
for(BluetoothDevice bt : pairedDevices)
deviceList.add(bt.getName() + "\n" + bt.getAddress() + "\n" + "Paired");
/*********************************************************************/
// Scan for other devices
final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
deviceList.add(device.getName() + "\n" + device.getAddress()
+ "\n" + "Not Paired");
}
}
};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, deviceList);
lv.setAdapter(adapter);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(receiver, filter);
}
}
代码看起来很好,但它没有找到或添加任何附近的设备列表,当我退出应用程序时,我得到以下异常日志:
12-12 12:53:59.818: E/ActivityThread(5097): Activity net.adeveloper.testdeviate.Main has leaked IntentReceiver net.adeveloper.testdeviate.Main$2@4055c128 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 12:53:59.818: E/ActivityThread(5097): android.app.IntentReceiverLeaked: Activity net.adeveloper.testdeviate.Main has leaked IntentReceiver net.adeveloper.testdeviate.Main$2@4055c128 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:799)
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:575)
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:865)
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.ContextImpl.registerReceiver(ContextImpl.java:852)
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.ContextImpl.registerReceiver(ContextImpl.java:846)
12-12 12:53:59.818: E/ActivityThread(5097): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
12-12 12:53:59.818: E/ActivityThread(5097): at net.adeveloper.testdeviate.Main.scan(Main.java:132)
12-12 12:53:59.818: E/ActivityThread(5097): at net.adeveloper.testdeviate.Main.turnOn(Main.java:83)
12-12 12:53:59.818: E/ActivityThread(5097): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 12:53:59.818: E/ActivityThread(5097): at java.lang.reflect.Method.invoke(Method.java:507)
12-12 12:53:59.818: E/ActivityThread(5097): at android.view.View$1.onClick(View.java:2187)
12-12 12:53:59.818: E/ActivityThread(5097): at android.view.View.performClick(View.java:2533)
12-12 12:53:59.818: E/ActivityThread(5097): at android.view.View$PerformClick.run(View.java:9320)
12-12 12:53:59.818: E/ActivityThread(5097): at android.os.Handler.handleCallback(Handler.java:587)
12-12 12:53:59.818: E/ActivityThread(5097): at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 12:53:59.818: E/ActivityThread(5097): at android.os.Looper.loop(Looper.java:150)
12-12 12:53:59.818: E/ActivityThread(5097): at android.app.ActivityThread.main(ActivityThread.java:4385)
12-12 12:53:59.818: E/ActivityThread(5097): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 12:53:59.818: E/ActivityThread(5097): at java.lang.reflect.Method.invoke(Method.java:507)
12-12 12:53:59.818: E/ActivityThread(5097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
12-12 12:53:59.818: E/ActivityThread(5097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
12-12 12:53:59.818: E/ActivityThread(5097): at dalvik.system.NativeStart.main(Native Method)
有什么想法解决问题吗?
要将配对设备添加到列表视图,我必须添加一个按钮并将其与scan()方法相关联。这令人沮丧,我想在点击“开启”按钮时填充列表。此外,找不到配对设备仍然存在。
答案 0 :(得分:0)
根据下面的答案,你应该覆盖BroadcastReceiver中的onStop方法并调用unregisterReceiver()来摆脱那些logcat错误。
https://stackoverflow.com/a/14357640/3250699
但是,我认为这不会解决发现问题。为了确保开始发现,您可以尝试检查ba.startDiscovery()上的返回吗?根据Android Docs,方法...&#34; [startDiscovery()]成功时返回true,错误时返回false&#34;。
文档还提到&#34;需要BLUETOOTH_ADMIN。&#34;所以请确保在AndroidManifest.xml中设置此权限
Android SDK文档: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
答案 1 :(得分:0)
您忘记取消注册BroadcastReceiver。这里有一些建议:
1.您可以在onCreate()中注册BroadcastReceiver,并在onDestroy()中取消注册BroadcastReceiver。
2.尝试注册BroadcastReceiver后开始发现蓝牙设备。
3.请注意,BroadcastRecevier会返回相同的BluetoothDevice,这与蓝牙发现的机制有关。通常你需要按mac地址过滤。
我很困惑你的意思是什么:当我点击&#34;打开&#34;我想填写列表。按钮。此外,找不到配对设备仍然存在。?
当你点击&#34;打开&#34;按钮,你只需做一件事,你检查蓝牙是否打开。然后使用startActivityForResult弹出系统窗口,提醒用户需要打开蓝牙,然后发现在线蓝牙设备并获取配对设备。你不会修改配对的蓝牙设备列表,也许你会发现更多或更少的不成对的蓝牙设备。
答案 2 :(得分:0)
对于最新的Android版本,您还需要添加
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
到AndroidManifest.xml
。
答案 3 :(得分:0)
我也遇到过这个问题。但问题不在代码中,这是因为我没有向应用程序授予位置权限。授予权限后,它会显示附近所有可用的设备