Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package huadi.androidgamepad;
- import huadi.androidgamepad.Events.InputDevice;
- import huadi.androidgamepad.bluetooth.BleStatu;
- import huadi.androidgamepad.bluetooth.RFduinoUUIDs;
- import java.util.Date;
- import java.util.UUID;
- 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.BluetoothManager;
- import android.bluetooth.BluetoothProfile;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.os.IBinder;
- import android.os.Looper;
- import android.os.Message;
- import android.util.Log;
- import android.widget.Toast;
- public class MyService extends Service
- {
- private final static String TAG = "RFduinoService";
- private Looper mServiceLooper;
- private ServiceHandler mServiceHandler;
- Events events = new Events();
- int id = 0;
- // Handler that receives messages from the thread
- private final class ServiceHandler extends Handler
- {
- public ServiceHandler(Looper looper)
- {
- super(looper);
- }
- @Override
- public void handleMessage(Message msg)
- {
- // Normally we would do some work here, like download a file.
- // For our sample, we just sleep for 5 seconds.
- long endTime = System.currentTimeMillis() + 5 * 1000;
- while (System.currentTimeMillis() < endTime)
- {
- synchronized (this)
- {
- try
- {
- wait(1000);
- //events.m_Devs.get(id).SendTest(0x3be, 0x275);
- Log.e("time", new Date().toString());
- Log.e("readInfo", read());
- }
- catch (Exception e)
- {
- }
- }
- }
- // Stop the service using the startId, so that we don't stop
- // the service in the middle of handling another job
- //stopSelf(msg.arg1);
- }
- }
- @Override
- public void onCreate()
- {
- Events.intEnableDebug(1);
- events.Init();
- for (InputDevice idev : events.m_Devs)
- {
- idev.Open(true);
- if (idev.getOpen() && idev.getName().contains("touch"))
- {
- //Toast.makeText(this, "123", Toast.LENGTH_LONG).show();
- id = idev.getId();
- }
- }
- if(initialize())
- if(connect(BleStatu.DeviceMacAddress))
- Log.e(TAG, "conn");
- // Start up the thread running the service. Note that we create a
- // separate thread because the service normally runs in the process's
- // main thread, which we don't want to block. We also make it
- // background priority so CPU-intensive work will not disrupt our UI.
- HandlerThread thread = new HandlerThread("ServiceStartArguments", android.os.Process.THREAD_PRIORITY_BACKGROUND);
- thread.start();
- // Get the HandlerThread's Looper and use it for our Handler
- mServiceLooper = thread.getLooper();
- mServiceHandler = new ServiceHandler(mServiceLooper);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
- Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
- // For each start request, send a message to start a job and deliver the
- // start ID so we know which request we're stopping when we finish the job
- Message msg = mServiceHandler.obtainMessage();
- msg.arg1 = startId;
- mServiceHandler.sendMessage(msg);
- // If we get killed, after returning from here, restart
- return START_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent)
- {
- // We don't provide binding, so return null
- return null;
- }
- @Override
- public void onDestroy()
- {
- Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
- }
- private BluetoothManager mBluetoothManager;
- private BluetoothAdapter mBluetoothAdapter;
- private BluetoothGatt mBluetoothGatt;
- private BluetoothGattService mBluetoothGattService;
- private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback()
- {
- @Override
- public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
- {
- if (newState == BluetoothProfile.STATE_CONNECTED)
- {
- Log.i(TAG, "Connected to RFduino.");
- Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());
- }
- else if (newState == BluetoothProfile.STATE_DISCONNECTED)
- {
- Log.i(TAG, "Disconnected from RFduino.");
- }
- else {
- Log.e(TAG, "" + newState);
- }
- }
- @Override
- public void onServicesDiscovered(BluetoothGatt gatt, int status)
- {
- if (status == BluetoothGatt.GATT_SUCCESS)
- {
- mBluetoothGattService = gatt.getService(RFduinoUUIDs.RFduino_Service_UUID);
- if (mBluetoothGattService == null)
- {
- Log.e(TAG, "RFduino GATT service not found!");
- return;
- }
- BluetoothGattCharacteristic receiveCharacteristic = mBluetoothGattService.getCharacteristic(RFduinoUUIDs.Receive_Characteristic_UUID);
- if (receiveCharacteristic != null)
- {
- BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic.getDescriptor(RFduinoUUIDs.Client_Characteristic_Config_UUID);
- if (receiveConfigDescriptor != null)
- {
- gatt.setCharacteristicNotification(receiveCharacteristic, true);
- receiveConfigDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
- gatt.writeDescriptor(receiveConfigDescriptor);
- Log.e(TAG + 1, "" + BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
- }
- else
- {
- Log.e(TAG, "RFduino receive config descriptor not found!");
- }
- }
- else
- {
- Log.e(TAG, "RFduino receive characteristic not found!");
- }
- }
- else
- {
- Log.w(TAG, "onServicesDiscovered received: " + status);
- }
- }
- @Override
- public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
- {
- if (status == BluetoothGatt.GATT_SUCCESS)
- {
- Log.e(TAG, "a");
- }
- }
- @Override
- public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)
- {
- Log.e(TAG, "b");
- }
- };
- public boolean initialize()
- {
- if (mBluetoothManager == null)
- {
- mBluetoothManager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
- if (mBluetoothManager == null)
- {
- Log.e(TAG, "Unable to initialize BluetoothManager.");
- return false;
- }
- }
- mBluetoothAdapter = mBluetoothManager.getAdapter();
- if (mBluetoothAdapter == null)
- {
- Log.e(TAG, "Unable to obtain a BluetoothAdapter.");
- return false;
- }
- return true;
- }
- public boolean connect(final String address)
- {
- if (mBluetoothAdapter == null || address == null)
- {
- Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
- return false;
- }
- // Previously connected device. Try to reconnect.
- if (mBluetoothGatt != null)
- {
- Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
- return mBluetoothGatt.connect();
- }
- final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
- // We want to directly connect to the device, so we are setting the autoConnect
- // parameter to false.
- mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
- Log.d(TAG, "Trying to create a new connection.");
- return true;
- }
- public void disconnect()
- {
- if (mBluetoothAdapter == null || mBluetoothGatt == null)
- {
- Log.w(TAG, "BluetoothAdapter not initialized");
- return;
- }
- mBluetoothGatt.disconnect();
- }
- public void close()
- {
- if (mBluetoothGatt == null)
- {
- return;
- }
- mBluetoothGatt.close();
- mBluetoothGatt = null;
- }
- public String read()
- {
- BluetoothGattService gattService = mBluetoothGatt.getService(UUID.fromString("00002220-0000-1000-8000-00805f9b34fb"));
- if (mBluetoothGatt == null)
- {
- Log.w(TAG, "mBluetoothGatt not initialized");
- return "mBluetoothGatt not initialized";
- }
- else if(gattService == null) // 就是這裡!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
- {
- Log.w(TAG, "gattService not initialized");
- return "gattService not initialized";
- }
- BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(RFduinoUUIDs.Receive_Characteristic_UUID);
- return "" + mBluetoothGatt.readCharacteristic(characteristic);
- }
- public boolean send(byte[] data)
- {
- if (mBluetoothGatt == null || mBluetoothGattService == null)
- {
- Log.w(TAG, "BluetoothGatt not initialized");
- return false;
- }
- BluetoothGattCharacteristic characteristic = mBluetoothGattService.getCharacteristic(RFduinoUUIDs.Send_Characteristic_UUID);
- if (characteristic == null)
- {
- Log.w(TAG, "Send characteristic not found");
- return false;
- }
- characteristic.setValue(data);
- characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
- return mBluetoothGatt.writeCharacteristic(characteristic);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment