Advertisement
Guest User

Untitled

a guest
Oct 17th, 2014
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. package com.example.bluetoothlowenergy;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.UUID;
  5. import android.app.Activity;
  6. import android.app.Service;
  7. import android.bluetooth.BluetoothAdapter;
  8. import android.bluetooth.BluetoothDevice;
  9. import android.bluetooth.BluetoothGatt;
  10. import android.bluetooth.BluetoothGattCallback;
  11. import android.bluetooth.BluetoothGattCharacteristic;
  12. import android.bluetooth.BluetoothGattDescriptor;
  13. import android.bluetooth.BluetoothGattService;
  14. import android.bluetooth.BluetoothProfile;
  15. import android.content.Context;
  16. import android.content.Intent;
  17. import android.content.SharedPreferences;
  18. import android.os.Bundle;
  19. import android.os.Handler;
  20. import android.os.IBinder;
  21. import android.os.Message;
  22. import android.preference.PreferenceManager;
  23. import android.util.Log;
  24. import android.widget.Button;
  25. import android.widget.TextView;
  26.  
  27.  
  28. public class BleService extends Service
  29. {
  30. private static final UUID SERVICE = UUID.fromString("624e957f-cb42-4cd6-bacc-84aeb898f69b");
  31. private static final UUID DATA = UUID.fromString("df342b03-53f9-43b4-acb6-62a63ca0615a");
  32. private static final byte[] ENABLE_SENSOR = {0x01};
  33.  
  34. private String temp;
  35. static final int MSG_REGISTER = 1;
  36. static final int MSG_UNREGISTER = 2;
  37. static final int MSG_START_SCAN = 3;
  38. static final int MSG_STATE_CHANGED = 4;
  39. static final int MSG_DEVICE_FOUND = 5;
  40. static final int MSG_DEVICE_CONNECT = 6;
  41. static final int MSG_DEVICE_DISCONNECT = 7;
  42. private static final long SCAN_PERIOD = 10000;
  43. public static final String TAG = "BleService";
  44. private static final String DEVICE_NAME = "BLEE";
  45. public static final String KEY_MAC_ADDRESSES = "KEY_MAC_ADDRESSES";
  46. private static BluetoothAdapter mBluetoothAdapter;
  47. private final Map<String, BluetoothDevice> mDevices = new HashMap<String, BluetoothDevice>();
  48. private Handler mHandler;
  49. private byte[] val;
  50. private BluetoothGatt mGatt = null;
  51. public float temperatura;
  52. public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent";
  53. public BleService()
  54. {
  55. mHandler = new Handler();
  56. }
  57.  
  58. public void startServices()
  59. {
  60. turnBluetoothOn();
  61. }
  62.  
  63. public void turnBluetoothOn()
  64. {
  65.  
  66. mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  67. //mBluetoothAdapter.isEnabled();
  68. mBluetoothAdapter.enable();
  69.  
  70. Log.d(TAG,"Bluetooth włączony");
  71. scanLeDevices();
  72. }
  73.  
  74. //scan area for ble devices
  75. private void scanLeDevices()
  76. {
  77.  
  78. mHandler.postDelayed(new Runnable()
  79. {
  80. @Override
  81. public void run()
  82. {
  83. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  84. }
  85. }, SCAN_PERIOD);
  86. mBluetoothAdapter.startLeScan(mLeScanCallback);
  87.  
  88. }
  89.  
  90.  
  91. private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback()
  92. {
  93.  
  94. @Override
  95. public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
  96. {
  97. if (device != null && !mDevices.containsValue(device) && device.getName() != null && device.getName().equals(DEVICE_NAME))
  98. {
  99. mDevices.put(device.getAddress(), device);
  100. Message msg = Message.obtain(null, MSG_DEVICE_FOUND);
  101. if (msg != null)
  102. {
  103. Bundle bundle = new Bundle();
  104. String[] addresses = mDevices.keySet().toArray(new String[mDevices.size()]);
  105. bundle.putStringArray(KEY_MAC_ADDRESSES, addresses);
  106. msg.setData(bundle);
  107. }
  108. Log.d(TAG, "Added " + device.getName() + ": " + device.getAddress());
  109. }
  110. connectToModule(macAddress);
  111. }
  112.  
  113.  
  114.  
  115. };
  116.  
  117. public void connectToModule(String mac)
  118. {
  119.  
  120. BluetoothDevice device = mDevices.get(mac);
  121. Log.d(TAG,"Wbiłem się do connectToModule");
  122. if (device != null)
  123. {
  124. mGatt = device.connectGatt(this, false, mGattCallback);
  125. Log.d(TAG,"Wbiłem się do modułu");
  126. }
  127. }
  128.  
  129. private BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
  130. {
  131. @Override
  132. public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
  133. {
  134. super.onConnectionStateChange(gatt, status, newState);
  135. Log.d(TAG, "Connection State Changed: " + (newState == BluetoothProfile.STATE_CONNECTED ? "Connected" : "Disconnected"));
  136.  
  137. if (newState == BluetoothProfile.STATE_CONNECTED)
  138. {
  139. Log.d(TAG,"Jestem w module!");
  140. gatt.discoverServices();
  141. }
  142. }
  143.  
  144. @Override
  145. public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  146. Log.d(TAG, "onServicesDiscovered: " + status);
  147. if (status == BluetoothGatt.GATT_SUCCESS)
  148. {
  149. subscribe(gatt);
  150. //Log.d(TAG,"Jestem w OnServicesDiscovered.");
  151. }
  152. }
  153.  
  154. @Override
  155. public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
  156. {
  157. Log.v(TAG, "onCharacteristicWrite: " + status);
  158. }
  159.  
  160. @Override
  161. public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
  162. {
  163. Log.v(TAG, "onDescriptorWrite: " + status);
  164. }
  165.  
  166.  
  167.  
  168. @Override
  169. public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,int status)
  170. {
  171. Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
  172.  
  173. if (characteristic.getUuid().equals(DATA))
  174. {
  175. val = characteristic.getValue();
  176. //String a=characteristic.getStringValue(1);
  177. //char s= (char)characteristic.getValue();
  178. //String s = val.getStringValue
  179. //Log.d(TAG,);
  180. float t = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
  181. temperatura = t/10;
  182. Log.d(TAG, "Value: "+ temperatura);
  183. String a = Double.toString(temperatura);
  184. String b = "SAKJLDFHNAKSJDA";
  185.  
  186.  
  187. }
  188. }
  189.  
  190. };
  191.  
  192.  
  193. private void subscribe(BluetoothGatt gatt)
  194. {
  195. Log.d(TAG,"Subscribe");
  196. BluetoothGattCharacteristic characteristic;
  197. characteristic = gatt.getService(SERVICE).getCharacteristic(DATA);
  198. gatt.readCharacteristic(characteristic);
  199.  
  200.  
  201. }
  202.  
  203. @Override
  204. public IBinder onBind(Intent intent)
  205. {
  206. // TODO Auto-generated method stub
  207. return null;
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement