我正在构建一个 android 蓝牙聊天应用程序并在其中遇到一些问题。我的问题是:我无法检测到范围内可用的蓝牙设备,也无法在列表中显示。因为我是 android 编程新手,无法检测到问题。请帮帮我。
我的代码是:
public class BluetoothSearchActivity extends Activity {
ArrayAdapter<String> btArrayAdapter;
BluetoothAdapter mBluetoothAdapter;
TextView stateBluetooth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView BluetoothSearchImageView=new ImageView(this);
BluetoothSearchImageView.setImageResource(R.drawable.inner1);
setContentView(BluetoothSearchImageView);
setContentView(R.layout.activity_bluetooth_search);
mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
ListView listDevicesFound=(ListView) findViewById(R.id.myList);
btArrayAdapter=new ArrayAdapter<String> (BluetoothSearchActivity.this,android.R.layout.simple_list_item_1);
listDevicesFound.setAdapter(btArrayAdapter);
registerReceiver(ActionFoundReceiver,new IntentFilter(BluetoothDevice.ACTION_FOUND));
btArrayAdapter.clear();
mBluetoothAdapter.startDiscovery();
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
private final BroadcastReceiver ActionFoundReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device=intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
btArrayAdapter.add(device.getName()+"\n"+device.getAddress());
btArrayAdapter.notifyDataSetChanged();
}
}
};