Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bluetoothlowenergy;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- import android.app.Activity;
- import android.app.Service;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothDevice;
- import android.bluetooth.BluetoothGatt;
- import android.bluetooth.BluetoothGattCallback;
- import android.bluetooth.BluetoothGattCharacteristic;
- import android.bluetooth.BluetoothGattDescriptor;
- import android.bluetooth.BluetoothGattService;
- import android.bluetooth.BluetoothProfile;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.IBinder;
- import android.os.Message;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.widget.Button;
- import android.widget.TextView;
- public class BleService extends Service
- {
- private static final UUID SERVICE = UUID.fromString("624e957f-cb42-4cd6-bacc-84aeb898f69b");
- private static final UUID DATA = UUID.fromString("df342b03-53f9-43b4-acb6-62a63ca0615a");
- private static final byte[] ENABLE_SENSOR = {0x01};
- private String temp;
- static final int MSG_REGISTER = 1;
- static final int MSG_UNREGISTER = 2;
- static final int MSG_START_SCAN = 3;
- static final int MSG_STATE_CHANGED = 4;
- static final int MSG_DEVICE_FOUND = 5;
- static final int MSG_DEVICE_CONNECT = 6;
- static final int MSG_DEVICE_DISCONNECT = 7;
- private static final long SCAN_PERIOD = 10000;
- public static final String TAG = "BleService";
- private static final String DEVICE_NAME = "BLEE";
- public static final String KEY_MAC_ADDRESSES = "KEY_MAC_ADDRESSES";
- private static BluetoothAdapter mBluetoothAdapter;
- private final Map<String, BluetoothDevice> mDevices = new HashMap<String, BluetoothDevice>();
- private Handler mHandler;
- private byte[] val;
- private BluetoothGatt mGatt = null;
- public float temperatura;
- public static final String BROADCAST_ACTION = "com.websmithing.broadcasttest.displayevent";
- public BleService()
- {
- mHandler = new Handler();
- }
- public void startServices()
- {
- turnBluetoothOn();
- }
- public void turnBluetoothOn()
- {
- mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- //mBluetoothAdapter.isEnabled();
- mBluetoothAdapter.enable();
- Log.d(TAG,"Bluetooth włączony");
- scanLeDevices();
- }
- //scan area for ble devices
- private void scanLeDevices()
- {
- mHandler.postDelayed(new Runnable()
- {
- @Override
- public void run()
- {
- mBluetoothAdapter.stopLeScan(mLeScanCallback);
- }
- }, SCAN_PERIOD);
- mBluetoothAdapter.startLeScan(mLeScanCallback);
- }
- private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback()
- {
- @Override
- public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord)
- {
- if (device != null && !mDevices.containsValue(device) && device.getName() != null && device.getName().equals(DEVICE_NAME))
- {
- mDevices.put(device.getAddress(), device);
- Message msg = Message.obtain(null, MSG_DEVICE_FOUND);
- if (msg != null)
- {
- Bundle bundle = new Bundle();
- String[] addresses = mDevices.keySet().toArray(new String[mDevices.size()]);
- bundle.putStringArray(KEY_MAC_ADDRESSES, addresses);
- msg.setData(bundle);
- }
- Log.d(TAG, "Added " + device.getName() + ": " + device.getAddress());
- }
- connectToModule(macAddress);
- }
- };
- public void connectToModule(String mac)
- {
- BluetoothDevice device = mDevices.get(mac);
- Log.d(TAG,"Wbiłem się do connectToModule");
- if (device != null)
- {
- mGatt = device.connectGatt(this, false, mGattCallback);
- Log.d(TAG,"Wbiłem się do modułu");
- }
- }
- private BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
- {
- @Override
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
- {
- super.onConnectionStateChange(gatt, status, newState);
- Log.d(TAG, "Connection State Changed: " + (newState == BluetoothProfile.STATE_CONNECTED ? "Connected" : "Disconnected"));
- if (newState == BluetoothProfile.STATE_CONNECTED)
- {
- Log.d(TAG,"Jestem w module!");
- gatt.discoverServices();
- }
- }
- @Override
- public void onServicesDiscovered(BluetoothGatt gatt, int status) {
- Log.d(TAG, "onServicesDiscovered: " + status);
- if (status == BluetoothGatt.GATT_SUCCESS)
- {
- subscribe(gatt);
- //Log.d(TAG,"Jestem w OnServicesDiscovered.");
- }
- }
- @Override
- public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
- {
- Log.v(TAG, "onCharacteristicWrite: " + status);
- }
- @Override
- public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)
- {
- Log.v(TAG, "onDescriptorWrite: " + status);
- }
- @Override
- public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,int status)
- {
- Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid());
- if (characteristic.getUuid().equals(DATA))
- {
- val = characteristic.getValue();
- //String a=characteristic.getStringValue(1);
- //char s= (char)characteristic.getValue();
- //String s = val.getStringValue
- //Log.d(TAG,);
- float t = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
- temperatura = t/10;
- Log.d(TAG, "Value: "+ temperatura);
- String a = Double.toString(temperatura);
- String b = "SAKJLDFHNAKSJDA";
- }
- }
- };
- private void subscribe(BluetoothGatt gatt)
- {
- Log.d(TAG,"Subscribe");
- BluetoothGattCharacteristic characteristic;
- characteristic = gatt.getService(SERVICE).getCharacteristic(DATA);
- gatt.readCharacteristic(characteristic);
- }
- @Override
- public IBinder onBind(Intent intent)
- {
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement