B
BitCrasher
Neues Mitglied
- 0
Guten Abend zusammen,
mein Name ist Jeremy und ich bin neu hier im Forum. Ich arbeite zur Zeit an einer App mit der ich einen RFduino ansteuern kann. Aktuell bin ich soweit, dass ich erfolgreich Bluetoothgeräte discovern und mit dem Bluetoothdevice weiterarbeiten kann.
Nun zu meinem Problem:
Sobald ich versuche mit mmsocket.connect() eine Verbindung zum Microcontroller aufzubauen funktioniert das auch ( eine grüne LED bestätigt mir eine Verbindung zu einem Bluetoothgerät). Jedoch Zeitgleich verrennt sich meine App in der connect() - Methode und findet erst wieder einen Ausgang, sobald ich meinen Controller stromlos schalte und damit die Verbindung cute.
Ich hoffe einer von euch kann mir meinen Fehler aufzeigen.
mein Name ist Jeremy und ich bin neu hier im Forum. Ich arbeite zur Zeit an einer App mit der ich einen RFduino ansteuern kann. Aktuell bin ich soweit, dass ich erfolgreich Bluetoothgeräte discovern und mit dem Bluetoothdevice weiterarbeiten kann.
Nun zu meinem Problem:
Sobald ich versuche mit mmsocket.connect() eine Verbindung zum Microcontroller aufzubauen funktioniert das auch ( eine grüne LED bestätigt mir eine Verbindung zu einem Bluetoothgerät). Jedoch Zeitgleich verrennt sich meine App in der connect() - Methode und findet erst wieder einen Ausgang, sobald ich meinen Controller stromlos schalte und damit die Verbindung cute.
Ich hoffe einer von euch kann mir meinen Fehler aufzeigen.
Code:
public class ListViewActivityAnz extends ActionBarActivity implements OnTouchListener {
private static final String NAME = null ;
public List<BTmodel> btModel;
public Boolean Sperre = true;
private BluetoothAdapter btAdapter;
public Boolean hochfahren = true;
public Boolean runterfahren = true;
BTmodel test = new BTmodel();
MainActivity test2 = new MainActivity();
public OutputStream myOutputStream;
public UUID MY_UUID = UUID.fromString("a46dd62b-318c-4bfc-8e4d-21475a4e379b");
BluetoothSocket sock = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_activity_anz);
Intent intent = getIntent();
String toasttext = intent.getStringExtra("selected");
String mac = toasttext.substring(toasttext.lastIndexOf("MAC: ") + 6);
btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice btdevice = btAdapter.getRemoteDevice(mac);
ConnectThread ct = new ConnectThread(btdevice);
Toast.makeText(ListViewActivityAnz.this, btdevice.getName(), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "a" + ct.mmSocket.isConnected(), Toast.LENGTH_SHORT).show();
ct.run();
ct.cancel();
//setupUI();
}
catch (Exception e) { }
{
Thread.currentThread().interrupt();
}
}
private void setupUI()
{
TextView Message = (TextView) findViewById(R.id.status2);
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket tmp = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) { }
mmSocket = tmp;
}
public void run() {
// Cancel discovery because it will slow down the connection
btAdapter.cancelDiscovery();
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
new ConnectedThread(mmSocket);
}
/** Will cancel an in-progress connection, and close the socket */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private static final int MESSAGE_READ = 0;
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the input and output streams, using temp objects because
// member streams are final
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
Handler mHandler=null;
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
/* Call this from the main activity to shutdown the connection */
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list_view_activity_anz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
}