Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien
Toon posts:

Eclipse android bluetooth

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hallo,

Ik ben een beginnend programeur in eclipse en ik probeer een bluetooth verbinding te maken met een HC-06.
Nu heb ik een code die mij goed lijkt, maar als ik op de con functie aanroep crasht mijn app. Iemand een idee hoe dat komt?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.example.bluetooth_test;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    private BluetoothAdapter btAdapter = null;
    private BluetoothSocket btSocket = null;
    private OutputStream outStream = null;
       
    
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    void snd(View view)
    {
        sendData("test");
    
    }

    void con(View view)
    {
    
        try
        {
            Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
            for(BluetoothDevice bt : pairedDevices)
            {
                btSocket = bt.createRfcommSocketToServiceRecord(MY_UUID);
            }
            btAdapter.cancelDiscovery();
            btSocket.connect();
            outStream = btSocket.getOutputStream();
        }
        catch(IOException e)
        {
        }
    
    }
    
    private void sendData(String message) 
    {
        byte[] msgBuffer = message.getBytes();

        try {
            outStream.write(msgBuffer);
        } 
        catch (IOException e) 
        {
        }
    }

}

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 20-11 11:59

NMe

Quia Ego Sic Dico.

Ik zou eens beginnen met breakpoints zetten en door je code steppen tot het crasht zodat je weet wáár het fout gaat.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
^ Dat en daarbij is 't wel handig als je even aangeeft wélke fout (if any) je krijgt.

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Verwijderd

Topicstarter
Hij zegt, "de toepassing bluetooth (proces com.example.bluetooth_test) is onverwacht gestopt. probeer het opnieuw."

Het debuggen lukt niet echt, kan dit wel met een telefoon?

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 20-11 11:59

NMe

Quia Ego Sic Dico.

Natuurlijk kan dat met een telefoon. http://developer.android....g/debugging-projects.html

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • InZane
  • Registratie: Oktober 2000
  • Laatst online: 23:48
Exceptions inslikken is trouwens niet echt netjes..

  • Janoz
  • Registratie: Oktober 2000
  • Laatst online: 20-11 22:59

Janoz

Moderator Devschuur®

!litemod

Met InZane. Elke keer wanneer je een lege catch schrijft zou je jezelf voor je hoofd moeten slaan. Op zijn minst zou je daar een aanroep naar Log in moeten zetten zodat je uberhaupt terug kunt zien dat het opgetreden is.

Bovenin dus even een
Java:
1
private static final String TAG = "MyBluetoothTest";

en de catch aanroepen even aanpassen naar
Java:
1
2
3
catch (IOException e) {
  Log.e(TAG,"IO fout bij connecten / bij versturen data", e);
}

Ken Thompson's famous line from V6 UNIX is equaly applicable to this post:
'You are not expected to understand this'


Verwijderd

Wat zegt Logcat?

Verwijderd

Topicstarter
Hallo,

Dit is enkel een test scripje, dus vandaar dat mijn catch niet zijn opgevangen.
Maar goed,
Dit is de fout,
09-22 18:03:41.203: E/BTL_IFC_WRP(9903): ##### ERROR : wrp_sock_connect: connect failed (Connection refused)#####
09-22 18:03:41.203: E/BTL_IFC(9903): ##### ERROR : btl_ifc_ctrl_connect: control channel failed Connection refused#####
09-22 18:03:41.211: E/MyBluetoothTest(9903): IO fout bij connecten

Lijkt alsof hij niet kan verbinden.

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 20-11 11:59

NMe

Quia Ego Sic Dico.

[google=wrp_sock_connect: connect failed (Connection refused)] lijkt bij de eerste hit al aan kanshebbend antwoord te tonen. ;)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Verwijderd

Topicstarter
Okay,

Het probleem was dat de UUID niet goed was, ik heb nu deze code om hem op te vragen van de device.
code:
1
2
Method m = bt.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
BluetoothSocket btSocket = (BluetoothSocket) m.invoke(bt, 1);


Bedankt jongens.
Pagina: 1