Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.05 KB | None | 0 0
  1. package com.sixpmplc.ble_demo;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.Activity;
  5. import android.bluetooth.BluetoothAdapter;
  6. import android.bluetooth.BluetoothDevice;
  7. import android.bluetooth.BluetoothGatt;
  8. import android.bluetooth.BluetoothGattCallback;
  9. import android.bluetooth.BluetoothGattCharacteristic;
  10. import android.bluetooth.BluetoothGattDescriptor;
  11. import android.bluetooth.BluetoothGattService;
  12. import android.bluetooth.BluetoothManager;
  13. import android.bluetooth.BluetoothProfile;
  14. import android.bluetooth.le.BluetoothLeScanner;
  15. import android.bluetooth.le.ScanCallback;
  16. import android.bluetooth.le.ScanFilter;
  17. import android.bluetooth.le.ScanResult;
  18. import android.bluetooth.le.ScanSettings;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.content.pm.PackageManager;
  22. import android.os.Build;
  23. import android.os.Bundle;
  24. import android.os.Handler;
  25. import android.os.Message;
  26. import android.support.v7.app.ActionBarActivity;
  27. import android.util.Log;
  28. import android.widget.TextView;
  29. import android.widget.Toast;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.List;
  33.  
  34. @TargetApi(21)
  35. public class MainActivity extends ActionBarActivity {
  36. private BluetoothAdapter mBluetoothAdapter;
  37. private int REQUEST_ENABLE_BT = 1;
  38. private Handler mHandler;
  39. private static final long SCAN_PERIOD = 30000;
  40. private BluetoothLeScanner mLEScanner;
  41. private ScanSettings settings;
  42. private List<ScanFilter> filters;
  43. private BluetoothGatt mGatt;
  44. private BluetoothDevice mDevice;
  45.  
  46. // setup UI handler
  47. private final static int UPDATE_DEVICE = 0;
  48. private final static int UPDATE_VALUE = 1;
  49. private final Handler uiHandler = new Handler() {
  50. public void handleMessage(Message msg) {
  51. final int what = msg.what;
  52. final String value = (String) msg.obj;
  53. switch(what) {
  54. case UPDATE_DEVICE: updateDevice(value); break;
  55. case UPDATE_VALUE: updateValue(value); break;
  56. }
  57. }
  58. };
  59.  
  60. private void updateDevice(String devName){
  61. TextView t=(TextView)findViewById(R.id.dev_type);
  62. t.setText(devName);
  63. }
  64.  
  65. private void updateValue(String value){
  66. TextView t=(TextView)findViewById(R.id.value_read);
  67. t.setText(value);
  68. }
  69.  
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.activity_main);
  74. mHandler = new Handler();
  75. if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
  76. Toast.makeText(this, "BLE Not Supported",
  77. Toast.LENGTH_SHORT).show();
  78. finish();
  79. }
  80. final BluetoothManager bluetoothManager =
  81. (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  82. mBluetoothAdapter = bluetoothManager.getAdapter();
  83.  
  84. }
  85.  
  86. @Override
  87. protected void onResume() {
  88. super.onResume();
  89. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
  90. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  91. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  92. } else {
  93. if (Build.VERSION.SDK_INT >= 21) {
  94. mLEScanner = mBluetoothAdapter.getBluetoothLeScanner();
  95. settings = new ScanSettings.Builder()
  96. .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
  97. .build();
  98. filters = new ArrayList<ScanFilter>();
  99. }
  100. scanLeDevice(true);
  101. }
  102. }
  103.  
  104. @Override
  105. protected void onPause() {
  106. super.onPause();
  107. if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled()) {
  108. scanLeDevice(false);
  109. }
  110. }
  111.  
  112. @Override
  113. protected void onDestroy() {
  114. if (mGatt == null) {
  115. return;
  116. }
  117. mGatt.close();
  118. mGatt = null;
  119. super.onDestroy();
  120. }
  121.  
  122. @Override
  123. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  124. if (requestCode == REQUEST_ENABLE_BT) {
  125. if (resultCode == Activity.RESULT_CANCELED) {
  126. //Bluetooth not enabled.
  127. finish();
  128. return;
  129. }
  130. }
  131. super.onActivityResult(requestCode, resultCode, data);
  132. }
  133.  
  134. private void scanLeDevice(final boolean enable) {
  135. if (enable) {
  136. mHandler.postDelayed(new Runnable() {
  137. @Override
  138. public void run() {
  139. if (Build.VERSION.SDK_INT < 21) {
  140. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  141. } else {
  142. mLEScanner.stopScan(mScanCallback);
  143.  
  144. }
  145. }
  146. }, SCAN_PERIOD);
  147. if (Build.VERSION.SDK_INT < 21) {
  148. mBluetoothAdapter.startLeScan(mLeScanCallback);
  149. } else {
  150. mLEScanner.startScan(filters, settings, mScanCallback);
  151. }
  152. } else {
  153. if (Build.VERSION.SDK_INT < 21) {
  154. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  155. } else {
  156. mLEScanner.stopScan(mScanCallback);
  157. }
  158. }
  159. }
  160.  
  161.  
  162. private ScanCallback mScanCallback = new ScanCallback() {
  163. @Override
  164. public void onScanResult(int callbackType, ScanResult result) {
  165. Log.i("callbackType", String.valueOf(callbackType));
  166. String devicename = result.getDevice().getName();
  167.  
  168. if (devicename != null){
  169. if (devicename.startsWith("TAIDOC")){
  170. Log.i("result", "Device name: "+devicename);
  171. Log.i("result", result.toString());
  172. BluetoothDevice btDevice = result.getDevice();
  173. connectToDevice(btDevice);
  174. }
  175. }
  176.  
  177. }
  178.  
  179. @Override
  180. public void onBatchScanResults(List<ScanResult> results) {
  181. for (ScanResult sr : results) {
  182. Log.i("ScanResult - Results", sr.toString());
  183. }
  184. }
  185.  
  186. @Override
  187. public void onScanFailed(int errorCode) {
  188. Log.e("Scan Failed", "Error Code: " + errorCode);
  189. }
  190. };
  191.  
  192. private BluetoothAdapter.LeScanCallback mLeScanCallback =
  193. new BluetoothAdapter.LeScanCallback() {
  194. @Override
  195. public void onLeScan(final BluetoothDevice device, int rssi,
  196. byte[] scanRecord) {
  197. runOnUiThread(new Runnable() {
  198. @Override
  199. public void run() {
  200. Log.i("onLeScan", device.toString());
  201. connectToDevice(device);
  202. }
  203. });
  204. }
  205. };
  206.  
  207. public void connectToDevice(BluetoothDevice device) {
  208. if (mGatt == null) {
  209. Log.d("connectToDevice", "connecting to device: "+device.toString());
  210. this.mDevice = device;
  211. mGatt = device.connectGatt(this, false, gattCallback);
  212. scanLeDevice(false);// will stop after first device detection
  213. }
  214. }
  215.  
  216. private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
  217.  
  218.  
  219. @Override
  220. public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
  221. Log.i("onConnectionStateChange", "Status: " + status);
  222. switch (newState) {
  223. case BluetoothProfile.STATE_CONNECTED:
  224. Log.i("gattCallback", "STATE_CONNECTED");
  225.  
  226. //update UI
  227. Message msg = Message.obtain();
  228.  
  229. String deviceName = gatt.getDevice().getName();
  230. switch (deviceName){
  231. case "TAIDOC TD1261":
  232. deviceName = "Thermo";
  233. break;
  234. case "TAIDOC TD8255":
  235. deviceName = "SPO2";
  236. break;
  237. case "TAIDOC TD4279":
  238. deviceName = "SPO2";
  239. break;
  240. }
  241.  
  242. msg.obj = deviceName;
  243. msg.what = 0;
  244. msg.setTarget(uiHandler);
  245. msg.sendToTarget();
  246.  
  247. gatt.discoverServices();
  248. break;
  249. case BluetoothProfile.STATE_DISCONNECTED:
  250. Log.e("gattCallback", "STATE_DISCONNECTED");
  251. Log.i("gattCallback", "reconnecting...");
  252. BluetoothDevice mDevice = gatt.getDevice();
  253. mGatt = null;
  254. connectToDevice(mDevice);
  255. break;
  256. default:
  257. Log.e("gattCallback", "STATE_OTHER");
  258. }
  259.  
  260. }
  261.  
  262. @Override
  263. public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  264. mGatt = gatt;
  265. List<BluetoothGattService> services = gatt.getServices();
  266. Log.i("onServicesDiscovered", services.toString());
  267. BluetoothGattCharacteristic therm_char = services.get(2).getCharacteristics().get(0);
  268.  
  269. for (BluetoothGattDescriptor descriptor : therm_char.getDescriptors()) {
  270. descriptor.setValue( BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
  271. mGatt.writeDescriptor(descriptor);
  272. }
  273.  
  274. //gatt.readCharacteristic(therm_char);
  275. gatt.setCharacteristicNotification(therm_char, true);
  276.  
  277. }
  278.  
  279. @Override
  280. public void onCharacteristicRead(BluetoothGatt gatt,
  281. BluetoothGattCharacteristic
  282. characteristic, int status) {
  283. Log.i("onCharacteristicRead", characteristic.toString());
  284. byte[] value=characteristic.getValue();
  285. String v = new String(value);
  286. Log.i("onCharacteristicRead", "Value: " + v);
  287. //gatt.disconnect();
  288. }
  289.  
  290. @Override
  291. public void onCharacteristicChanged(BluetoothGatt gatt,
  292. BluetoothGattCharacteristic
  293. characteristic) {
  294. float char_float_value = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_FLOAT,1);
  295. Log.i("onCharacteristicChanged", Float.toString(char_float_value));
  296.  
  297.  
  298. String deviceName = gatt.getDevice().getName();
  299. String value = null;
  300. switch (deviceName){
  301. case "TAIDOC TD1261":
  302. value = Float.toString(char_float_value);
  303. break;
  304. case "TAIDOC TD8255":
  305. value = Float.toString(char_float_value*10);
  306. break;
  307. }
  308.  
  309. //update UI
  310. Message msg = Message.obtain();
  311. msg.obj = value;
  312. msg.what = 1;
  313. msg.setTarget(uiHandler);
  314. msg.sendToTarget();
  315.  
  316.  
  317.  
  318. //gatt.disconnect();
  319. }
  320.  
  321. };
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement