Android bluetooth verbinding maken

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • itcouldbeanyone
  • Registratie: Augustus 2014
  • Laatst online: 12-09 21:49
ik ben bezig om in Android een BLE verbinding te openen voor me werk moet ik een simpele app laten communiceren met een stuk hardware.
nu loop ik tegen Android aan . ik ben zelf geen programmeur, maar heb toch het een en ander al voor elkaar.
ben nou al een tijd bezig, en me geduld begint op te raken.
is hier iemand die alles van Android bluetooth af weet ?

met de onderstaande code krijg ik een melding dat mijn bluetooth aan moet.
Geweldig!.

nu de verbinding nog.
als ik : BluetoothGatt bluetoothGatt = Adevice.connectGatt(this , false, btleGattCallback);
oproep, dan krijg ik errors dat connectGatt does not exsist.
of connectGatt expects (Context, boolean, gattCalback)

ok wat is nou een Context , er is hier weinig duidelijkheid over.


ik heb dit als bron gebruikt
http://toastdroid.com/201...ooth-low-energy-tutorial/

hoe krijg ik nu een verbinding ?
als ik niet connectGatt kan oproepen.


alles is gemaakt in Processing 3.1.1

hier de test code

Java: BLE_test
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 import android.bluetooth.*;//BluetoothAdapter;
import android.content.*;
import android.widget.Toast;
import android.view.Gravity;
import android.app.Fragment;
import android.app.Activity;
import java.util.UUID;
import android.os.Binder;
import android.os.IBinder;
import java.lang.Object; 
//public BluetoothDevice  HAK = null;
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
boolean foundDevice=false; //When this is true, the screen turns green.
boolean ConnectedDevice =false;
int BACKGND=0; //Set the background to BLUE
int RESULT_OK=1;
String Results;
BroadcastReceiver myDiscoverer = new myOwnBroadcastReceiver();
private BluetoothGatt bluetoothGatt;                                               //BluetoothGatt controls the Bluetooth communication link
String MLDP_PRIVATE_SERVICE = "00035b03-58e6-07dd-021a-08123a000300"; //Private service for Microchip MLDP
String MLDP_DATA_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a000301"; //Characteristic for MLDP Data, properties - notify, write
String MLDP_CONTROL_PRIVATE_CHAR = "00035b03-58e6-07dd-021a-08123a0003ff"; //Characteristic for MLDP Control, properties - read, write
String CHARACTERISTIC_NOTIFICATION_CONFIG = "00002902-0000-1000-8000-00805f9b34fb";  //Special UUID for descriptor needed to enable notifications
String BLE_Support = "android.hardware.bluetooth_le";
String MAC = "00:1E:C0:31:02:4B";
final  UUID UUID_MLDP_DATA_PRIVATE_CHARACTERISTIC = UUID.fromString(MLDP_DATA_PRIVATE_CHAR);
final  UUID UUID_CHARACTERISTIC_NOTIFICATION_CONFIG = UUID.fromString(CHARACTERISTIC_NOTIFICATION_CONFIG);
Context T;
void setup(){
 size (200, 200);
  /*IF Bluetooth is NOT enabled, then ask user permission to enable it */
 if (!bluetooth.isEnabled()) {
 Intent requestBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
 startActivityForResult(requestBluetooth, 0);
 }
}

void draw(){
  if(foundDevice==false){
  bluetooth.startLeScan(leScanCallback);
  }
  else{bluetooth.stopLeScan(leScanCallback);}
 if(foundDevice){
 background(10,255,10);
 final BluetoothDevice Adevice = bluetooth.getRemoteDevice(MAC); // MAC is the adress of the device we want to connect
 if(Adevice!=null){
BluetoothGatt bluetoothGatt = Adevice.connectGatt(this , false, btleGattCallback);                //Directly connect to the device so autoConnect is false
 }
  }
}  

public class myOwnBroadcastReceiver extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
  String action=intent.getAction();

 //Notification that BluetoothDevice is FOUND
 if(BluetoothDevice.ACTION_FOUND.equals(action)){
 foundDevice=true; //Change the screen to green
 }
 
 //Notification if bluetooth device is connected
 if(BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)){
 ConnectedDevice=true; //turn screen purple
 }
 //Display the name of the discovered device
 String discoveredDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
println("Discovered: " + discoveredDeviceName);
 
 //Display more information about the discovered device
 BluetoothDevice discoveredDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
println("getAddress() = " + discoveredDevice.getAddress());
println("getName() = " + discoveredDevice.getName());
 
 int bondyState=discoveredDevice.getBondState();
