I'm writing an app to turn on bluetooth, scan for bluetooth-discoverable devices nearby including paired and not paired in order to add them to a list.
Here is my code:
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);
}
}
The code seems fine, but it doesn't find or adds any nearby devices to list and when I quit the app, I get following exception log:
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)
Any idea to fix the problems?
To add paired devices to the list view, I have to add a button and associate it with scan() method. That's frustrating, I want to populate the list when I hit "Turn On" button. Besides, finding not paired devices still remains.