D
daviddy
Neues Mitglied
- 0
hallo
ich möchte mit meinem PC über bluetooth verbinden. Durch meine App kann die liste von verfügbaren geräte schon sehen. meine frage ist:
wie kann ich meine handy verbinden, wenn ich auf der liste irgend einen gerät klicke.
Vielen Dank schon mal.
meine code ist:
ich möchte mit meinem PC über bluetooth verbinden. Durch meine App kann die liste von verfügbaren geräte schon sehen. meine frage ist:
wie kann ich meine handy verbinden, wenn ich auf der liste irgend einen gerät klicke.
Vielen Dank schon mal.
meine code ist:
Code:
public class ScanActivity extends ListActivity {
private static final int REQUEST_BT_ENABLE = 0x1;
public static String EXTRA_DEVICE_ADDRESS = "device_address";
ListView listGeraete;
TextView text;
BluetoothAdapter bluetoothAdapter;
ArrayAdapter<String> arrayAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
text = (TextView) findViewById(R.id.txt);
ListView listGeraete = getListView();
arrayAdapter = new ArrayAdapter<String>(ScanActivity.this,android.R.layout.simple_list_item_1);
listGeraete.setAdapter(arrayAdapter);
// if bt disable, enabling
if (!bluetoothAdapter.isEnabled()) {
Intent enableBt = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBt, REQUEST_BT_ENABLE);
}
// start discovery
bluetoothAdapter.startDiscovery();
registerReceiver( ScanReceiver , new IntentFilter(
BluetoothDevice.ACTION_FOUND));
}
private final BroadcastReceiver ScanReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
text.setText("Die verfügbaren BT-Geräten sind:");
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();
}
}
};
// select a device
public void onListItemClick(ListView l, View view, int position, long id) {
bluetoothAdapter.cancelDiscovery();
String devicesinfo = ((TextView) view).getText().toString();
String address = devicesinfo.substring(devicesinfo.length());
Intent intent = new Intent();
intent.putExtra(EXTRA_DEVICE_ADDRESS, address);
setResult(Activity.RESULT_OK, intent);
Toast.makeText(getApplicationContext(),"Connecting to " + devicesinfo +
address,
Toast.LENGTH_SHORT).show();
}
}