println("getBondState() = " + bondyState);
 
 String mybondState;
 switch(bondyState){
 case 10: mybondState="BOND_NONE";
 break;
 case 11: mybondState="BOND_BONDING";
 break;
 case 12: mybondState="BOND_BONDED";
 break;
 default: mybondState="INVALID BOND STATE";
 break;
 }
println("getBondState() = " + mybondState);
 
 //Change foundDevice to true which will make the screen turn green
 foundDevice=true;
 }
}



public BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
  //@Override
  public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
    println(device);
  //  println(bluetooth.getRemoteDevice(MAC));
  if(device.toString().equals(MAC)){println("HAK found"); foundDevice=true;

}
    
  }
};


final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {

  @Override
  public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
    // this will get called anytime you perform a read or write characteristic operation
  }

  @Override
  public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
    // this will get called when a device connects or disconnects
  }

  @Override
  public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { 
    // this will get called after the client initiates a       BluetoothGatt.discoverServices() call
  }
};




   //     

Ben niet slim, maar wel dom


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

itcouldbeanyone schreef op dinsdag 28 juni 2016 @ 11:35:
ok wat is nou een Context , er is hier weinig duidelijkheid over.
Je maakt een geintje? Een Context is zo'n beetje het ding waar heel je Android-app aan opgehangen is, en je vraag letterlijk in Google stoppen geeft talloze antwoorden. Je zal dus op de plek waar je die connectie wil maken daadwerkelijk de applicatiecontext door moeten geven vanuit je Activity of hem op moeten halen via de vele statische functies die daarvoor bestaan.

'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.


Acties:
  • 0 Henk 'm!

  • itcouldbeanyone
  • Registratie: Augustus 2014
  • Laatst online: 12-09 21:49
NMe schreef op dinsdag 28 juni 2016 @ 11:56:
[...]

Je maakt een geintje? Een Context is zo'n beetje het ding waar heel je Android-app aan opgehangen is, en je vraag letterlijk in Google stoppen geeft talloze antwoorden. Je zal dus op de plek waar je die connectie wil maken daadwerkelijk de applicatiecontext door moeten geven vanuit je Activity of hem op moeten halen via de vele statische functies die daarvoor bestaan.
Ik zoiets wel begrepen maar hoe geef ik de activity door.
this geeft als waarde nu de naam van de sketch.
Binneni. Een class de naam van de class

Ben niet slim, maar wel dom


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

This geeft het huidige object terug, dus da's nogal wiedes. ;) Misschien kun je beter eerst even wat lezen over hoe Java werkt? Je bent nu vrijwel blind aan het programmeren. Da's een beetje als in een auto zonder stuur stappen en vreemd opkijken wanneer je tegen een boom aan rijdt. ;)

Anyway: [google=android how to get application context from other class]

'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.


Acties:
  • 0 Henk 'm!

  • itcouldbeanyone
  • Registratie: Augustus 2014
  • Laatst online: 12-09 21:49
mijn Draw ziet er nu zo uit
Java: BLEtest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 

void draw(){
  if(foundDevice==false){
  bluetooth.startLeScan(leScanCallback);
  }
  else{bluetooth.stopLeScan(leScanCallback);}
 if(foundDevice){
 background(10,255,10);
DeviceScanActivity Connecting =  new DeviceScanActivity();
Connecting.Connect();
  }
}  
class DeviceScanActivity extends ListActivity {
  void Connect(){ 
  final BluetoothDevice Adevice = bluetooth.getRemoteDevice(MAC); // MAC is the adress of the device we want to connect
try{bluetoothGatt = Adevice.connectGatt(this , true, btleGattCallback);    }            //Directly connect to the device so autoConnect is false
catch (Exception e) {println(e.getMessage());}

}
};

 


ik kan nu via Connecting.Connect();
connectGatt oproepen.

de app compiled . maar na het oproepen van deze functie.
de volgende error.

Java: error
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
 
FATAL EXCEPTION: Animation Thread
Process: processing.test.ble_test, PID: 24552
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  at android.os.Handler.<init>(Handler.java:200)
FATAL EXCEPTION: Animation Thread
  at android.os.Handler.<init>(Handler.java:114)
Process: processing.test.ble_test, PID: 24552
  at android.app.Activity.<init>(Activity.java:844)
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  at android.app.ListActivity.<init>(ListActivity.java:175)
  at android.os.Handler.<init>(Handler.java:200)
  at processing.test.ble_test.BLE_TEST$DeviceScanActivity.<init>(BLE_TEST.java:86)
  at android.os.Handler.<init>(Handler.java:114)
  at processing.test.ble_test.BLE_TEST.draw(BLE_TEST.java:82)
  at android.app.Activity.<init>(Activity.java:844)
  at processing.core.PApplet.handleDraw(Unknown Source)
  at android.app.ListActivity.<init>(ListActivity.java:175)
  at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
  at processing.test.ble_test.BLE_TEST$DeviceScanActivity.<init>(BLE_TEST.java:86)
  at processing.core.PApplet.run(Unknown Source)
  at processing.test.ble_test.BLE_TEST.draw(BLE_TEST.java:82)
  at java.lang.Thread.run(Thread.java:818)
  at processing.core.PApplet.handleDraw(Unknown Source)
  at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
  at processing.core.PApplet.run(Unknown Source)
  at java.lang.Thread.run(Thread.java:818)


 

