Service-Erkennung fehlgeschlagen, Ausnahme bei Verwendung von Bluetooth unter Android
Ich arbeite derzeit an einer Android-Anwendung, die über Bluetooth eine Verbindung zu einem Instrument herstellt und Zeichenfolgenbefehle schreiben und Zeichenfolgenantworten zurückerhalten muss. Derzeit habe ich die Verbindung / Lesen / Schreiben für TCP / IP über Wi-Fi und jetzt versucht, Bluetooth zu implementieren. Aber ich stoße auf einige Straßensperren. Ich habe im Internet nach ähnlichen Beispielen gesucht und hatte kein Glück. Ich habe das Beispiel für eine Android-Entwicklerressource verwendet: Bluetooth Chat als Hauptreferenzpunkt.
Mein aktueller Code scheint zu funktionieren. Dann wird am Verbindungspunkt eine Ausnahme "Service Discovery Failed" (Dienstermittlung fehlgeschlagen) ausgelöst. Ich benutze dasDeviceListActivity
class, um die Ermittlung und Auswahl des Geräts vorzunehmen, zu dem ich eine Verbindung herstellen möchte. Es gibt anActivityResult zurück und dann wartet meine Bluetooth-Klasse darauf, dass dies erledigt wird und stellt dann die Verbindung her. Der Code darunter ist fast identisch mit der Bluetooth-Chat-App.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(!m_BluetoothAdapter.isEnabled())
{
m_BluetoothAdapter.enable();
}
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
BluetoothDevice device = m_BluetoothAdapter.getRemoteDevice(address);
// Attempt to connect to the device
connect(device);
}
break;
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
// Bluetooth is now enabled, so set up a chat session
}
else {
// User did not enable Bluetooth or an error occured
Toast.makeText(this, "Bluetooth not enabled", Toast.LENGTH_SHORT).show();
finish();
}
}
}
Dies ist meine Verbindungsfunktion:
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private void connect(BluetoothDevice device) {
m_Device = device;
BluetoothSocket tmp = null;
// Get a BluetoothSocket for a connection with the
// given BluetoothDevice
try {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
}
catch (IOException e) {
}
m_Socket = tmp;
m_BluetoothAdapter.cancelDiscovery();
try {
// This is a blocking call and will only return on a
// successful connection or an exception
m_Socket.connect();
}
catch (IOException e) {
try {
m_Socket.close();
}
catch (IOException e2) {
}
return;
}
}
Hoffentlich ist alles, was ich falsch mache, einfach, aber ich fürchte, es ist nie so einfach. Dies ist das erste Mal, dass ich eine Bluetooth-Entwicklung durchführe, und vielleicht mache ich etwas krass Falsche
Sie können das Gerät jederzeit manuell über das Telefon koppeln / finden ... Es ist ein Passcode erforderlich, aber ich glaube nicht, dass dies das Problem ist, das ich habe.