I am trying to list bluetooth devices available periodically. The idea is to run the startDiscovery() fuction every 4 secs and list the visible devices.
package com.neondude.cupid;
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.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
Timer mTimer;
private ListView listView;
private ArrayList<String> mDeviceList = new ArrayList<String>();
private BluetoothAdapter mBluetoothAdapter;
private ArrayAdapter<String> BTArrayAdapter;
Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
BTArrayAdapter = new ArrayAdapter<String>(this,R.layout.list_view);
listView.setAdapter(BTArrayAdapter);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
private void startTimer(){
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new Mytask(), 4000, 1000 * 5);
}
class Mytask extends TimerTask {
@Override
public void run(){
mHandler.post(new Runnable(){
@Override
public void run(){
find();
}
});
}
}
public void find(){
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
else {
BTArrayAdapter.clear();
mBluetoothAdapter.startDiscovery();
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
mBluetoothAdapter.startDiscovery();
}
final BroadcastReceiver mReceiver = 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);
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE);
BTArrayAdapter.add(device.getName() + "\n" + device.getAddress() + "\n" + rssi);
BTArrayAdapter.notifyDataSetChanged();
}
}
};
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
}
I have included the startDiscovery() function in the timertask handler. But when i do this , the devices and not listed in my listView.
You should check if the bluetooth is on, and ask the user to turn on if is off:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
else{
startTimer();
}
You could start the timer onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (resultCode == RESULT_OK) {
// The user turn on the bluetooth
startTimer();
}
}
}
从不调用startTimer()
函数,因此timerTask不开始。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.