Ben niet slim, maar wel dom


Acties:
  • 0 Henk 'm!

  • armageddon_2k1
  • Registratie: September 2001
  • Laatst online: 27-07 10:18
Ja weet je, vul de enige zinnige Engelse zin "Can't create handler inside thread that has not called Looper.prepare()" eens in in Google en daar zie je al heel veel mensen die tegen hetzelfde aangelopen zijn.

Zoals hierboven al vermeld is, heb je nog geen idee hoe Java werkt. In het gunstigste geval vind je op Google een antwoord en dan klopt je 5 minuten later weer aan met de volgende fatal exception. Wat is je doel? Iets werkend krijgen of iets leren? Moet je het werkend krijgen zou ik voorstellen iemand in te huren. Wil je het allemaal zelf doen ga dan eens terug naar de basis. Je bent nu allemaal verschillende (lastige) concepten aan het combineren (threads, bluetooth, animatie...).

Ik lees dat het voor je werk is. Ben je dan wel de geschikte persoon hiervoor?

Engineering is like Tetris. Succes disappears and errors accumulate.


Acties:
  • 0 Henk 'm!

  • itcouldbeanyone
  • Registratie: Augustus 2014
  • Laatst online: 12-09 21:49
het moet gewoon snel gaan werken.
in de techniek is het vaker een puinzooi en gaan ze niet zomaar mensen in huren.
zelfs na 2 weken klooten.

ik ben geen java man dat weet ik, maar als jij in 5 min de oplossing weet graag.

ik bedenk alleen printplaten, en moet nu ineens dit fixsen.

Ben niet slim, maar wel dom


Acties:
  • +1 Henk 'm!

Verwijderd

Dus omdat je baas niemand wil betalen moeten wij het gratis voor je doen? Ha! :)

Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Zaph hierboven brengt het wat cru maar dat is wel de essentie. Je baas heeft de opdracht aan de verkeerde persoon gegeven. Progammeren is niet een kwestie van links en rechts wat copy/pasten zonder te begrijpen wat je aan het doen bent, en lang niet alles heeft een vijfminutenfix. En in dit geval: zelfs als die fix er wel was loop je twee minuten later tegen een volgend probleem aan. Je bent er veel meer bij gebaat om tegen je baas te zeggen dat dit buiten je expertiseveld ligt en je dit gewoon niet voor hem kan oplossen. Je gaat toch ook niet naar de slager om een brood te kopen?

'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.


Acties:
  • 0 Henk 'm!

  • itcouldbeanyone
  • Registratie: Augustus 2014
  • Laatst online: 12-09 21:49
Het is alleen maar het BLE gedeelte waar ik tegen aan loop
De rest is allemaal al af. De gehele interface met alle canbus instructies heb ik al mooi voor elkaar in processing.
Zodra ik mijn data string heb ben ik klaar.
Dus dat copy pasten is gewoon onzin.
Android is gewoon nieuw voor mij en waarschijnlijk eenmalig.

En ik zeg ook niet dat jullie het moeten doen. Maar een beetje een richting geven zit er tegenwoordig weinig in bij tweakers


tussen deze print en android moet ik dus een commenucatie leggen.

Afbeeldingslocatie: http://daig.co.uk/P1.jpg

[ Voor 11% gewijzigd door itcouldbeanyone op 28-06-2016 20:28 ]

Ben niet slim, maar wel dom


Acties:
  • 0 Henk 'm!

Verwijderd

itcouldbeanyone schreef op dinsdag 28 juni 2016 @ 19:17:
Maar een beetje een richting geven zit er tegenwoordig weinig in bij tweakers
Da's niet heel terecht, he?

Je wilt weten wat een Context is, wat gemakkelijk te vinden is. Je krijgt een foutmelding die je met een beetje zoeken zo de goede richting op helpt.

Maar toch vraag je het hier.

Mensen hier zijn absoluut bereid om te helpen bij problemen, maar vinden wel dat je zelf ook een beetje moeite mag doen - zeker als het een relatief kleine moeite is.

Tweakers is immers geen helpdesk, en niemand hier krijgt betaald voor het beantwoorden van vragen.
Pagina: 1