Ich versuche, eine einfache Verbindungssequenz zu einem seriellen Bluetooth-Gerät am Anfang meiner App zu machen. Im Moment ist das alles innerhalb onCreate()
:Fehler bei der Bluetooth-Sequenz
BT = BluetoothAdapter.getDefaultAdapter();
BT.enable();
if(!BT.isEnabled()) {
Intent enabler = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enabler, REQUEST_ENABLE);
BT.enable();
Toast.makeText(getApplicationContext(), "Bluetooth On.", Toast.LENGTH_LONG).show();
//finish apk
finish();
}
else {
Toast.makeText(getApplicationContext(), "Bluetooth On.", Toast.LENGTH_LONG).show();
}
pairedDevices = BT.getBondedDevices();
pDevices = new ArrayList<BluetoothDevice>();
if (pairedDevices.size()>0) {
for(BluetoothDevice bt : pairedDevices)
{
pDevices.add(bt); //Get the device's name and the address
}
}
else {
Toast.makeText(getApplicationContext(), "Nothing paired.", Toast.LENGTH_LONG).show();
}
try {
BluetoothDevice dispositivo = BT.getRemoteDevice(pDevices.get(0).getAddress());
btSocket = dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);
btSocket.connect();
}
catch (IOException e){
Toast.makeText(getApplicationContext(), "Failed.", Toast.LENGTH_LONG).show();
}
Ziel zum ersten verfügbaren gekoppeltes Gerät zu verbinden ist. Bisher wird immer "Fehlgeschlagen" angezeigt. auch wenn ich ein nicht verbundenes gekoppeltes Gerät neben dem Telefon habe.
Sollte ich das woanders in der App machen? Ich bin nicht wirklich damit beschäftigt, die Hauptaktivität zu verschieben, da dies für ein persönliches Projekt ist.
Edit: Rechtschreibung