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