D
daviddy
Neues Mitglied
- 0
hallo freunde,
wenn mein activity mit ListActivity schreibe, startet mein app nicht.auf eure hilfe freue ich mich sehr)
uses- perm. für bluetooth habe ich schon zugefügt.
danke
wenn mein activity mit ListActivity schreibe, startet mein app nicht.auf eure hilfe freue ich mich sehr)
Code:
public class MainActivity extends ListActivity {
private static final int REQUEST_BT_ENABLE = 0x1;
ListView listGeraete;
TextView text;
BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> arrayAdapter;
static private ArrayAdapter<BluetoothDevice> BtDevices = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
text = (TextView) findViewById(R.id.txt);
listGeraete = (ListView) findViewById(R.id.list);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1);
listGeraete.setAdapter(arrayAdapter);
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
}
// Aktivierung von Bluetooth
public void ClickBtOn(View view)
{
bluetoothAdapter.enable();
Intent enablerBt = new Intent(bluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enablerBt, REQUEST_BT_ENABLE);
}
// Deaktivierung von Bluetooth
public void ClickBtOff(View view) {
bluetoothAdapter.disable();
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(ActionFoundReceiver);
}
public void ClickBtScan(View view) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt, REQUEST_BT_ENABLE);
}
arrayAdapter.clear();
bluetoothAdapter.startDiscovery();
}
private final BroadcastReceiver ActionFoundReceiver = 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);
arrayAdapter.add(device.getName() + "\n" + device.getAddress());
arrayAdapter.notifyDataSetChanged();
}
}
};
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
if(bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.cancelDiscovery();
}
Toast.makeText(getApplicationContext(), "Connecting to " + BtDevices.getItem(position).getName() + "," + BtDevices.getItem(position).getAddress(), Toast.LENGTH_SHORT).show();
}
}
danke