xref: /btstack/port/mtk/LEScan/src/com/bluekitchen/lescan/MainActivity.java (revision 045013fe998d7ba668a6fda194a274adcb4b68a7)
18caefee3SMatthias Ringwald package com.bluekitchen.lescan;
28caefee3SMatthias Ringwald 
38caefee3SMatthias Ringwald import android.app.Activity;
48caefee3SMatthias Ringwald import android.os.Bundle;
58caefee3SMatthias Ringwald import android.util.Log;
68caefee3SMatthias Ringwald import android.view.Menu;
78caefee3SMatthias Ringwald import android.widget.TextView;
88caefee3SMatthias Ringwald 
98caefee3SMatthias Ringwald import com.bluekitchen.btstack.BD_ADDR;
108caefee3SMatthias Ringwald import com.bluekitchen.btstack.BT_UUID;
118caefee3SMatthias Ringwald import com.bluekitchen.btstack.BTstack;
128caefee3SMatthias Ringwald import com.bluekitchen.btstack.GATTCharacteristic;
138caefee3SMatthias Ringwald import com.bluekitchen.btstack.GATTService;
148caefee3SMatthias Ringwald import com.bluekitchen.btstack.Packet;
158caefee3SMatthias Ringwald import com.bluekitchen.btstack.PacketHandler;
168caefee3SMatthias Ringwald import com.bluekitchen.btstack.Util;
178caefee3SMatthias Ringwald import com.bluekitchen.btstack.event.BTstackEventState;
18*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GAPEventAdvertisingReport;
19*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventCharacteristicQueryResult;
20*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventCharacteristicValueQueryResult;
21*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventNotification;
22*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventQueryComplete;
23*045013feSMatthias Ringwald import com.bluekitchen.btstack.event.GATTEventServiceQueryResult;
248caefee3SMatthias Ringwald import com.bluekitchen.btstack.event.HCIEventCommandComplete;
258caefee3SMatthias Ringwald import com.bluekitchen.btstack.event.HCIEventDisconnectionComplete;
268caefee3SMatthias Ringwald import com.bluekitchen.btstack.event.HCIEventLEConnectionComplete;
278caefee3SMatthias Ringwald 
288caefee3SMatthias Ringwald public class MainActivity extends Activity implements PacketHandler {
298caefee3SMatthias Ringwald 
308caefee3SMatthias Ringwald 	private static final String BTSTACK_TAG = "BTstack";
318caefee3SMatthias Ringwald 
328caefee3SMatthias Ringwald 	private enum STATE {
338caefee3SMatthias Ringwald 		w4_btstack_working, w4_scan_result, w4_connected, w4_services_complete, w4_characteristic_complete, w4_characteristic_read
348caefee3SMatthias 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,
358caefee3SMatthias Ringwald 		w4_acc_data, w4_connected_acc, w4_disconnect, track_rssi, battery_data
368caefee3SMatthias Ringwald 	};
378caefee3SMatthias Ringwald 
388caefee3SMatthias Ringwald 	private TextView tv;
398caefee3SMatthias Ringwald 	private BTstack btstack;
408caefee3SMatthias Ringwald 	private STATE state;
418caefee3SMatthias Ringwald 	private int testAddrType;
428caefee3SMatthias Ringwald 	private BD_ADDR deviceAddr;
438caefee3SMatthias Ringwald 	private int connectionHandle;
448caefee3SMatthias Ringwald 	private GATTService testService;
458caefee3SMatthias Ringwald 	private GATTCharacteristic testCharacteristic;
468caefee3SMatthias Ringwald 	private int service_count = 0;
478caefee3SMatthias Ringwald 	private int characteristic_count = 0;
488caefee3SMatthias Ringwald 	private int test_run_count = 0;
498caefee3SMatthias Ringwald 	private String onScreenMessage = "";
508caefee3SMatthias Ringwald 
518caefee3SMatthias Ringwald 	BD_ADDR sensor_tag_addr = new BD_ADDR("1C:BA:8C:20:C7:F6");
528caefee3SMatthias Ringwald 	// Accelerometer
538caefee3SMatthias 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};
548caefee3SMatthias 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};
558caefee3SMatthias 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};
568caefee3SMatthias Ringwald 	private byte[] acc_enable = new byte[] {1};
578caefee3SMatthias Ringwald 	private byte acc_notification = 1;
588caefee3SMatthias Ringwald 	private GATTService accService;
598caefee3SMatthias Ringwald 	private GATTCharacteristic enableCharacteristic;
608caefee3SMatthias Ringwald 	private GATTCharacteristic configCharacteristic;
618caefee3SMatthias Ringwald 
628caefee3SMatthias Ringwald 	// Battery
638caefee3SMatthias Ringwald 	private GATTService batteryService;
648caefee3SMatthias Ringwald 	private GATTCharacteristic batteryLevelCharacteristic;
658caefee3SMatthias Ringwald 	//private byte[] battery_service_uuid =   new byte[] {0, 0, (byte)0x18, (byte)0x0F, 0, 0, (byte)0x10, 0, (byte)0x80, 0, 0, (byte)0x80, (byte)0x5f, (byte)0x9b, (byte)0x34, (byte)0xfb};
668caefee3SMatthias Ringwald 	private byte[] battery_level_chr_uuid = new byte[] {0, 0, (byte)0x2a, (byte)0x1b, 0, 0, (byte)0x10, 0, (byte)0x80, 0, 0, (byte)0x80, (byte)0x5f, (byte)0x9b, (byte)0x34, (byte)0xfb};
67*045013feSMatthias Ringwald 	GATTEventCharacteristicValueQueryResult battery;
688caefee3SMatthias Ringwald 	private int batteryLevel = 0;
698caefee3SMatthias Ringwald 	private int counter;
708caefee3SMatthias Ringwald 
718caefee3SMatthias Ringwald 
728caefee3SMatthias Ringwald 	@Override
onCreate(Bundle savedInstanceState)738caefee3SMatthias Ringwald 	protected void onCreate(Bundle savedInstanceState) {
748caefee3SMatthias Ringwald 		super.onCreate(savedInstanceState);
758caefee3SMatthias Ringwald 		setContentView(R.layout.activity_main);
768caefee3SMatthias Ringwald 
778caefee3SMatthias Ringwald 		tv = new TextView(this);
788caefee3SMatthias Ringwald 		setContentView(tv);
798caefee3SMatthias Ringwald 
808caefee3SMatthias Ringwald 		test();
818caefee3SMatthias Ringwald 	}
828caefee3SMatthias Ringwald 
addMessage(final String message)838caefee3SMatthias Ringwald 	void addMessage(final String message){
848caefee3SMatthias Ringwald 		onScreenMessage = onScreenMessage + "\n" + message;
858caefee3SMatthias Ringwald 		Log.d(BTSTACK_TAG, message);
868caefee3SMatthias Ringwald 		runOnUiThread(new Runnable(){
878caefee3SMatthias Ringwald 			public void run(){
888caefee3SMatthias Ringwald 				tv.setText(onScreenMessage);
898caefee3SMatthias Ringwald 			}
908caefee3SMatthias Ringwald 		});
918caefee3SMatthias Ringwald 	}
928caefee3SMatthias Ringwald 
938caefee3SMatthias Ringwald 
addTempMessage(final String message)948caefee3SMatthias Ringwald 	void addTempMessage(final String message){
958caefee3SMatthias Ringwald 		Log.d(BTSTACK_TAG, message);
968caefee3SMatthias Ringwald 		runOnUiThread(new Runnable(){
978caefee3SMatthias Ringwald 			public void run(){
988caefee3SMatthias Ringwald 				tv.setText(onScreenMessage +"\n" + message);
998caefee3SMatthias Ringwald 			}
1008caefee3SMatthias Ringwald 		});
1018caefee3SMatthias Ringwald 	}
1028caefee3SMatthias Ringwald 
clearMessages()1038caefee3SMatthias Ringwald 	void clearMessages(){
1048caefee3SMatthias Ringwald 		onScreenMessage = "";
1058caefee3SMatthias Ringwald 		runOnUiThread(new Runnable(){
1068caefee3SMatthias Ringwald 			public void run(){
1078caefee3SMatthias Ringwald 				tv.setText(onScreenMessage);
1088caefee3SMatthias Ringwald 			}
1098caefee3SMatthias Ringwald 		});
1108caefee3SMatthias Ringwald 	}
1118caefee3SMatthias Ringwald 
1128caefee3SMatthias Ringwald 
1138caefee3SMatthias Ringwald 	@Override
onCreateOptionsMenu(Menu menu)1148caefee3SMatthias Ringwald 	public boolean onCreateOptionsMenu(Menu menu) {
1158caefee3SMatthias Ringwald 		// Inflate the menu; this adds items to the action bar if it is present.
1168caefee3SMatthias Ringwald 		getMenuInflater().inflate(R.menu.main, menu);
1178caefee3SMatthias Ringwald 		return true;
1188caefee3SMatthias Ringwald 	}
1198caefee3SMatthias Ringwald 
1208caefee3SMatthias Ringwald 
testCharacteristics(Packet packet)1218caefee3SMatthias Ringwald 	public void testCharacteristics(Packet packet){
1228caefee3SMatthias Ringwald 
1238caefee3SMatthias Ringwald 		switch (state){
1248caefee3SMatthias Ringwald 		case w4_btstack_working:
1258caefee3SMatthias Ringwald 			if (packet instanceof BTstackEventState){
1268caefee3SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
1278caefee3SMatthias Ringwald 				if (event.getState() == 2)	{
1288caefee3SMatthias Ringwald 					addMessage("GAPLEScanStart()");
1298caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
1308caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
1318caefee3SMatthias Ringwald 				}
1328caefee3SMatthias Ringwald 			}
1338caefee3SMatthias Ringwald 			break;
1348caefee3SMatthias Ringwald 		case w4_scan_result:
135*045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
136*045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
1378caefee3SMatthias Ringwald 				testAddrType = report.getAddressType();
1388caefee3SMatthias Ringwald 				deviceAddr = report.getAddress();
1398caefee3SMatthias Ringwald 				addMessage(String.format("Adv: type %d, addr %s", testAddrType, deviceAddr));
1408caefee3SMatthias Ringwald 				addMessage(String.format("Data: %s", Util.asHexdump(report.getData())));
1418caefee3SMatthias Ringwald 				addMessage("GAPLEScanStop()");
1428caefee3SMatthias Ringwald 				btstack.GAPLEScanStop();
1438caefee3SMatthias Ringwald 				addMessage("GAPLEConnect(...)");
1448caefee3SMatthias Ringwald 				state = STATE.w4_connected;
1458caefee3SMatthias Ringwald 				btstack.GAPLEConnect(testAddrType, deviceAddr);
1468caefee3SMatthias Ringwald 			}
1478caefee3SMatthias Ringwald 			break;
1488caefee3SMatthias Ringwald 		case w4_connected:
1498caefee3SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
1508caefee3SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
1518caefee3SMatthias Ringwald 				connectionHandle = event.getConnectionHandle();
1528caefee3SMatthias Ringwald 				addMessage(String.format("Connection complete, status %d, handle %x", event.getStatus(), connectionHandle));
1538caefee3SMatthias Ringwald 				state = STATE.w4_services_complete;
1548caefee3SMatthias Ringwald 				addMessage("GATTDiscoverPrimaryServices(...)");
1558caefee3SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServices(connectionHandle);
1568caefee3SMatthias Ringwald 			}
1578caefee3SMatthias Ringwald 			break;
1588caefee3SMatthias Ringwald 		case w4_services_complete:
159*045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
160*045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
1618caefee3SMatthias Ringwald 				if (testService == null){
1628caefee3SMatthias Ringwald 					addMessage(String.format("First service UUID %s", event.getService().getUUID()));
1638caefee3SMatthias Ringwald 					testService = event.getService();
1648caefee3SMatthias Ringwald 				}
1658caefee3SMatthias Ringwald 				Log.d(BTSTACK_TAG, "Service: " + event.getService());
1668caefee3SMatthias Ringwald 				service_count++;
1678caefee3SMatthias Ringwald 			}
168*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
1698caefee3SMatthias Ringwald 				addMessage(String.format("Service query complete, total %d services", service_count));
1708caefee3SMatthias Ringwald 				state = STATE.w4_characteristic_complete;
1718caefee3SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForService(connectionHandle, testService);
1728caefee3SMatthias Ringwald 			}
1738caefee3SMatthias Ringwald 			break;
1748caefee3SMatthias Ringwald 
1758caefee3SMatthias Ringwald 		case w4_characteristic_complete:
176*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
177*045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
1788caefee3SMatthias Ringwald 				if (testCharacteristic == null){
1798caefee3SMatthias Ringwald 					addMessage(String.format("First characteristic UUID %s", event.getCharacteristic().getUUID()));
1808caefee3SMatthias Ringwald 					testCharacteristic = event.getCharacteristic();
1818caefee3SMatthias Ringwald 				}
1828caefee3SMatthias Ringwald 				Log.d(BTSTACK_TAG, "Characteristic: " + event.getCharacteristic());
1838caefee3SMatthias Ringwald 				characteristic_count++;
1848caefee3SMatthias Ringwald 			}
185*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
1868caefee3SMatthias Ringwald 				addMessage(String.format("Characteristic query complete, total %d characteristics", characteristic_count));
1878caefee3SMatthias Ringwald 				if (characteristic_count > 0){
1888caefee3SMatthias Ringwald 					state = STATE.w4_characteristic_read;
1898caefee3SMatthias Ringwald 					btstack.GATTReadValueOfCharacteristic(connectionHandle, testCharacteristic);
1908caefee3SMatthias Ringwald 				} else {
1918caefee3SMatthias Ringwald 					state = STATE.w4_disconnect;
1928caefee3SMatthias Ringwald 					btstack.GAPDisconnect(connectionHandle);
1938caefee3SMatthias Ringwald 				}
1948caefee3SMatthias Ringwald 			}
1958caefee3SMatthias Ringwald 			break;
1968caefee3SMatthias Ringwald 
1978caefee3SMatthias Ringwald 		case w4_characteristic_read:
198*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicValueQueryResult){
1998caefee3SMatthias Ringwald 				addMessage("Read complete");
2008caefee3SMatthias Ringwald 				Log.d(BTSTACK_TAG, packet.toString());
2018caefee3SMatthias Ringwald 				state = STATE.w4_characteristic_write;
2028caefee3SMatthias Ringwald 				byte [] data = { 'B', 'T', 's', 't', 'a', 'c', 'k'};
2038caefee3SMatthias Ringwald 				btstack.GATTWriteValueOfCharacteristic(connectionHandle, testCharacteristic, data.length, data);
2048caefee3SMatthias Ringwald 			}
2058caefee3SMatthias Ringwald 			break;
2068caefee3SMatthias Ringwald 		case w4_characteristic_write:
207*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2088caefee3SMatthias Ringwald 				addMessage("Write complete, disconnect now.");
2098caefee3SMatthias Ringwald 				state = STATE.w4_disconnect;
2108caefee3SMatthias Ringwald 				// btstack.GAPDisconnect(testHandle);
2118caefee3SMatthias Ringwald 			}
2128caefee3SMatthias Ringwald 			break;
2138caefee3SMatthias Ringwald 		case w4_disconnect:
2148caefee3SMatthias Ringwald 			if (packet instanceof HCIEventDisconnectionComplete){
2158caefee3SMatthias Ringwald 				addMessage("Disconnected.");
2168caefee3SMatthias Ringwald 			}
2178caefee3SMatthias Ringwald 			break;
2188caefee3SMatthias Ringwald 		default:
2198caefee3SMatthias Ringwald 			break;
2208caefee3SMatthias Ringwald 		}
2218caefee3SMatthias Ringwald 	}
2228caefee3SMatthias Ringwald 
testAccelerometer(Packet packet)2238caefee3SMatthias Ringwald 	public void testAccelerometer(Packet packet){
2248caefee3SMatthias Ringwald 		switch (state){
2258caefee3SMatthias Ringwald 		case w4_btstack_working:
2268caefee3SMatthias Ringwald 			if (packet instanceof BTstackEventState){
2278caefee3SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
2288caefee3SMatthias Ringwald 				if (event.getState() == 2)	{
2298caefee3SMatthias Ringwald 					addMessage("GAPLEScanStart()");
2308caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
2318caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
2328caefee3SMatthias Ringwald 				}
2338caefee3SMatthias Ringwald 			}
2348caefee3SMatthias Ringwald 			break;
2358caefee3SMatthias Ringwald 		case w4_scan_result:
236*045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
237*045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
2388caefee3SMatthias Ringwald 				testAddrType = report.getAddressType();
2398caefee3SMatthias Ringwald 				deviceAddr = report.getAddress();
2408caefee3SMatthias Ringwald 				if (deviceAddr.toString().equalsIgnoreCase(sensor_tag_addr.toString())){
2418caefee3SMatthias Ringwald 					addMessage(String.format("Adv: type %d, addr %s", testAddrType, deviceAddr));
2428caefee3SMatthias Ringwald 					addMessage(String.format("Data: %s", Util.asHexdump(report.getData())));
2438caefee3SMatthias Ringwald 					addMessage("GAPLEScanStop()");
2448caefee3SMatthias Ringwald 					btstack.GAPLEScanStop();
2458caefee3SMatthias Ringwald 					addMessage("GAPLEConnect(...)");
2468caefee3SMatthias Ringwald 					state = STATE.w4_connected_acc;
2478caefee3SMatthias Ringwald 					btstack.GAPLEConnect(testAddrType, deviceAddr);
2488caefee3SMatthias Ringwald 				}
2498caefee3SMatthias Ringwald 			}
2508caefee3SMatthias Ringwald 			break;
2518caefee3SMatthias Ringwald 
2528caefee3SMatthias Ringwald 
2538caefee3SMatthias Ringwald 
2548caefee3SMatthias Ringwald 		case w4_connected_acc:
2558caefee3SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
2568caefee3SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
2578caefee3SMatthias Ringwald 				connectionHandle = event.getConnectionHandle();
2588caefee3SMatthias Ringwald 				addMessage(String.format("Connection complete, status %d, handle %x", event.getStatus(), connectionHandle));
2598caefee3SMatthias Ringwald 				addMessage("Search for ACC service");
2608caefee3SMatthias Ringwald 				state = STATE.w4_acc_service_result;
2618caefee3SMatthias Ringwald 				byte [] uuid = new byte[16];
2628caefee3SMatthias Ringwald 				Util.flipX(this.acc_service_uuid, uuid);
2638caefee3SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServicesByUUID128(connectionHandle, new BT_UUID(uuid));
2648caefee3SMatthias Ringwald 			}
2658caefee3SMatthias Ringwald 			break;
2668caefee3SMatthias Ringwald 
2678caefee3SMatthias Ringwald 		case w4_acc_service_result:
2688caefee3SMatthias Ringwald 			clearMessages();
2698caefee3SMatthias Ringwald 			addMessage(String.format("w4_acc_service_result state"));
270*045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
271*045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
2728caefee3SMatthias Ringwald 				addMessage(String.format("ACC service found %s", event.getService().getUUID()));
2738caefee3SMatthias Ringwald 				accService = event.getService();
2748caefee3SMatthias Ringwald 				break;
2758caefee3SMatthias Ringwald 			}
276*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2778caefee3SMatthias Ringwald 				if (accService == null) {
2788caefee3SMatthias Ringwald 					addMessage("No acc service found");
2798caefee3SMatthias Ringwald 					break;
2808caefee3SMatthias Ringwald 				}
2818caefee3SMatthias Ringwald 				addMessage("ACC Service found, searching for acc enable characteristic");
2828caefee3SMatthias Ringwald 				state = STATE.w4_acc_enable_characteristic_result;
2838caefee3SMatthias Ringwald 				byte [] uuid = new byte[16];
2848caefee3SMatthias Ringwald 				Util.flipX(this.acc_chr_enable_uuid, uuid);
2858caefee3SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForServiceByUUID128(connectionHandle, accService, new BT_UUID(uuid));
2868caefee3SMatthias Ringwald 			}
2878caefee3SMatthias Ringwald 			break;
2888caefee3SMatthias Ringwald 
2898caefee3SMatthias Ringwald 		case w4_acc_enable_characteristic_result:
290*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
291*045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
2928caefee3SMatthias Ringwald 				enableCharacteristic = event.getCharacteristic();
2938caefee3SMatthias Ringwald 				addMessage("Enable ACC Characteristic found ");
2948caefee3SMatthias Ringwald 			}
295*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
2968caefee3SMatthias Ringwald 				if (enableCharacteristic == null) {
2978caefee3SMatthias Ringwald 					addMessage("No acc enable chr found");
2988caefee3SMatthias Ringwald 					break;
2998caefee3SMatthias Ringwald 				}
3008caefee3SMatthias Ringwald 				addMessage("Write enable acc characteristic");
3018caefee3SMatthias Ringwald 				state = STATE.w4_write_acc_enable_result;
3028caefee3SMatthias Ringwald 				btstack.GATTWriteValueOfCharacteristic(connectionHandle, enableCharacteristic, 1, this.acc_enable);
3038caefee3SMatthias Ringwald 			}
3048caefee3SMatthias Ringwald 			break;
3058caefee3SMatthias Ringwald 		case w4_write_acc_enable_result:
306*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3078caefee3SMatthias Ringwald 				addMessage("Acc enabled,searching for acc client config characteristic");
3088caefee3SMatthias Ringwald 				byte [] uuid = new byte[16];
3098caefee3SMatthias Ringwald 				Util.flipX(this.acc_chr_client_config_uuid, uuid);
3108caefee3SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForServiceByUUID128(connectionHandle, accService, new BT_UUID(uuid));
3118caefee3SMatthias Ringwald 				state = STATE.w4_acc_client_config_characteristic_result;
3128caefee3SMatthias Ringwald 			}
3138caefee3SMatthias Ringwald 			break;
3148caefee3SMatthias Ringwald 
3158caefee3SMatthias Ringwald 		case w4_acc_client_config_characteristic_result:
316*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
317*045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
3188caefee3SMatthias Ringwald 				configCharacteristic = event.getCharacteristic();
3198caefee3SMatthias Ringwald 				addMessage("ACC Client Config Characteristic found");
3208caefee3SMatthias Ringwald 			}
321*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3228caefee3SMatthias Ringwald 				if (configCharacteristic == null) {
3238caefee3SMatthias Ringwald 					addMessage("No acc config chr found");
3248caefee3SMatthias Ringwald 					break;
3258caefee3SMatthias Ringwald 				}
3268caefee3SMatthias Ringwald 				addMessage("Write ACC Client Config Characteristic");
3278caefee3SMatthias Ringwald 				state = STATE.w4_acc_data;
3288caefee3SMatthias Ringwald 				btstack.GATTWriteClientCharacteristicConfiguration(connectionHandle, configCharacteristic, this.acc_notification);
3298caefee3SMatthias Ringwald 			}
3308caefee3SMatthias Ringwald 			break;
3318caefee3SMatthias Ringwald 
3328caefee3SMatthias Ringwald 		case w4_acc_data:
333*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
3348caefee3SMatthias Ringwald 				addMessage("Acc configured for notification");
3358caefee3SMatthias Ringwald 				break;
3368caefee3SMatthias Ringwald 			}
3378caefee3SMatthias Ringwald 
338*045013feSMatthias Ringwald 			if (packet instanceof GATTEventNotification){
3398caefee3SMatthias Ringwald 				addTempMessage("Acc Value");
3408caefee3SMatthias Ringwald 				Log.d(BTSTACK_TAG, packet.toString());
3418caefee3SMatthias Ringwald 				//state = STATE.w4_btstack_working;
3428caefee3SMatthias Ringwald 				//btstack.GAPDisconnect(connectionHandle);
3438caefee3SMatthias Ringwald 			}
3448caefee3SMatthias Ringwald 			break;
3458caefee3SMatthias Ringwald 
3468caefee3SMatthias Ringwald 		default:
3478caefee3SMatthias Ringwald 			break;
3488caefee3SMatthias Ringwald 		}
3498caefee3SMatthias Ringwald 	}
3508caefee3SMatthias Ringwald 
testConnectDisconnect(Packet packet)3518caefee3SMatthias Ringwald 	public void testConnectDisconnect(Packet packet){
3528caefee3SMatthias Ringwald 		if (packet instanceof HCIEventDisconnectionComplete){
3538caefee3SMatthias Ringwald 			if (state != STATE.w4_disconnect) {
3548caefee3SMatthias Ringwald 				state = STATE.w4_scan_result;
3558caefee3SMatthias Ringwald 				btstack.GAPLEScanStart();
3568caefee3SMatthias Ringwald 				clearMessages();
3578caefee3SMatthias Ringwald 				HCIEventDisconnectionComplete event = (HCIEventDisconnectionComplete) packet;
3588caefee3SMatthias Ringwald 				addMessage(String.format("Unexpected disconnect %x. Start scan.", event.getConnectionHandle()));
3598caefee3SMatthias Ringwald 				return;
3608caefee3SMatthias Ringwald 			}
3618caefee3SMatthias Ringwald 		}
3628caefee3SMatthias Ringwald 
3638caefee3SMatthias Ringwald 		switch (state){
3648caefee3SMatthias Ringwald 		case w4_btstack_working:
3658caefee3SMatthias Ringwald 			if (packet instanceof BTstackEventState){
3668caefee3SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
3678caefee3SMatthias Ringwald 				if (event.getState() == 2)	{
3688caefee3SMatthias Ringwald //					addMessage("GAPLEScanStart()");
3698caefee3SMatthias Ringwald //					state = STATE.w4_scan_result;
3708caefee3SMatthias Ringwald 					// btstack.GAPLEScanStart();
3718caefee3SMatthias Ringwald //					runOnUiThread(new Runnable(){
3728caefee3SMatthias Ringwald //						public void run(){
3738caefee3SMatthias Ringwald 							addMessage("BTstack dissconnect.");
3748caefee3SMatthias Ringwald 							btstack.disconnect();
3758caefee3SMatthias Ringwald 
3768caefee3SMatthias Ringwald //					runOnUiThread(new Runnable(){
3778caefee3SMatthias Ringwald //						public void run(){
3788caefee3SMatthias Ringwald 							try {
3798caefee3SMatthias Ringwald 								Thread.sleep(5000);
3808caefee3SMatthias Ringwald 							} catch (InterruptedException e) {
3818caefee3SMatthias Ringwald 								e.printStackTrace();
3828caefee3SMatthias Ringwald 							}
3838caefee3SMatthias Ringwald 							btstack.connect();
3848caefee3SMatthias Ringwald 							addMessage("Power on BTstack.");
3858caefee3SMatthias Ringwald 							state = STATE.w4_btstack_working;
3868caefee3SMatthias Ringwald 							test_run_count = 0;
3878caefee3SMatthias Ringwald 							btstack.BTstackSetPowerMode(1);
3888caefee3SMatthias Ringwald //						}
3898caefee3SMatthias Ringwald //					});
3908caefee3SMatthias Ringwald 				}
3918caefee3SMatthias Ringwald 			}
3928caefee3SMatthias Ringwald 			break;
3938caefee3SMatthias Ringwald 		case w4_scan_result:
394*045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
3958caefee3SMatthias Ringwald 				BD_ADDR sensor_tag_addr = new BD_ADDR("1C:BA:8C:20:C7:F6");
3968caefee3SMatthias Ringwald 				//BD_ADDR pts_dongle = new BD_ADDR("00:1B:DC:07:32:EF");
3978caefee3SMatthias Ringwald 
398*045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
3998caefee3SMatthias Ringwald 				BD_ADDR reportAddr = report.getAddress();
4008caefee3SMatthias Ringwald 				if (reportAddr.toString().equalsIgnoreCase(sensor_tag_addr.toString())){
4018caefee3SMatthias Ringwald 					testAddrType = report.getAddressType();
4028caefee3SMatthias Ringwald 					deviceAddr = report.getAddress();
4038caefee3SMatthias Ringwald 					addMessage(String.format("Adv: type %d, addr %s", testAddrType, deviceAddr));
4048caefee3SMatthias Ringwald 					addMessage(String.format("Data: %s", Util.asHexdump(report.getData())));
4058caefee3SMatthias Ringwald 
4068caefee3SMatthias Ringwald 					addMessage("GAPLEScanStop()");
4078caefee3SMatthias Ringwald 					btstack.GAPLEScanStop();
4088caefee3SMatthias Ringwald 					addMessage("GAPLEConnect(...)");
4098caefee3SMatthias Ringwald 					state = STATE.w4_connected;
4108caefee3SMatthias Ringwald 
4118caefee3SMatthias Ringwald 					btstack.GAPLEConnect(testAddrType, deviceAddr);
4128caefee3SMatthias Ringwald 				}
4138caefee3SMatthias Ringwald 			}
4148caefee3SMatthias Ringwald 			break;
4158caefee3SMatthias Ringwald 		case w4_connected:
4168caefee3SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
4178caefee3SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
4188caefee3SMatthias Ringwald 
4198caefee3SMatthias Ringwald 				connectionHandle = event.getConnectionHandle();
4208caefee3SMatthias Ringwald 				addMessage(String.format("Connection complete, status %d, handle %x", event.getStatus(), connectionHandle));
4218caefee3SMatthias Ringwald 				state = STATE.w4_services_complete;
4228caefee3SMatthias Ringwald 				addMessage("GATTDiscoverPrimaryServices(...)");
4238caefee3SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServices(connectionHandle);
4248caefee3SMatthias Ringwald 
4258caefee3SMatthias Ringwald 			}
4268caefee3SMatthias Ringwald 			break;
4278caefee3SMatthias Ringwald 		case w4_services_complete:
428*045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
429*045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
4308caefee3SMatthias Ringwald 				if (testService == null){
4318caefee3SMatthias Ringwald 					addMessage(String.format("First service UUID %s", event.getService().getUUID()));
4328caefee3SMatthias Ringwald 					testService = event.getService();
4338caefee3SMatthias Ringwald 				}
4348caefee3SMatthias Ringwald 				Log.d(BTSTACK_TAG, "Service: " + event.getService());
4358caefee3SMatthias Ringwald 				service_count++;
4368caefee3SMatthias Ringwald 			}
437*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
4388caefee3SMatthias Ringwald 				addMessage(String.format("Service query complete, total %d services", service_count));
4398caefee3SMatthias Ringwald 				state = STATE.w4_disconnect;
4408caefee3SMatthias Ringwald 				test_run_count++;
4418caefee3SMatthias Ringwald 				btstack.GAPDisconnect(connectionHandle);
4428caefee3SMatthias Ringwald 			}
4438caefee3SMatthias Ringwald 			break;
4448caefee3SMatthias Ringwald 		case w4_disconnect:
4458caefee3SMatthias Ringwald 			Log.d(BTSTACK_TAG, packet.toString());
4468caefee3SMatthias Ringwald 			if (packet instanceof HCIEventDisconnectionComplete){
4478caefee3SMatthias Ringwald 				clearMessages();
4488caefee3SMatthias Ringwald 				addMessage("BTstack dissconnect.");
4498caefee3SMatthias Ringwald 				btstack.disconnect();
4508caefee3SMatthias Ringwald 				try {
4518caefee3SMatthias Ringwald 					Thread.sleep(15000);
4528caefee3SMatthias Ringwald 				} catch (InterruptedException e) {
4538caefee3SMatthias Ringwald 					e.printStackTrace();
4548caefee3SMatthias Ringwald 				}
4558caefee3SMatthias Ringwald 
4568caefee3SMatthias Ringwald 				btstack.connect();
4578caefee3SMatthias Ringwald 				addMessage("Power on BTstack.");
4588caefee3SMatthias Ringwald 				state = STATE.w4_btstack_working;
4598caefee3SMatthias Ringwald 				test_run_count = 0;
4608caefee3SMatthias Ringwald 				btstack.BTstackSetPowerMode(1);
4618caefee3SMatthias Ringwald 
4628caefee3SMatthias Ringwald 
4638caefee3SMatthias Ringwald 				/*if (test_run_count%10 == 0){
4648caefee3SMatthias Ringwald 					addMessage("Power off BTstack.");
4658caefee3SMatthias Ringwald 					state = STATE.w4_btstack_working;
4668caefee3SMatthias Ringwald 					btstack.BTstackSetPowerMode(0);
4678caefee3SMatthias Ringwald 					try {
4688caefee3SMatthias Ringwald 						Thread.sleep(15000);
4698caefee3SMatthias Ringwald 					} catch (InterruptedException e) {
4708caefee3SMatthias Ringwald 						e.printStackTrace();
4718caefee3SMatthias Ringwald 					}
4728caefee3SMatthias Ringwald 					addMessage("Power on BTstack.");
4738caefee3SMatthias Ringwald 					btstack.BTstackSetPowerMode(1);
4748caefee3SMatthias Ringwald 				} else {
4758caefee3SMatthias Ringwald 					addMessage("GAPLEScanStart()");
4768caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
4778caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
4788caefee3SMatthias Ringwald 				}*/
4798caefee3SMatthias Ringwald 			}
4808caefee3SMatthias Ringwald 			break;
4818caefee3SMatthias Ringwald 		default:
4828caefee3SMatthias Ringwald 			break;
4838caefee3SMatthias Ringwald 		}
4848caefee3SMatthias Ringwald 	}
4858caefee3SMatthias Ringwald 
trackRSSI(Packet packet)4868caefee3SMatthias Ringwald 	public void trackRSSI(Packet packet){
4878caefee3SMatthias Ringwald 		switch (state){
4888caefee3SMatthias Ringwald 		case w4_btstack_working:
4898caefee3SMatthias Ringwald 			if (packet instanceof BTstackEventState){
4908caefee3SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
4918caefee3SMatthias Ringwald 				if (event.getState() == 2)	{
4928caefee3SMatthias Ringwald 					addMessage("GAPLEScanStart()");
4938caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
4948caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
4958caefee3SMatthias Ringwald 				}
4968caefee3SMatthias Ringwald 			}
4978caefee3SMatthias Ringwald 			break;
4988caefee3SMatthias Ringwald 		case w4_scan_result:
499*045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
5008caefee3SMatthias Ringwald 				clearMessages();
501*045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
5028caefee3SMatthias Ringwald 				testAddrType = report.getAddressType();
5038caefee3SMatthias Ringwald 				deviceAddr = report.getAddress();
5048caefee3SMatthias Ringwald 				addMessage(String.format("Adv: type %d, addr %s", testAddrType, deviceAddr));
5058caefee3SMatthias Ringwald 				addMessage(String.format("Data: %s", Util.asHexdump(report.getData())));
5068caefee3SMatthias Ringwald 				addMessage("GAPLEScanStop()");
5078caefee3SMatthias Ringwald 				btstack.GAPLEScanStop();
5088caefee3SMatthias Ringwald 				addMessage("GAPLEConnect(...)");
5098caefee3SMatthias Ringwald 				state = STATE.w4_connected;
5108caefee3SMatthias Ringwald 				btstack.GAPLEConnect(testAddrType, deviceAddr);
5118caefee3SMatthias Ringwald 			}
5128caefee3SMatthias Ringwald 			break;
5138caefee3SMatthias Ringwald 		case w4_connected:
5148caefee3SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
5158caefee3SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
5168caefee3SMatthias Ringwald 				if (event.getStatus() != 0) {
5178caefee3SMatthias Ringwald 					addMessage(String.format("Connection didn't succeed, status %d, restart scanning.", event.getStatus()));
5188caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
5198caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
5208caefee3SMatthias Ringwald 					break;
5218caefee3SMatthias Ringwald 				}
5228caefee3SMatthias Ringwald 
5238caefee3SMatthias Ringwald 				state = STATE.track_rssi;
5248caefee3SMatthias Ringwald 				connectionHandle = event.getConnectionHandle();
5258caefee3SMatthias Ringwald 				addMessage(String.format("Connection complete, status %d, handle %x. Start RSSI query.", event.getStatus(), connectionHandle));
5268caefee3SMatthias Ringwald 				counter = 0;
5278caefee3SMatthias Ringwald 				new Thread(new Runnable(){
5288caefee3SMatthias Ringwald 					@Override
5298caefee3SMatthias Ringwald 					public void run() {
5308caefee3SMatthias Ringwald 						try {
5318caefee3SMatthias Ringwald 							while(state == STATE.track_rssi){
5328caefee3SMatthias Ringwald 								Thread.sleep(1000);
5338caefee3SMatthias Ringwald 								btstack.HCIReadRssi(connectionHandle);
5348caefee3SMatthias Ringwald 							}
5358caefee3SMatthias Ringwald 						} catch (InterruptedException e) {}
5368caefee3SMatthias Ringwald 					}
5378caefee3SMatthias Ringwald 				}).start();
5388caefee3SMatthias Ringwald 			}
5398caefee3SMatthias Ringwald 			break;
5408caefee3SMatthias Ringwald 		case track_rssi:
5418caefee3SMatthias Ringwald 			if (packet instanceof HCIEventDisconnectionComplete){
5428caefee3SMatthias Ringwald 				clearMessages();
5438caefee3SMatthias Ringwald 				addMessage(String.format("Received dissconnect. Start scannning."));
5448caefee3SMatthias Ringwald 				state = STATE.w4_scan_result;
5458caefee3SMatthias Ringwald 				btstack.GAPLEScanStart();
5468caefee3SMatthias Ringwald 				break;
5478caefee3SMatthias Ringwald 			}
5488caefee3SMatthias Ringwald 
5498caefee3SMatthias Ringwald 			if (!(packet instanceof HCIEventCommandComplete)) break;
5508caefee3SMatthias Ringwald 			HCIEventCommandComplete event = (HCIEventCommandComplete) packet;
5518caefee3SMatthias Ringwald 			int opcode = event.getCommandOpcode();
5528caefee3SMatthias Ringwald 			if (opcode != 0X1405) break;
5538caefee3SMatthias Ringwald 			counter = counter + 1;
5548caefee3SMatthias Ringwald 			byte data[] = event.getReturnParameters();
5558caefee3SMatthias Ringwald 			addTempMessage(String.format("counter %d, status %d, RSSI: %ddBm", counter, Util.readByte(data, 0), data[3]));
5568caefee3SMatthias Ringwald 			break;
5578caefee3SMatthias Ringwald 
5588caefee3SMatthias Ringwald 		default:
5598caefee3SMatthias Ringwald 			break;
5608caefee3SMatthias Ringwald 		}
5618caefee3SMatthias Ringwald 	}
5628caefee3SMatthias Ringwald 
queryBatteryLevel(Packet packet)5638caefee3SMatthias Ringwald 	public void queryBatteryLevel(Packet packet){
5648caefee3SMatthias Ringwald 		// Restart scanning on disconnect.
5658caefee3SMatthias Ringwald 		if (packet instanceof HCIEventDisconnectionComplete){
5668caefee3SMatthias Ringwald 			clearMessages();
5678caefee3SMatthias Ringwald 			addMessage(String.format("Received dissconnect. Start scannning."));
5688caefee3SMatthias Ringwald 			state = STATE.w4_scan_result;
5698caefee3SMatthias Ringwald 			btstack.GAPLEScanStart();
5708caefee3SMatthias Ringwald 			return;
5718caefee3SMatthias Ringwald 		}
5728caefee3SMatthias Ringwald 
5738caefee3SMatthias Ringwald 		switch (state){
5748caefee3SMatthias Ringwald 		case w4_btstack_working:
5758caefee3SMatthias Ringwald 			if (packet instanceof BTstackEventState){
5768caefee3SMatthias Ringwald 				BTstackEventState event = (BTstackEventState) packet;
5778caefee3SMatthias Ringwald 				if (event.getState() == 2)	{
5788caefee3SMatthias Ringwald 					addMessage("BTstack working, start scanning.");
5798caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
5808caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
5818caefee3SMatthias Ringwald 				}
5828caefee3SMatthias Ringwald 			}
5838caefee3SMatthias Ringwald 			break;
5848caefee3SMatthias Ringwald 		case w4_scan_result:
585*045013feSMatthias Ringwald 			if (packet instanceof GAPEventAdvertisingReport){
5868caefee3SMatthias Ringwald 				// Advertisement received. Connect to the found BT address.
5878caefee3SMatthias Ringwald 				clearMessages();
588*045013feSMatthias Ringwald 				GAPEventAdvertisingReport report = (GAPEventAdvertisingReport) packet;
5898caefee3SMatthias Ringwald 				testAddrType = report.getAddressType();
5908caefee3SMatthias Ringwald 				deviceAddr = report.getAddress();
5918caefee3SMatthias Ringwald 				addMessage(String.format("Adv: type %d, addr %s\ndata: %s \n Stop scan, initiate connect.", testAddrType, deviceAddr, Util.asHexdump(report.getData())));
5928caefee3SMatthias Ringwald 				btstack.GAPLEScanStop();
5938caefee3SMatthias Ringwald 				state = STATE.w4_connected;
5948caefee3SMatthias Ringwald 				btstack.GAPLEConnect(testAddrType, deviceAddr);
5958caefee3SMatthias Ringwald 			}
5968caefee3SMatthias Ringwald 			break;
5978caefee3SMatthias Ringwald 		case w4_connected:
5988caefee3SMatthias Ringwald 			if (packet instanceof HCIEventLEConnectionComplete){
5998caefee3SMatthias Ringwald 				HCIEventLEConnectionComplete event = (HCIEventLEConnectionComplete) packet;
6008caefee3SMatthias Ringwald 				if (event.getStatus() != 0) {
6018caefee3SMatthias Ringwald 					addMessage(String.format("Connection didn't succeed, status %d, restart scanning.", event.getStatus()));
6028caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
6038caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
6048caefee3SMatthias Ringwald 					break;
6058caefee3SMatthias Ringwald 				}
6068caefee3SMatthias Ringwald 
6078caefee3SMatthias Ringwald 				// Query battery service.
6088caefee3SMatthias Ringwald 				state = STATE.w4_services_complete;
6098caefee3SMatthias Ringwald 				connectionHandle = event.getConnectionHandle();
6108caefee3SMatthias Ringwald 				addMessage(String.format("Connection complete, status %d, handle %x.\nQuery battery service.", event.getStatus(), connectionHandle));
6118caefee3SMatthias Ringwald 
6128caefee3SMatthias Ringwald 				//btstack.GATTDiscoverPrimaryServicesByUUID128(connectionHandle, uuid128(this.battery_service_uuid));
6138caefee3SMatthias Ringwald 				btstack.GATTDiscoverPrimaryServicesByUUID16(connectionHandle, 0x180f);
6148caefee3SMatthias Ringwald 			}
6158caefee3SMatthias Ringwald 			break;
6168caefee3SMatthias Ringwald 
6178caefee3SMatthias Ringwald 		case w4_services_complete:
618*045013feSMatthias Ringwald 			if (packet instanceof GATTEventServiceQueryResult){
619*045013feSMatthias Ringwald 				// Store battery service. Wait for GATTEventQueryComplete event to send next GATT command.
620*045013feSMatthias Ringwald 				GATTEventServiceQueryResult event = (GATTEventServiceQueryResult) packet;
6218caefee3SMatthias Ringwald 				addMessage(String.format("Battery service found %s", event.getService().getUUID()));
6228caefee3SMatthias Ringwald 				batteryService = event.getService();
6238caefee3SMatthias Ringwald 				break;
6248caefee3SMatthias Ringwald 			}
625*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
6268caefee3SMatthias Ringwald 				// Check if battery service is found.
6278caefee3SMatthias Ringwald 				if (batteryService == null) {
6288caefee3SMatthias Ringwald 					addMessage("No battery service found, restart scanning.");
6298caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
6308caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
6318caefee3SMatthias Ringwald 					break;
6328caefee3SMatthias Ringwald 				}
6338caefee3SMatthias Ringwald 				addMessage("Battery service is found. Query battery level.");
6348caefee3SMatthias Ringwald 				state = STATE.w4_characteristic_complete;
6358caefee3SMatthias Ringwald 				btstack.GATTDiscoverCharacteristicsForServiceByUUID128(connectionHandle, batteryService, uuid128(this.battery_level_chr_uuid));
6368caefee3SMatthias Ringwald 			}
6378caefee3SMatthias Ringwald 			break;
6388caefee3SMatthias Ringwald 		case w4_characteristic_complete:
639*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicQueryResult){
640*045013feSMatthias Ringwald 				// Store battery level characteristic. Wait for GATTEventQueryComplete event to send next GATT command.
641*045013feSMatthias Ringwald 				GATTEventCharacteristicQueryResult event = (GATTEventCharacteristicQueryResult) packet;
6428caefee3SMatthias Ringwald 				batteryLevelCharacteristic = event.getCharacteristic();
6438caefee3SMatthias Ringwald 				addMessage("Battery level characteristic found.");
6448caefee3SMatthias Ringwald 				break;
6458caefee3SMatthias Ringwald 			}
6468caefee3SMatthias Ringwald 
647*045013feSMatthias Ringwald 			if (!(packet instanceof GATTEventQueryComplete)) break;
6488caefee3SMatthias Ringwald 			if (batteryLevelCharacteristic == null) {
6498caefee3SMatthias Ringwald 				addMessage("No battery level characteristic found");
6508caefee3SMatthias Ringwald 				break;
6518caefee3SMatthias Ringwald 			}
6528caefee3SMatthias Ringwald 			clearMessages();
6538caefee3SMatthias Ringwald 			addMessage("Polling battery.");
6548caefee3SMatthias Ringwald 			counter = 0;
6558caefee3SMatthias Ringwald 			state = STATE.battery_data;
6568caefee3SMatthias Ringwald 			new Thread(new Runnable(){
6578caefee3SMatthias Ringwald 				@Override
6588caefee3SMatthias Ringwald 				public void run() {
6598caefee3SMatthias Ringwald 					try {
6608caefee3SMatthias Ringwald 						while(state == STATE.battery_data){
6618caefee3SMatthias Ringwald 							Thread.sleep(5000);
6628caefee3SMatthias Ringwald 							btstack.GATTReadValueOfCharacteristic(connectionHandle, batteryLevelCharacteristic);
6638caefee3SMatthias Ringwald 						}
6648caefee3SMatthias Ringwald 					} catch (InterruptedException e) {}
6658caefee3SMatthias Ringwald 				}
6668caefee3SMatthias Ringwald 			}).start();
6678caefee3SMatthias Ringwald 			break;
6688caefee3SMatthias Ringwald 
6698caefee3SMatthias Ringwald 		case battery_data:
6708caefee3SMatthias Ringwald 			clearMessages();
671*045013feSMatthias Ringwald 			if (packet instanceof GATTEventCharacteristicValueQueryResult){
672*045013feSMatthias Ringwald 				GATTEventCharacteristicValueQueryResult battery = (GATTEventCharacteristicValueQueryResult) packet;
6738caefee3SMatthias Ringwald 
6748caefee3SMatthias Ringwald 				if (battery.getValueLength() < 1) break;
6758caefee3SMatthias Ringwald 				this.batteryLevel = battery.getValue()[0];
6768caefee3SMatthias Ringwald 				addTempMessage(String.format("Counter %d, battery level: %d", counter, batteryLevel) + "%");
6778caefee3SMatthias Ringwald 				counter = counter + 1;
6788caefee3SMatthias Ringwald 				break;
6798caefee3SMatthias Ringwald 
6808caefee3SMatthias Ringwald 			}
681*045013feSMatthias Ringwald 			if (packet instanceof GATTEventQueryComplete){
682*045013feSMatthias Ringwald 				GATTEventQueryComplete event = (GATTEventQueryComplete) packet;
6838caefee3SMatthias Ringwald 				addMessage(String.format("Counter %d, battery level: %d", counter, batteryLevel) + "%");
6848caefee3SMatthias Ringwald 				if (event.getStatus() != 0){
6858caefee3SMatthias Ringwald 					addMessage("Battery data could not be read - status 0x%02x. Restart scanning." + String.valueOf(event.getStatus()));
6868caefee3SMatthias Ringwald 					state = STATE.w4_scan_result;
6878caefee3SMatthias Ringwald 					btstack.GAPLEScanStart();
6888caefee3SMatthias Ringwald 					break;
6898caefee3SMatthias Ringwald 				}
6908caefee3SMatthias Ringwald 			}
6918caefee3SMatthias Ringwald 
6928caefee3SMatthias Ringwald 			break;
6938caefee3SMatthias Ringwald 		default:
6948caefee3SMatthias Ringwald 			break;
6958caefee3SMatthias Ringwald 		}
6968caefee3SMatthias Ringwald 	}
6978caefee3SMatthias Ringwald 
uuid128(byte[] att_uuid)6988caefee3SMatthias Ringwald 	private BT_UUID uuid128(byte[] att_uuid) {
6998caefee3SMatthias Ringwald 		byte [] uuid = new byte[16];
7008caefee3SMatthias Ringwald 		Util.flipX(att_uuid, uuid);
7018caefee3SMatthias Ringwald 		return new BT_UUID(uuid);
7028caefee3SMatthias Ringwald 	}
7038caefee3SMatthias Ringwald 
handlePacket(Packet packet)7048caefee3SMatthias Ringwald 	public void handlePacket(Packet packet){
7058caefee3SMatthias Ringwald 		// queryBatteryLevel(packet);
7068caefee3SMatthias Ringwald 		// trackRSSI(packet);
7078caefee3SMatthias Ringwald 		// testCharacteristics(packet);
7088caefee3SMatthias Ringwald 		// testAccelerometer(packet);
7098caefee3SMatthias Ringwald 		testConnectDisconnect(packet);
7108caefee3SMatthias Ringwald 	}
7118caefee3SMatthias Ringwald 
test()7128caefee3SMatthias Ringwald 	void test(){
7138caefee3SMatthias Ringwald 		counter = 0;
7148caefee3SMatthias Ringwald 		addMessage("LE Test Application");
7158caefee3SMatthias Ringwald 
7168caefee3SMatthias Ringwald 		btstack = new BTstack();
7178caefee3SMatthias Ringwald 		btstack.registerPacketHandler(this);
7188caefee3SMatthias Ringwald 
7198caefee3SMatthias Ringwald 		boolean ok = btstack.connect();
7208caefee3SMatthias Ringwald 		if (!ok) {
7218caefee3SMatthias Ringwald 			addMessage("Failed to connect to BTstack Server");
7228caefee3SMatthias Ringwald 			return;
7238caefee3SMatthias Ringwald 		}
7248caefee3SMatthias Ringwald 
7258caefee3SMatthias Ringwald 		addMessage("BTstackSetPowerMode(1)");
7268caefee3SMatthias Ringwald 
7278caefee3SMatthias Ringwald 		state = STATE.w4_btstack_working;
7288caefee3SMatthias Ringwald 		btstack.BTstackSetPowerMode(1);
7298caefee3SMatthias Ringwald 	}
7308caefee3SMatthias Ringwald }
731