无法使用蓝牙设备完成列表视图

时间:2016-07-11 07:44:12

标签: android android-bluetooth

我正在尝试完成扫描BT设备的列表视图,但它无法正常工作。我已经检查了我找到的所有教程,但我无法解决这个问题。

这是代码Seleccion活动:

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.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class Seleccion extends AppCompatActivity {
ListView lista;
BluetoothAdapter bAdapter;
private static final int    REQUEST_ENABLE_BT   = 1;
private ArrayList<String> mDeviceList = new ArrayList<String>();


private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        System.out.println("aquiiiiiiiiiiiiiiiiiiiiiii");
        BluetoothDevice device =     intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        mDeviceList.add(device.getName() + "\n" + device.getAddress());
        Log.i("BT", device.getName() + "\n" + device.getAddress());
        lista.setAdapter(new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, mDeviceList));
    }
}
};



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seleccion);
lista=(ListView)findViewById(R.id.lista);

bAdapter = BluetoothAdapter.getDefaultAdapter();
bAdapter.startDiscovery();
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, filter);

}

@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(mReceiver);
}
}

任何帮助将不胜感激

0 个答案:

没有答案