xref: /btstack/platform/daemon/binding/java/example/com/bluekitchen/GATTClientTest.java (revision 046b44372d25c7bd6fe78e5cf6e5176a1979f437)
18257e5f9SMatthias Ringwald package com.bluekitchen;
28257e5f9SMatthias Ringwald 
38257e5f9SMatthias Ringwald import com.bluekitchen.btstack.BD_ADDR;
48257e5f9SMatthias Ringwald import com.bluekitchen.btstack.BT_UUID;
58257e5f9SMatthias Ringwald import com.bluekitchen.btstack.BTstack;
68257e5f9SMatthias Ringwald import com.bluekitchen.btstack.GATTCharacteristic;
78257e5f9SMatthias Ringwald import com.bluekitchen.btstack.GATTService;
88257e5f9SMatthias Ringwald import com.bluekitchen.btstack.Packet;
98257e5f9SMatthias Ringwald import com.bluekitchen.btstack.PacketHandler;
108257e5f9SMatthias Ringwald import com.bluekitchen.btstack.Util;
118257e5f9SMatthias Ringwald import com.bluekitchen.btstack.event.BTstackEventState;
12045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GAPEventAdvertisingReport;
13045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventCharacteristicQueryResult;
14045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventCharacteristicValueQueryResult;
15045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventNotification;
16045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventQueryComplete;
17045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventServiceQueryResult;
188257e5f9SMatthias Ringwald import com.bluekitchen.btstack.event.HCIEventDisconnectionComplete;
198257e5f9SMatthias Ringwald import com.bluekitchen.btstack.event.HCIEventLEConnectionComplete;
208257e5f9SMatthias Ringwald 
218257e5f9SMatthias Ringwald public class GATTClientTest implements PacketHandler {
228257e5f9SMatthias Ringwald 
238257e5f9SMatthias Ringwald 	private enum STATE {
248257e5f9SMatthias Ringwald 		w4_btstack_working, w4_scan_result, w4_connected, w4_services_complete, w4_characteristic_complete, w4_characteristic_read
258257e5f9SMatthias Ringwald 	, w4_characteristic_write, w4_acc_service_result, w4_acc_enable_characteristic_result, w4_write_acc_enable_result, w4_acc_client_config_characteristic_result, w4_acc_client_config_result,
268257e5f9SMatthias Ringwald 	w4_acc_data, w4_connected_acc, battery_data
278257e5f9SMatthias Ringwald 	};
288257e5f9SMatthias Ringwald 
298257e5f9SMatthias Ringwald 	private BTstack btstack;
308257e5f9SMatthias Ringwald 	private STATE state;
318257e5f9SMatthias Ringwald 	private int testAddrType;
328257e5f9SMatthias Ringwald 	private BD_ADDR testAddr;
338257e5f9SMatthias Ringwald 	private int testHandle;
348257e5f9SMatthias Ringwald 	private GATTService testService;
358257e5f9SMatthias Ringwald 	private GATTCharacteristic testCharacteristic;
368257e5f9SMatthias Ringwald 	private int service_count = 0;
378257e5f9SMatthias Ringwald 	private int characteristic_count = 0;
388257e5f9SMatthias Ringwald 	private int connectionHandle;
398257e5f9SMatthias Ringwald 	private int counter = 0;
408257e5f9SMatthias Ringwald 
418257e5f9SMatthias Ringwald 	private byte[] acc_service_uuid =           new byte[] {(byte)0xf0, 0, (byte)0xaa, (byte)0x10, 4, (byte)0x51, (byte)0x40, 0, (byte)0xb0, 0, 0, 0, 0, 0, 0, 0};
428257e5f9SMatthias Ringwald 	private byte[] acc_chr_client_config_uuid = new byte[] {(byte)0xf0, 0, (byte)0xaa, (byte)0x11, 4, (byte)0x51, (byte)0x40, 0, (byte)0xb0, 0, 0, 0, 0, 0, 0, 0};
438257e5f9SMatthias Ringwald 	private byte[] acc_chr_enable_uuid =        new byte[] {(byte)0xf0, 0, (byte)0xaa, (byte)0x12, 4, (byte)0x51, (byte)0x40, 0, (byte)0xb0, 0, 0, 0, 0, 0, 0, 0};
448257e5f9SMatthias Ringwald 	private byte[] acc_enable = new byte[] {1};
458257e5f9SMatthias Ringwald 	private byte acc_notification = 1;
468257e5f9SMatthias Ringwald 	private GATTService accService;
478257e5f9SMatthias Ringwald 	private GATTCharacteristic enableCharacteristic;
488257e5f9SMatthias Ringwald 	private GATTCharacteristic configCharacteristic;
498257e5f9SMatthias Ringwald 
508257e5f9SMatthias Ringwald 	private GATTService batteryService;
518257e5f9SMatthias Ringwald 	private GATTCharacteristic batteryLevelCharacteristic;
528257e5f9SMatthias Ringwald 	private byte[] battery_level_chr_uuid = new byte[] {0, 0, (byte)0x2a, (byte)0x19, 0, 0, (byte)0x10, 0, (byte)0x80, 0, 0, (byte)0x80, (byte)0x5f, (byte)0x9b, (byte)0x34, (byte)0xfb};
53045013feSMatthias Ringwald 	GATTEventCharacteristicValueQueryResult battery;
548257e5f9SMatthias Ringwald 
uuid128(byte[] att_uuid)558257e5f9SMatthias Ringwald 	private BT_UUID uuid128(byte[] att_uuid) {
568257e5f9SMatthias Ringwald 		byte [] uuid = new byte[16];
578257e5f9SMatthias Ringwald 		Util.flipX(att_uuid, uuid);
588257e5f9SMatthias Ringwald 		return new BT_UUID(uuid);
598257e5f9SMatthias Ringwald 	}
608257e5f9SMatthias Ringwald 
handlePacket(Packet packet)618257e5f9SMatthias Ringwald 	public void handlePacket(Packet packet){
628257e5f9SMatthias Ringwald 		if (packet instanceof HCIEventDisconnectionComplete){
638257e5f9SMatthias Ringwald 			System.out.println(String.format("Received dissconnect, restart scannning."));
648257e5f9SMatthias Ringwald 			state = STATE.w4_scan_result;
658257e5f9SMatthias Ringwald 			btstack.GAPLEScanStart();
668257e5f9SMatthias Ringwald 			return;
678257e5f9SMatthias Ringwald 		}
688257e5f9SMatthias Ringwald 
69045013feSMatthias Ringwald 		if (packet instanceof GATTEventQueryComplete){
70045013feSMatthias Ringwald 			GATTEventQueryComplete event = (GATTEventQueryComplete) packet;
718257e5f9SMatthias Ringwald 			System.out.println(testAddr + " battery data");
72*046b4437SMilanka Ringwald 			if (event.getATTStatus() != 0){
738257e5f9SMatthias Ringwald 				System.out.println("Battery data could not be read.\nRestart scanning.");
748257e5f9SMatthias Ringwald 				state = STATE.w4_scan_result;
758257e5f9SMatthias Ringwald 				btstack.GAPLEScanStart();
768257e5f9SMatthias Ringwald 				return;
778257e5f9SMatthias Ringwald 			}
788257e5f9SMatthias Ringwald 		}
798257e5f9SMatthias Ringwald 
808257e5f9SMatthias Ringwald 		switch (state){
818257e5f9SMatthias Ringwald 			case w4_btstack_working:
828257e5f9SMatthias Ringwald 				if (packet instanceof BTstackEventState){
838257e5f9SMatthias Ringwald 					BTstackEventState event = (BTstackEventState) packet;
848257e5f9SMatthias Ringwald 					if (event.getState() == 2)	{
858257e5f9SMatthias Ringwald 						System.out.println("BTstack working, start scanning.");
868257e5f9SMatthias Ringwald 						state = STATE.w4_scan_result;
878257e5f9SMatthias Ringwald 						btstack.GAPLEScanStart();
888257e5f9SMatthias Ringwald 					}
898257e5f9SMatthias Ringwald 				}
908257e5f9SMatthias Ringwald 				break;
918257e5f9SMatthias Ringwald 			case w4_scan_result:
92045013feSMatthias Ringwald 				if (packet instanceof GAPEventAdvertisingReport){
938257e5f9SMatthias Ringwald 					// Advertisement received. Connect to the found BT address.
94045013feSMatthias Ringwald 					GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
958257e5f9SMatthias Ringwald 					testAddrType = report.getAddressType();
968257e5f9SMatthias Ringwald 					testAddr = report.getAddress();
978257e5f9SMatthias Ringwald 					System.out.println(String.format("Adv: type %d, addr %s\ndata: %s \n Stop scan, initiate connect.", testAddrType, testAddr, Util.asHexdump(report.getData())));
988257e5f9SMatthias Ringwald 					btstack.GAPLEScanStop();
998257e5f9SMatthias Ringwald 					state = STATE.w4_connected;
1008257e5f9SMatthias Ringwald 					btstack.GAPLEConnect(testAddrType, testAddr);
1018257e5f9SMatthias Ringwald 				}
1028257e5f9SMatthias Ringwald 				break;
1038257e5f9SMatthias Ringwald 			case w4_connected:
1048257e5f9SMatthias Ringwald 				if (packet instanceof HCIEventLEConnectionComplete){
1058257e5f9SMatthias Ringwald 					HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
1068257e5f9SMatthias Ringwald 					if (event.getStatus() != 0) {
1078257e5f9SMatthias Ringwald 						System.out.println(testAddr + String.format(" - connection failed, status %d.\nRestart scanning.", event.getStatus()));
1088257e5f9SMatthias Ringwald 						state = STATE.w4_scan_result;
1098257e5f9SMatthias Ringwald 						btstack.GAPLEScanStart();
1108257e5f9SMatthias Ringwald 						break;
1118257e5f9SMatthias Ringwald 					}
1128257e5f9SMatthias Ringwald 
1138257e5f9SMatthias Ringwald 					// Query battery service.
1148257e5f9SMatthias Ringwald 					state = STATE.w4_services_complete;
1158257e5f9SMatthias Ringwald 					connectionHandle = event.getConnectionHandle();
1168257e5f9SMatthias Ringwald 					System.out.println(testAddr + String.format(" - connected %x.\nQuery battery service.", connectionHandle));
1178257e5f9SMatthias Ringwald 					btstack.GATTDiscoverPrimaryServicesByUUID16(connectionHandle, 0x180f);
1188257e5f9SMatthias Ringwald 				}
1198257e5f9SMatthias Ringwald 				break;
1208257e5f9SMatthias Ringwald 			case w4_services_complete:
121045013feSMatthias Ringwald 				if (packet instanceof GATTEventServiceQueryResult){
122045013feSMatthias Ringwald 					// Store battery service. Wait for GATTEventQueryComplete event to send next GATT command.
123045013feSMatthias Ringwald 					GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
1248257e5f9SMatthias Ringwald 					System.out.println(testAddr + String.format(" - battery service %s", event.getService().getUUID()));
1258257e5f9SMatthias Ringwald 					batteryService = event.getService();
1268257e5f9SMatthias Ringwald 					break;
1278257e5f9SMatthias Ringwald 				}
128045013feSMatthias Ringwald 				if (packet instanceof GATTEventQueryComplete){
1298257e5f9SMatthias Ringwald 					// Check if battery service is found.
1308257e5f9SMatthias Ringwald 					if (batteryService == null) {
1318257e5f9SMatthias Ringwald 						System.out.println(testAddr + " - no battery service. \nRestart scanning.");
1328257e5f9SMatthias Ringwald 						state = STATE.w4_scan_result;
1338257e5f9SMatthias Ringwald 						btstack.GAPLEScanStart();
1348257e5f9SMatthias Ringwald 						break;
1358257e5f9SMatthias Ringwald 					}
1368257e5f9SMatthias Ringwald 					System.out.println(testAddr + " - query battery level.");
1378257e5f9SMatthias Ringwald 					state = STATE.w4_characteristic_complete;
1388257e5f9SMatthias Ringwald 					btstack.GATTDiscoverCharacteristicsForServiceByUUID128(connectionHandle, batteryService, uuid128(this.battery_level_chr_uuid));
1398257e5f9SMatthias Ringwald 				}
1408257e5f9SMatthias Ringwald 				break;
1418257e5f9SMatthias Ringwald 			case w4_characteristic_complete:
142045013feSMatthias Ringwald 				if (packet instanceof GATTEventCharacteristicQueryResult){
143045013feSMatthias Ringwald 					// Store battery level characteristic. Wait for GATTEventQueryComplete event to send next GATT command.
144045013feSMatthias Ringwald 					GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
1458257e5f9SMatthias Ringwald 					batteryLevelCharacteristic = event.getCharacteristic();
1468257e5f9SMatthias Ringwald 					System.out.println(testAddr + " - battery level found.");
1478257e5f9SMatthias Ringwald 					break;
1488257e5f9SMatthias Ringwald 				}
1498257e5f9SMatthias Ringwald 
150045013feSMatthias Ringwald 				if (!(packet instanceof GATTEventQueryComplete)) break;
1518257e5f9SMatthias Ringwald 				if (batteryLevelCharacteristic == null) {
1528257e5f9SMatthias Ringwald 					System.out.println("No battery level characteristic found");
1538257e5f9SMatthias Ringwald 					break;
1548257e5f9SMatthias Ringwald 				}
1558257e5f9SMatthias Ringwald 				System.out.println(testAddr + " - polling battery.");
1568257e5f9SMatthias Ringwald 				counter = 0;
1578257e5f9SMatthias Ringwald 				state = STATE.battery_data;
1588257e5f9SMatthias Ringwald 				new Thread(new Runnable(){
1598257e5f9SMatthias Ringwald 					@Override
1608257e5f9SMatthias Ringwald 					public void run() {
1618257e5f9SMatthias Ringwald 						try {
1628257e5f9SMatthias Ringwald 							while(state == STATE.battery_data){
1638257e5f9SMatthias Ringwald 								Thread.sleep(5000);
1648257e5f9SMatthias Ringwald 								btstack.GATTReadValueOfCharacteristic(connectionHandle, batteryLevelCharacteristic);
1658257e5f9SMatthias Ringwald 							}
1668257e5f9SMatthias Ringwald 						} catch (InterruptedException e) {}
1678257e5f9SMatthias Ringwald 					}
1688257e5f9SMatthias Ringwald 				}).start();
1698257e5f9SMatthias Ringwald 				break;
1708257e5f9SMatthias Ringwald 			case battery_data:
171045013feSMatthias Ringwald 				if (packet instanceof GATTEventCharacteristicValueQueryResult){
172045013feSMatthias Ringwald 					GATTEventCharacteristicValueQueryResult battery = (GATTEventCharacteristicValueQueryResult) packet;
1738257e5f9SMatthias Ringwald 
1748257e5f9SMatthias Ringwald 					if (battery.getValueLength() != 1) break;
1758257e5f9SMatthias Ringwald 					byte[] data = battery.getValue();
1768257e5f9SMatthias Ringwald 					counter = counter + 1;
1778257e5f9SMatthias Ringwald 					System.out.println(String.format("Counter %d, battery level: %d", counter, data[0]) + "%");
1788257e5f9SMatthias Ringwald 					break;
1798257e5f9SMatthias Ringwald 
1808257e5f9SMatthias Ringwald 				}
1818257e5f9SMatthias Ringwald 				break;
1828257e5f9SMatthias Ringwald 			default:
1838257e5f9SMatthias Ringwald 				break;
1848257e5f9SMatthias Ringwald 		}
1858257e5f9SMatthias Ringwald 	}
1868257e5f9SMatthias Ringwald 
handlePacketAcc(Packet packet)1878257e5f9SMatthias Ringwald 	public void handlePacketAcc(Packet packet){
1888257e5f9SMatthias Ringwald 
1898257e5f9SMatthias Ringwald //		System.out.println(packet.toString());
1908257e5f9SMatthias Ringwald 		if (packet instanceof HCIEventDisconnectionComplete){
1918257e5f9SMatthias Ringwald 			HCIEventDisconnectionComplete event = (HCIEventDisconnectionComplete) packet;
1928257e5f9SMatthias Ringwald 			testHandle = event.getConnectionHandle();
1938257e5f9SMatthias Ringwald 			System.out.println(String.format("Received disconnect, status %d, handle %x", event.getStatus(), testHandle));
1948257e5f9SMatthias Ringwald 			return;
1958257e5f9SMatthias Ringwald 		}
1968257e5f9SMatthias Ringwald 
1978257e5f9SMatthias Ringwald 		switch (state){
1988257e5f9SMatthias Ringwald 		case w4_btstack_working:
1998257e5f9SMatthias Ringwald 			if (packet instanceof BTstackEventState){
2008257e5f9SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
2018257e5f9SMatthias Ringwald 				if (event.getState() == 2)	{
2028257e5f9SMatthias Ringwald 
2038257e5f9SMatthias Ringwald 					System.out.println("GAPLEScanStart()");
2048257e5f9SMatthias Ringwald 					state = STATE.w4_scan_result;
2058257e5f9SMatthias Ringwald 					btstack.GAPLEScanStart();
2068257e5f9SMatthias Ringwald 				}
2078257e5f9SMatthias Ringwald 			}
2088257e5f9SMatthias Ringwald 			break;
2098257e5f9SMatthias Ringwald 		case w4_scan_result:
210045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
211045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
2128257e5f9SMatthias Ringwald 				testAddrType = report.getAddressType();
2138257e5f9SMatthias Ringwald 				testAddr = report.getAddress();
2148257e5f9SMatthias Ringwald 				System.out.println(String.format("Adv: type %d, addr %s", testAddrType, testAddr));
2158257e5f9SMatthias Ringwald 				System.out.println(String.format("Data: %s", Util.asHexdump(report.getData())));
2168257e5f9SMatthias Ringwald 				System.out.println("GAPLEScanStop()");
2178257e5f9SMatthias Ringwald 				btstack.GAPLEScanStop();
2188257e5f9SMatthias Ringwald 				System.out.println("GAPLEConnect(...)");
2198257e5f9SMatthias Ringwald 				state = STATE.w4_connected_acc;
2208257e5f9SMatthias Ringwald 				btstack.GAPLEConnect(testAddrType, testAddr);
2218257e5f9SMatthias Ringwald 			}
2228257e5f9SMatthias Ringwald 			break;
2238257e5f9SMatthias Ringwald 
2248257e5f9SMatthias Ringwald 		case w4_connected:
2258257e5f9SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
2268257e5f9SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
2278257e5f9SMatthias Ringwald 				testHandle = event.getConnectionHandle();
2288257e5f9SMatthias Ringwald 				System.out.println(String.format("Connection complete, status %d, handle %x", event.getStatus(), testHandle));
2298257e5f9SMatthias Ringwald 				state = STATE.w4_services_complete;
2308257e5f9SMatthias Ringwald 				System.out.println("GATTDiscoverPrimaryServices(...)");
2318257e5f9SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServices(testHandle);
2328257e5f9SMatthias Ringwald 			}
2338257e5f9SMatthias Ringwald 			break;
2348257e5f9SMatthias Ringwald 		case w4_services_complete:
235045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
236045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
2378257e5f9SMatthias Ringwald 				if (testService == null){
2388257e5f9SMatthias Ringwald 					System.out.println(String.format("First service UUID %s", event.getService().getUUID()));
2398257e5f9SMatthias Ringwald 					testService = event.getService();
2408257e5f9SMatthias Ringwald 				}
2418257e5f9SMatthias Ringwald 				System.out.println("Service: " + event.getService());
2428257e5f9SMatthias Ringwald 				service_count++;
2438257e5f9SMatthias Ringwald 			}
244045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2458257e5f9SMatthias Ringwald 				System.out.println(String.format("Service query complete, total %d services", service_count));
2468257e5f9SMatthias Ringwald 				state = STATE.w4_characteristic_complete;
2478257e5f9SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForService(testHandle, testService);
2488257e5f9SMatthias Ringwald 			}
2498257e5f9SMatthias Ringwald 			break;
2508257e5f9SMatthias Ringwald 
2518257e5f9SMatthias Ringwald 		case w4_characteristic_complete:
252045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
253045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
2548257e5f9SMatthias Ringwald 				if (testCharacteristic == null){
2558257e5f9SMatthias Ringwald 					System.out.println(String.format("First characteristic UUID %s", event.getCharacteristic().getUUID()));
2568257e5f9SMatthias Ringwald 					testCharacteristic = event.getCharacteristic();
2578257e5f9SMatthias Ringwald 				}
2588257e5f9SMatthias Ringwald 				System.out.println("Characteristic: " + event.getCharacteristic());
2598257e5f9SMatthias Ringwald 				characteristic_count++;
2608257e5f9SMatthias Ringwald 			}
261045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2628257e5f9SMatthias Ringwald 				System.out.println(String.format("Characteristic query complete, total %d characteristics", characteristic_count));
2638257e5f9SMatthias Ringwald 				state = STATE.w4_characteristic_read;
2648257e5f9SMatthias Ringwald 				btstack.GATTReadValueOfCharacteristic(testHandle, testCharacteristic);
2658257e5f9SMatthias Ringwald 			}
2668257e5f9SMatthias Ringwald 			break;
2678257e5f9SMatthias Ringwald 
2688257e5f9SMatthias Ringwald 		case w4_characteristic_read:
269045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicValueQueryResult){
2708257e5f9SMatthias Ringwald 				System.out.println("Read complete");
2718257e5f9SMatthias Ringwald 				System.out.println( packet.toString());
2728257e5f9SMatthias Ringwald 				state = STATE.w4_characteristic_write;
2738257e5f9SMatthias Ringwald 				byte [] data = { 'B', 'T', 's', 't', 'a', 'c', 'k'};
2748257e5f9SMatthias Ringwald 				btstack.GATTWriteValueOfCharacteristic(testHandle, testCharacteristic, data.length, data);
2758257e5f9SMatthias Ringwald 			}
2768257e5f9SMatthias Ringwald 			break;
2778257e5f9SMatthias Ringwald 		case w4_characteristic_write:
278045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2798257e5f9SMatthias Ringwald 				System.out.println("Write complete, search for ACC service");
2808257e5f9SMatthias Ringwald 				state = STATE.w4_acc_service_result;
2818257e5f9SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServicesByUUID128(testHandle, new BT_UUID(this.acc_service_uuid)); // not working
2828257e5f9SMatthias Ringwald 			}
2838257e5f9SMatthias Ringwald 			break;
2848257e5f9SMatthias Ringwald 
2858257e5f9SMatthias Ringwald 		case w4_connected_acc:
2868257e5f9SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
2878257e5f9SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
2888257e5f9SMatthias Ringwald 				testHandle = event.getConnectionHandle();
2898257e5f9SMatthias Ringwald 				System.out.println(String.format("Connection complete, status %d, handle %x", event.getStatus(), testHandle));
2908257e5f9SMatthias Ringwald 				System.out.println("Search for ACC service");
2918257e5f9SMatthias Ringwald 				state = STATE.w4_acc_service_result;
2928257e5f9SMatthias Ringwald 				byte [] uuid = new byte[16];
2938257e5f9SMatthias Ringwald 				Util.flipX(this.acc_service_uuid, uuid);	// works
2948257e5f9SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServicesByUUID128(testHandle, new BT_UUID(uuid));
2958257e5f9SMatthias Ringwald 			}
2968257e5f9SMatthias Ringwald 			break;
2978257e5f9SMatthias Ringwald 
2988257e5f9SMatthias Ringwald 		 case w4_acc_service_result:
2998257e5f9SMatthias Ringwald 			System.out.println(String.format("w4_acc_service_result state"));
300045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
301045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
3028257e5f9SMatthias Ringwald 				System.out.println(String.format("ACC service found %s", event.getService().getUUID()));
3038257e5f9SMatthias Ringwald 				accService = event.getService();
3048257e5f9SMatthias Ringwald 				break;
3058257e5f9SMatthias Ringwald 			}
306045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3078257e5f9SMatthias Ringwald 				if (accService == null) {
3088257e5f9SMatthias Ringwald 					System.out.println("No acc service found");
3098257e5f9SMatthias Ringwald 					break;
3108257e5f9SMatthias Ringwald 				}
3118257e5f9SMatthias Ringwald 				System.out.println("ACC Service found, searching for acc enable characteristic");
3128257e5f9SMatthias Ringwald 				state = STATE.w4_acc_enable_characteristic_result;
3138257e5f9SMatthias Ringwald 				byte [] uuid = new byte[16];
3148257e5f9SMatthias Ringwald 				Util.flipX(this.acc_chr_enable_uuid, uuid);
3158257e5f9SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForServiceByUUID128(testHandle, accService, new BT_UUID(uuid));
3168257e5f9SMatthias Ringwald 			}
3178257e5f9SMatthias Ringwald 			break;
3188257e5f9SMatthias Ringwald 
3198257e5f9SMatthias Ringwald 		case w4_acc_enable_characteristic_result:
320045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
321045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
3228257e5f9SMatthias Ringwald 				enableCharacteristic = event.getCharacteristic();
3238257e5f9SMatthias Ringwald 				System.out.println("Enable ACC Characteristic found ");
3248257e5f9SMatthias Ringwald 			}
325045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3268257e5f9SMatthias Ringwald 				if (enableCharacteristic == null) {
3278257e5f9SMatthias Ringwald 					System.out.println("No acc enable chr found");
3288257e5f9SMatthias Ringwald 					break;
3298257e5f9SMatthias Ringwald 				}
3308257e5f9SMatthias Ringwald 				System.out.println("Write enable acc characteristic");
3318257e5f9SMatthias Ringwald 				state = STATE.w4_write_acc_enable_result;
3328257e5f9SMatthias Ringwald 				btstack.GATTWriteValueOfCharacteristic(testHandle, enableCharacteristic, 1, this.acc_enable);
3338257e5f9SMatthias Ringwald 			}
3348257e5f9SMatthias Ringwald 			break;
3358257e5f9SMatthias Ringwald 		case w4_write_acc_enable_result:
336045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3378257e5f9SMatthias Ringwald 				System.out.println("Acc enabled,searching for acc client config characteristic");
3388257e5f9SMatthias Ringwald 				byte [] uuid = new byte[16];
3398257e5f9SMatthias Ringwald 				Util.flipX(this.acc_chr_client_config_uuid, uuid);
3408257e5f9SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForServiceByUUID128(testHandle, accService, new BT_UUID(uuid));
3418257e5f9SMatthias Ringwald 				state = STATE.w4_acc_client_config_characteristic_result;
3428257e5f9SMatthias Ringwald 			}
3438257e5f9SMatthias Ringwald 			break;
3448257e5f9SMatthias Ringwald 
3458257e5f9SMatthias Ringwald 		case w4_acc_client_config_characteristic_result:
346045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
347045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
3488257e5f9SMatthias Ringwald 				configCharacteristic = event.getCharacteristic();
3498257e5f9SMatthias Ringwald 				System.out.println("ACC Client Config Characteristic found");
3508257e5f9SMatthias Ringwald 			}
351045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3528257e5f9SMatthias Ringwald 				if (configCharacteristic == null) {
3538257e5f9SMatthias Ringwald 					System.out.println("No acc config chr found");
3548257e5f9SMatthias Ringwald 					break;
3558257e5f9SMatthias Ringwald 				}
3568257e5f9SMatthias Ringwald 				System.out.println("Write ACC Client Config Characteristic");
3578257e5f9SMatthias Ringwald 				state = STATE.w4_acc_data;
3588257e5f9SMatthias Ringwald 				btstack.GATTWriteClientCharacteristicConfiguration(testHandle, configCharacteristic, this.acc_notification);
3598257e5f9SMatthias Ringwald 			}
3608257e5f9SMatthias Ringwald 			break;
3618257e5f9SMatthias Ringwald 
3628257e5f9SMatthias Ringwald 		case w4_acc_data:
363045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3648257e5f9SMatthias Ringwald 				System.out.println("Acc configured for notification");
3658257e5f9SMatthias Ringwald 				break;
3668257e5f9SMatthias Ringwald 			}
3678257e5f9SMatthias Ringwald 
368045013feSMatthias Ringwald 			if (packet instanceof GATTEventNotification){
3698257e5f9SMatthias Ringwald 				System.out.println("Acc Value");
3708257e5f9SMatthias Ringwald 				System.out.println(packet.toString());
3718257e5f9SMatthias Ringwald 				btstack.GAPDisconnect(testHandle);
3728257e5f9SMatthias Ringwald 			}
3738257e5f9SMatthias Ringwald 
3748257e5f9SMatthias Ringwald 		default:
3758257e5f9SMatthias Ringwald 			break;
3768257e5f9SMatthias Ringwald 		}
3778257e5f9SMatthias Ringwald 	}
3788257e5f9SMatthias Ringwald 
test()3798257e5f9SMatthias Ringwald 	void test(){
3808257e5f9SMatthias Ringwald 
3818257e5f9SMatthias Ringwald 		System.out.println("LE Test Application");
3828257e5f9SMatthias Ringwald 
3838257e5f9SMatthias Ringwald 		// connect to BTstack Daemon via default port on localhost
3848257e5f9SMatthias Ringwald 		btstack = new BTstack();
3858257e5f9SMatthias Ringwald 		btstack.setTcpPort(BTstack.DEFAULT_TCP_PORT);
3868257e5f9SMatthias Ringwald 		btstack.registerPacketHandler(this);
3878257e5f9SMatthias Ringwald 		boolean ok = btstack.connect();
3888257e5f9SMatthias Ringwald 		if (!ok) {
3898257e5f9SMatthias Ringwald 			System.out.println("Failed to connect to BTstack Server");
3908257e5f9SMatthias Ringwald 			return;
3918257e5f9SMatthias Ringwald 		}
3928257e5f9SMatthias Ringwald 
3938257e5f9SMatthias Ringwald 		System.out.println("BTstackSetPowerMode(1)");
3948257e5f9SMatthias Ringwald 
3958257e5f9SMatthias Ringwald 		state = STATE.w4_btstack_working;
3968257e5f9SMatthias Ringwald 		btstack.BTstackSetPowerMode(1);
3978257e5f9SMatthias Ringwald 	}
3988257e5f9SMatthias Ringwald 
main(String args[])3998257e5f9SMatthias Ringwald 	public static void main(String args[]){
4008257e5f9SMatthias Ringwald 		new GATTClientTest().test();
4018257e5f9SMatthias Ringwald 	}
4028257e5f9SMatthias Ringwald }
403